母函数介绍见另一篇随笔HDU1028Ignatius and the Princess III(母函数)
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<map> 5 #include<vector> 6 #include<set> 7 #include<stack> 8 #include<queue> 9 #include<algorithm> 10 #include<stdlib.h> 11 using namespace std; 12 #define MAX(a,b) (a > b ? a : b) 13 #define MIN(a,b) (a < b ? a : b) 14 #define MAXN 400005 15 #define INF 2000000007 16 #define mem(a) memset(a,0,sizeof(a)) 17 18 int c1[301],c2[301]; 19 20 int main() 21 { 22 int n; 23 while(~scanf("%d",&n) && n!= 0) 24 { 25 int i; 26 for(i =0 ;i<=n;i++) 27 { 28 c1[i] = 1; 29 c2[i] = 0; 30 } 31 for(i =2 ;i<=17; i++) 32 { 33 for(int j = 0;j<=n;j++) 34 { 35 for(int k = 0; k+j <= n; k+=i*i) 36 { 37 c2[j+k]+=c1[j]; 38 } 39 } 40 for(int j = 0; j<=n;j++) 41 { 42 c1[j] = c2[j]; 43 c2[j] = 0; 44 } 45 } 46 printf("%d ",c1[n]); 47 } 48 return 0; 49 }
另外我也写了一个其他方法,可以看看
#include<iostream> #include<stdio.h> #include<string.h> #include<map> #include<vector> #include<set> #include<stack> #include<queue> #include<algorithm> #include<stdlib.h> using namespace std; #define MAX(a,b) (a > b ? a : b) #define MIN(a,b) (a < b ? a : b) #define MAXN 400005 #define INF 2000000007 #define mem(a) memset(a,0,sizeof(a)) int ans(int n,int k) { if(k == 1)return 1; int x = n/(k*k); int num=0; for(int i =0 ;i<= x; i++) { num += ans(n-i*k*k, k-1); } return num; } int main() { int n; while(~scanf("%d",&n) && n != 0) { int i,key; for(i =1;i<=18;i++) { if(i*i > n){ key = i-1; break; } } printf("%d ",ans(n,key)); } return 0; }