Square Coins

来源:互联网 发布:大数据平台服务目录 编辑:程序博客网 时间:2024/05/18 00:36
3月11号校赛详细安排

Square Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11828    Accepted Submission(s): 8105


Problem Description
People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland.
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.
 

Input
The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.
 

Output
For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output.
 

Sample Input
210300
 

Sample Output
1427



解题报告:母函数模板题

code

#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<sstream>#include<algorithm>#include<math.h>#include<queue>#include<stack>#include<map>#include<set>using namespace std;typedef long long ll;const int maxn =305;  /*题目可能的最大组合价值,可以大一点*/int c1[maxn], c2[maxn];/*c1表示单词组合价值为i时的数目为c1[i];c2表示中间量,保存每一次的情况*/int price[18];/*硬币价值,数据范围取n的最大值+1;下标从1开始*/int n;/*n种价值的硬币;n个输入数据*/int m;/*输入数据的最大组合价值*/int main(){ //   freopen("input.txt","r",stdin);    memset(price,0,sizeof(price));    m=300;n=17;    for(int i=1;i<=n;i++){/*对price初始化*/        price[i]=i*i;    }    memset(c1,0,sizeof(c1));    memset(c2,0,sizeof(c2));    for(int i=0;i<=m;i+=price[1])/*第一个表达式(1+x+x^2+..x^n)初始化*/        c1[i]=1;    for(int i=2;i<=n;i++)    /*i表示第i个表达式*/    {        for(int j=0;j<=m;j++)/*j表示前面i个表达式累乘后的表达式里第j个变量*/            for(int k=0;k+j<=m;k+=price[i])/*k表示第j个的指数,指数为价值*/                c2[k+j]+=c1[j];        for(int j=0;j<=m;j++)        {            c1[j]=c2[j];            c2[j]=0;        }    }    int a;    while(~scanf("%d",&a))    {        if(!a) break;        printf("%d\n",c1[a]);    }    return 0;}



0 0
原创粉丝点击