动态规划: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 ...
随机推荐
- 《Head First 设计模式》之适配器模式与外观模式
适配器模式(Adapter) 适配器(adapter-pattern):将一个类的接口,转换成客户期望的另一个接口.适配器让原来接口不兼容的类可以合作无间.两种形式: 对象适配器(组合) 类适配器(多 ...
- jsch连接Linux工具类
import com.alibaba.fastjson.JSONObject;import com.jcraft.jsch.*;import org.slf4j.Logger;import org.s ...
- valueOf() 和 toString()
valueOf():如果存在任意原始值,返回最适合该对象类型的原始值. toString():将该对象的原始值以字符串形式返回. 这两个方法一般是交由JS去隐式调用,以满足不同的运算情况. 举个栗子 ...
- Ajax 使用 FormData做为data的参数时 出现Illegal invocation
今天在用ajax向后台传递数据时出现此错误,在ajax的参数中加上 contentType: false, processData: false, 这两句即可.
- MATLAB之折线图、柱状图、饼图以及常用绘图技巧
MATLAB之折线图.柱状图.饼图以及常用绘图技巧 一.折线图 参考代码: %图1:各模式直接成本预测 %table0-table1为1*9的数组,记录关键数据 table0 = data_modol ...
- URL最大长度问题
在http协议中,其实并没有对url长度作出限制,往往url的最大长度和用户浏览器和Web服务器有关,不一样的浏览器,能接受的最大长度往往是不一样的,当然,不一样的Web服务器能够处理的最大长度的UR ...
- /etc/syslog.conf日志配置文件详解
//将info或更高级别的消息送到/var/log/messages,除了mail以外. //其中*是通配符,代表任何设备:none表示不对任何级别的信息进行记录. *.info;mail.none; ...
- nc扫描端口
nc -n -v -z -w 1 ip地址 1-1000 (端口号) 详细信息 -v 排除dns -n 不发送任何数据-z 超时设置为1秒 -w 1
- LeetCode Move Zeroes (简单题)
题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...
- COGS 1944. 背驮式行走
★ 输入文件:piggyback.in 输出文件:piggyback.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] Bessie和她妹妹Elsie白天都在牧场 ...