假如输入4
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
一共5种
假如输入3 用母函数的方法就是写成
(1+X+X2+X3)(1+X2)(1+X3) 展开后 求X3的系数
假如输入n
就是(1+X+X2+X3+X4....)(1+X2+X4+X6..)(1+X3+X6...)(....)
Sample Input
4
10
20
Sample Output
5
42
627
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string> 6 # include <cmath> 7 # include <queue> 8 # include <list> 9 # define LL long long 10 using namespace std ; 11 12 int c1[10010], c2[10010] ; 13 int main() 14 { 15 //freopen("in.txt","r",stdin) ; 16 int nNum; 17 int i, j, k; 18 19 while(cin >> nNum) 20 { 21 for(i=0; i<=nNum; ++i) 22 { 23 c1[i] = 1; 24 c2[i] = 0; 25 } 26 for(i=2; i<=nNum; ++i) 27 { 28 29 for(j=0; j<=nNum; ++j) 30 for(k=0; k+j<=nNum; k+=i) 31 { 32 c2[j+k] += c1[j]; 33 } 34 for(j=0; j<=nNum; ++j) 35 { 36 c1[j] = c2[j]; 37 c2[j] = 0; 38 } 39 } 40 cout << c1[nNum] << endl; 41 } 42 return 0; 43 }
时间: 2024-10-14 18:46:34