hdoj 1398 Square Coins

来源:互联网 发布:算法设计与分析电子书 编辑:程序博客网 时间:2024/06/05 17:23

Square Coins

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


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
 
//次题所采用的多项式相乘的算法是先以第一个表达式的变量去遍历第二个表达式的变量 #include <stdio.h>#include<string.h>#define N 1010 int main(){    int i,j,k;    int c1[N],c2[N];//k代表指数,c1表示系数,c2做中间过度的变量     int n;    while(scanf("%d",&n)!=EOF&&n)    {        for(i=0;i<=n;i++)//初始化c1为1,代表第一个表达式的各个变量的系数皆为1        {            c1[i]=1;            c2[i]=0;        }        for(i=2;i<=17;i++) //从第二个表达式开始,题目平方 数最大为17          {             for(j=0;j<=n;j++)//J表示进行一次表达式相乘后,第一个表达式的的第J个变量               for(k=0;k+j<=n;k+=i*i)// 此处表示第一个表达式的第J个变量遍历第二个表达式 (逻辑上的第I个表达式)                {                   c2[j+k]+=c1[j];//C1[j]存储的是上一次运算结束后一个表达式中指数为j的系数,因为此时的第二个表达式的所有的系数均为1,其实就是将便利的过程中的拥有k+j指数的系数加1                }               for(k=0;k<=n;k++)               {                   c1[k]=c2[k];//将c2 存储的系数赋值给相应的c1,然后清零c2,                    c2[k]=0;               }         }         printf("%d\n",c1[n]);    }    return 0;}
母函数在知识点:http://blog.csdn.net/zhangxiaoxiang123/article/details/48429459
0 0
原创粉丝点击