Square Coins

本是古典 何须时尚 2022-08-07 13:44 307阅读 0赞

Square Coins

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

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

  1. 2
  2. 10
  3. 30
  4. 0

Sample Output

  1. 1
  2. 4
  3. 27

母函数,这一点有点特殊,数字不连续而是指定的数字范围

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. using namespace std;
  5. int main()
  6. {
  7. int i,j,k,flag,n,a[315],b[315];
  8. while(cin>>n&&n)
  9. {
  10. for(i=0;i<=n;i++)
  11. {
  12. a[i]=1;
  13. b[i]=0;
  14. }
  15. for(i=2;i*i<=n&&i<=17;i++)
  16. {
  17. for(j=0;j<=n;j++)
  18. {
  19. for(k=0;k+j<=n;k+=i*i)
  20. {
  21. b[j+k]+=a[j];
  22. }
  23. }
  24. for(j=0;j<=n;j++)
  25. {
  26. a[j]=b[j];
  27. b[j]=0;
  28. }
  29. }
  30. printf("%d\n",a[n]);
  31. }
  32. }

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读