动态规划: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 ...
随机推荐
- .net core mvc全局设置跨域访问
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ...
- <probing> 元素指定扩展Asp.Net加载程序集位置
下面的示例说明如何指定运行库应在其中搜索程序集的应用程序基子目录. <configuration> <runtime> <assemblyBinding xmln ...
- android打开文件、保存对话框、创建新文件夹对话框(转载)
转载地址:点击打开 这是一个简单的只有3个按钮的程序,3个按钮分别对应三种工作的模式(保存.打开和文件夹选择).封装的SimpleFileDialog.java的内容如下: package com.e ...
- hibernate课程 初探单表映射1-6 hibernate项目建立以及导入jar包
hibernate 项目建立 1 new ==>java project hibernate 导入jar包 1 windows==>prerence==>java ==>bui ...
- NIO(一)缓冲区
I/O的基本概念 同步和异步的概念: 所谓的同步就是在发出一个请求的时候,如果没有得到结果,就不返回.即调用者主动等待返回结果. 所谓的异步:调用之后直接返回结果,一般通过回调函数来处理这个应用. 阻 ...
- Redis分片(分区)
分区的概念 分区是分割数据到多个Redis实例的处理过程,因此每个实例只保存key的一个子集. 如果只使用一个redis实例时,其中保存了服务器中全部的缓存数据,这样会有很大风险,如果单台redis服 ...
- Java 中 Double 相关问题
在项目当中,对于double类型数据的使用比较频繁.尤其是处理金钱相关的数据,在使用Double类型的数据时,涉及到精度,显示,四舍五入等等问题. 1. 显示问题,当double 数据 小于 0.0 ...
- C语言的一小步—————— 一些小项目及解析
——-------- 仅以此献给东半球第二优秀的C语言老师,黑锤李某鸽,希望总有那么一天我们的知识可以像他的丰臀一样渊博! bug跟蚊子的相似之处: 1.不知道藏在哪里. 2.不知道有多少. 3.总是 ...
- [VC]VC实现开机自动运行程序
有时候,我们需要在计算机启动的时候就启动某些程序,不要人干预.这里,提供一种让程序开机自动运行的方法.见下面代码: BOOL CXXX::SetAutoRun(CString strPath) { C ...
- Android(java)学习笔记85:使用SQLite的基本流程