【题目大意】:把一个数拆成若干个2的幂的和,问有几种拆法。
【解题思路】:递推,当一个数是奇数的时候,没办法,它只能从它前面的那个偶数加个1转移过来。当一个数是偶数的话,可以由它前一个数+1转移过来,也可以是它的一半转移过来,因为偶数换成二进制之后末尾一定是0,也就是偶数左移一位的组合方式。
【代码】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
#define mod 1000000
int f[2001000];
void init(){
memset(f,0,sizeof(f));
f[0]=1;
f[1]=1;
for (int i=2; i<=2000100; i++){
if (i%2==0) f[i]=(f[i-1]+f[i/2])%mod;
else f[i]=f[i-1]%mod;
}
}
int main() {
init();
int T,n;
cin >> T;
while (T--){
scanf("%d",&n);
cout << f[n] << endl;
}
return 0;
}
f[n][k]:至少以2^k的分量去分解n时有多少种分解方案。题目的解即为f[n][0]。