hdu 2709 递推

题意:给出一个数,把他拆成2^n和的形式,问有多少种拆法

链接:点我

对6进行分析

1 1 1 1 1 1

1 1 1 1 2

1 1 2 2

1 1 4

2 2 4

2 4

对最上面4个,显然是由4的拆分然后每个加+1 +1得到的

最下面是由,2的拆分乘2得到的

设a[n]为和为 n 的种类数;

根据题目可知,加数为2的N次方,即 n 为奇数时等于它前一个数 n-1 的种类数 a[n-1] ,若 n 为偶数时分加数中有无 1 讨论,即关键是对 n 为偶数时进行讨论:

1.n为奇数,a[n]=a[n-1]

2.n为偶数:

(1)如果加数里含1,则一定至少有两个1,即对n-2的每一个加数式后面 +1+1,总类数为a[n-2];

(2)如果加数里没有1,即对n/2的每一个加数式乘以2,总类数为a[n-2];

所以总的种类数为:a[n]=a[n-2]+a[n/2];

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 #define MOD 1000000000
10 const int INF=0x3f3f3f3f;
11 const double eps=1e-5;
12 typedef long long ll;
13 #define cl(a) memset(a,0,sizeof(a))
14 #define ts printf("*****
");
15 const int MAXN=1000010;
16 int a[MAXN];
17 int n,m,tt;
18 void init()
19 {
20     a[1]=1;
21     a[2]=2;
22     a[3]=2;
23     for(int i=4;i<MAXN;i++)
24     {
25         if(i%2) a[i]=a[i-1];
26         else
27         {
28             a[i]=a[i/2]+a[i-2];
29             a[i]%=MOD;
30         }
31     }
32 }
33 int main()
34 {
35     int i,j,k;
36     #ifndef ONLINE_JUDGE
37     freopen("1.in","r",stdin);
38     #endif
39     init();
40     while(scanf("%d",&n)!=EOF)
41     {
42         printf("%d
",a[n]);
43     }
44 }
原文地址:https://www.cnblogs.com/cnblogs321114287/p/4491962.html