Description
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).
給出一個N(1≤N≤10^6),使用一些2的若干次冪的數相加來求之.問有多少種方法
Input
一個整數N.
Output
方法數.這個數可能很大,請輸出其在十進制下的最後9位.
Sample Input
Sample Output
有以下六種方式
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
HINT
本來認爲是有倍數關係的2的次冪想用拆成1之後組合數來做的,後來發現直接dp就行。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=1e6+1;
const int mod=1e9;
int dp[MAXN];
int main(int argc, char *argv[])
{
int n,i,j;
scanf("%d",&n);
dp[1]=1;
for(i=2;i<=n;i++)
if(i%2==1) dp[i]=dp[i-1];
else dp[i]=(dp[i-1]+dp[i>>1])%mod;
printf("%d\n",dp[n]);
return 0;
}