HDU1398-Square Coins

简介:

Square Coins
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8148    Accepted Submission(s): 5532


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
2
10
30
0
 

Sample Output
1
4
27
 

Source
Asia 1999, Kyoto (Japan)
 

题意:硬币面值为平方数,面值分别为1,4,9,16......289 (=17^2)
让你求对于面值n,你用以上面值的硬币有多少种拼法儿。

思路:母函数。

1个1元的钞票可以用函数1+x表示,
1个4元的钞票可以用函数1+x^4表示,
1个9元的钞票可以用函数1+x^9表示,
1个16元的钞票可以用函数1+x^16表示,

几种钱币的组合的情况,可以用以上几个函数的乘积表示:
(1+x)(1+x^4)(1+x^9)(1+x^16)

=(1+x^4+x+x^5)(1+x^16+x^9+x^25)

=1+x^16+x^9+x^25+x^4+x^20+x^13+x^29+x+x^17+x^10+x^26+x^5+x^21+x^14+x^30         

从上面的函数知道:可拼出从1元到10元,系数便是方案数。
例如右端有x^5 项,即称出5元的方案有1:5=4+1;同样,10=1+9;14=1+9+4。
故称出6克的方案有1,称出14克的方案有1?

反过来就是:

求组成n的可拼组合总数
(1+x^1+x^2+x^3+....+x^n)*(1+x^4+x^8+x^16+....+x^n)*(1+x^9+x^18+x^27+....+x^n)......(1+x^m+x^2m+x^3m+....+x^n)
其中<m<=n>

 

 


AC的第一道关于母函数的题,纪念一下(*^__^*)
AC代码:

#include<stdio.h>
#include<string.h>
#define MAX 400
int c1[MAX],c2[MAX];
int main()
{
 int i,j,n,m,k;
 while(scanf("%d",&n),n!=0)
 {
  for(i=0;i<=n;i++)
  {
   c1[i]=0;c2[i]=0;
  }
  for(i=0;i<=n;i++) c1[i]=1;
  m=3;
  for(i=4;i<=n;i=m*m,m++)
  {
   for(j=0;j<=n;j++)
   for(k=0;k+j<=n;k+=i)
   {
    c2[j+k]+=c1[j];
   }
   for(j=0;j<=n;j++)
   {
    c1[j]=c2[j];c2[j]=0;
   }
  }
  printf("%d\n",c1[n]);
 }
 return 0;
} 
相关文章
HDU 1506 Largest Rectangle in a Histogram(单调栈)
HDU 1506 Largest Rectangle in a Histogram(单调栈)
HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)
HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)
68 0
【1105】Spiral Matrix (25分)【螺旋矩阵】
【1105】Spiral Matrix (25分)【螺旋矩阵】 【1105】Spiral Matrix (25分)【螺旋矩阵】
102 0
【PTA】7-6 求最大公约数 (40point(s))
【PTA】7-6 求最大公约数 (40point(s))
168 0
|
机器学习/深度学习
|
人工智能