POJ 2229 Sumsets(簡單DP)
阿新 • • 發佈:2017-09-09
nbsp 自己 arc arm 相加 res sum name base
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).
A single line with a single integer, N.
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).
Input
Output
The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).Sample Input
7
Sample Output
6
題意:
一個數N能被拆分成多少種只由2的n次方相加的組合。
題解:
剛開始怎麽也推不出來,╮(╯▽╰)╭自己還是好菜啊。。看了別人的題解後發現其實很容易。?當i為奇數時,dp[i]=dp[i-1];當i為偶數時,dp[i]=dp[i-1]+dp[i/2]。
i為奇數,dp[i]等於前一個偶數的組合+1。偶數時,等於前一個數+1的組合數,加上dp[i/2]的組合數(可以看成提個公因子2)。
#include<iostream> using namespace std; const int maxn=1e6+5,mod=1e9; int dp[maxn]; int main() { int n; cin>>n; dp[1]=1,dp[2]=2; for(int i=3;i<=n;i++) dp[i]=(i&1)?dp[i-1]:(dp[i-2]+dp[i>>1])%mod; cout<<dp[n]<<endl; }
POJ 2229 Sumsets(簡單DP)