【hdu 1398】 Square Coins

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
 

这道题的题意是你有1,4,9,16,25…289元的硬币无数个,求给定价格的支付方案数,这题可以用母函数先预处理300以内所有价格的方案数,再直接输出,下面是程序:
#include<stdio.h>
#include<iostream>
#define ll long long
using namespace std;
const int N=305;
ll s[N],a[N];
int read(){
	int s=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		s*=10;
		s+=c-'0';
		c=getchar();
	}
	return s;
}
void out(ll n){
	if(n>9){
		out(n/10);
	}
	putchar(n%10+'0');
}
int main(){
	int i,j,k,n;
	for(i=0;i<=N;i++){
		s[i]=1;
		a[i]=0;
	}
	for(i=2;i<=N;i++){
		for(j=0;j<=N;j++){
			for(k=0;j+k<=N;k+=i*i){
				a[j+k]+=s[j];
			}
		}
		for(j=0;j<=N;j++){
			s[j]=a[j];
			a[j]=0;
		}
	}
	while(n=read()){
		out(s[n]);
		putchar('\n');
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tlecoce/article/details/79349734