动态规划:HDU-1398-Square Coins(母函数模板)
解题心得:
1、其实此题有两种做法,动态规划,母函数。个人更喜欢使用动态规划来做,也可以直接套母函数的模板
Square Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12191 Accepted Submission(s): 8352
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)
dp:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int va[18];
for(int i=1;i<=17;i++)
{
va[i] = i*i;
}
int n;
int d[310];
for(int i=0;i<310;i++)
{
d[i] = 1;
}
for(int i=2;i<=17;i++)
{
for(int j=va[i];j<310;j++)
d[j] += d[j-va[i]];
}
while(~scanf("%d",&n))
{
if(n == 0)
break;
printf("%d\n",d[n]);
}
}
母函数:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int c1[310],c2[310];
int n;
while(scanf("%d",&n) && n)
{
for(int i=0;i<=n;i++)
{
c1[i] = 1;
c2[i] = 0;
} for(int i=2;i*i<=n;i++)
{
for(int j=0;j<=n;j++)
{
for(int k=0;k+j<=n;k+=i*i)
c2[k+j] += c1[j];
}
for(int k=0;k<=n;k++)
{
c1[k] = c2[k];
c2[k] = 0;
}
}
printf("%d\n",c1[n]);
}
}
动态规划:HDU-1398-Square Coins(母函数模板)的更多相关文章
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1398 Square Coins 整数拆分变形 母函数
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- HDU 1398 Square Coins(母函数或dp)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1398 Square Coins 分钱币问题
Square Coins Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDOJ 1398 Square Coins 母函数
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1398 Square Coins(生成函数,完全背包)
pid=1398">链接:hdu 1398 题意:有17种货币,面额分别为i*i(1<=i<=17),都为无限张. 给定一个值n(n<=300),求用上述货币能使价值 ...
- hdu 1398 Square Coins(简单dp)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Pro ...
- HDU 1398 Square Coins
题目大意:有面值分别为.1,4,9,.......17^2的硬币无数多个.问你组成面值为n的钱的方法数. 最简单的母函数模板题: #include <cstdio> #include &l ...
- 杭电ACM hdu 1398 Square Coins
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
随机推荐
- jQuery 方法 属性
Attribute:$("p").addClass(css中定义的样式类型); 给某个元素添加样式$("img").attr({src:"test.j ...
- C#数据库(MSSQL)帮助类
/// <summary> /// 数据库帮助类 /// <author>Devin</author> /// </summary> public se ...
- swift学习笔记7
不管做什么事,只要敬业点,把该作的做好.不要总找借口. 不要看不起小事,生活本是一件件小事的集合.细节决定成败. 士兵突击里面有句台词:他每做一件小事的时候,都好像抓住了一根救命稻草,到最后你才发现, ...
- 锁丶threading.local丶线程池丶生产者消费者模型
一丶锁 线程安全: 线程安全能够保证多个线程同时执行时程序依旧运行正确, 而且要保证对于共享的数据,可以由多个线程存取,但是同一时刻只能有一个线程进行存取. import threading v = ...
- This is your path and you will pursue it with excellence.
This is your path and you will pursue it with excellence.自己选的路就要走出精彩.
- 签证-L1/L2
http://blog.sina.com.cn/s/blog_7664b7f70102uweb.html 14年4月我接到公司通知,要从MICROSTRATEGY中国研发中心内部transfer到美国 ...
- cms-帖子管理
mapper: <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC & ...
- springMvc-视图模型封装及注解参数
1.视图模型封装,ModelAndView可以向页面返回视图的同时吧模型也传入页面 2.注解参数,springMvc很好的地方在于简单,高效,@RequestParam注解能非常好的取得页面参数 代码 ...
- java Vamei快速教程01
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是完全面向对象的语言.Java通过虚拟机的运行机制,实现“跨平台”的理念. ...
- convert命令
可以修改图片的分辨率 convert -resize 600×600 src.jpg dst.jpg src.jpg是你要修改的图片的名字 dst.jpg是新生成的图片名字