There are four combinations of coins to pay ten credits:
ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Silverland.
2 10 30 0
1 427
题意:有17中硬币,分别是1-17的平方,然后问你300以内的数可有多少中组成方法
这是简单的母函数模板提目
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int s[20],ans[305],tmp[305]; int mu() { for(int i=1;i<=17;i++) s[i]=i*i; memset(ans,0,sizeof(ans)); memset(tmp,0,sizeof(tmp)); for(int i=0;i<=300;i++) ans[i]=1; for(int i=2;i<=17;i++) { for(int j=0;j<=300;j++) for(int k=0;k<=300&&k*s[i]+j<=300;k++) { tmp[j+k*s[i]]+=ans[j]; } for(int j=0;j<=300;j++) { ans[j]=tmp[j]; tmp[j]=0; } } return 0; } int main() { int n; while(cin>>n) { if(n==0) break; mu(); cout<<ans[n]<<endl; } return 0; }