HDU:1398 Square Coins(母函数)

Square Coins

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


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
 

Recommend
Ignatius.L

题目大意:给你一个数n,问你有多少种情况可以组成数n,组成n的数只能是1^2 2^2 3^2 4^2这些数,数量不限,求方法数。

解题思路:母函数。第一次接触母函数,母函数大概就是可以解决求方法组合数之类的问题。现在大概懂了些,贴上我学习的博客:
先看这个http://blog.csdn.net/ydykl/article/details/6655142
再看杭电的母函数ppt讲解
在看这个,大概就能懂了。http://blog.csdn.net/xiaofei_it/article/details/17042651

代码如下:
#include <cstdio>
#include <cstring>
int a1[310];//ai表示指数也就是价格i的系数 
int a2[310];
//G(x)=(1+x^1+x^2+x^3+x^4+....)(1+x^4+x^8+x^12+...)(1+x^9+x^18+x^27+...)
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
		{
			break;
		}
		for(int i=0;i<=n;i++)//初始化 (1+x^1+x^2+x^3+x^4+....)
		{
			a1[i]=1;//对应上式a1[i]=1就是说指数为i的x的系数为1 
		}
		memset(a2,0,sizeof(a2));//a2是中间数据 
		for(int i=2;i*i<=n;i++)//从2^2开始枚举,也就是枚举出 (1+x^4+x^8+x^12+...)
		{
			for(int j=0;j<=n;j++)//扫描已知的前面的项的情况,如该这一项(1+x^9+x^18+x^27+...),那么扫描的是 (1+x^1+x^2+x^3+x^4+....)(1+x^4+x^8+x^12+...)的结果 
			{
				for(int k=0;k+j<=n;k=k+i*i)//枚举出 (1+x^4+x^8+x^12+...)
				{
					a2[j+k]=a2[j+k]+a1[j];//j+k就是指数当中的x^j*x^k=x^(j+k)   例如4a^4*a^9=4a^13即结果a2[13]=a[13]+a[4]=a[13]+4,那么指数是13的情况就又多了4种 
				}
			}
			for(int j=0;j<=n;j++)//把a2储存的中间数据传递给a1,并将a2清空 
			{
				a1[j]=a2[j];
				a2[j]=0;
			}
		}
		printf("%d\n",a1[n]);
	}
	return 0;
 } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值