原題
Square Coins
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
210
30
0
Sample Output
14
27
題意
涉及知識及算法
代碼
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX_V = 300;
int C1[MAX_V];
int C2[MAX_V];
int v;
void solve()
{
//C2爲輔助的存貯
memset(C2,0,sizeof(C2));
//把第一個表達式的所有的係數初始化爲1
for(int i=0;i<=MAX_V;i++)
{
C1[i]=1;
}
//第2至第n個表達式
for(int i=2;i<=MAX_V;i++)
{
//C1的數組實質上存儲的是前i-1個表達式累乘後各個項係數
//j是標識前i-1個表達式累乘後各個項的指數
for(int j=0;j<=MAX_V;j++)
{
//k爲第i個表達式中每個項的指數,若爲第二個式子,則,第一
//項爲x^0,第二項爲x^4,第三項爲x^8,故步長爲i*i
for(int k=0;j+k<=MAX_V;k+=i*i)
{
//若前面i-1個式子計算後某項爲ax^j(係數爲a),
//則乘法(ax^j)*(x*k)=ax^(j+k)可表示爲C2[j+k]+=a=C1[j];
C2[j+k]+=C1[j];
}
}
//刷新爲當前i個表達式累乘後各個項係數
for(int j=0;j<=MAX_V;j++)
{
C1[j]=C2[j];
C2[j]=0;
}
}
}
int main()
{
solve();
while(cin>>v&&v)
{
cout<<C1[v]<<endl;
}
return 0;
}