閱讀851 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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;
} 

最後更新:2017-04-03 05:39:49

  上一篇:go 機房收費係統之上機、下機
  下一篇:go 繼承ViewGroup重寫onMeasure方法的詳解