hdu 1398 Square Coins(生成函数,完全背包)
题意:有17种货币,面额分别为i*i(1<=i<=17),都为无限张。
给定一个值n(n<=300),求用上述货币能使价值总和为n的方案数
分析:这题能够用母函数的思想,对300以内的值进行预处理就可以
也可用全然背包思想求300以内的方案数
母函数:
#include<stdio.h>
int main()
{
int c1[305],c2[305],i,j,k,n;
for(i=0;i<=300;i++){
c1[i]=1;
c2[i]=0;
}
for(i=2;i<=17;i++){
for(j=0;j<=300;j++)
for(k=0;j+k<=300;k+=i*i) //这里每次累加i*i,由于第i种货币面额为i*i
c2[j+k]+=c1[j];
for(j=0;j<=300;j++){
c1[j]=c2[j];
c2[j]=0;
}
}
while(scanf("%d",&n)!=EOF){
if(n==0)
break;
printf("%d\n",c1[n]);
}
return 0;
}
全然背包
#include<stdio.h>
int main()
{
int a[20]={0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289},f[305]={0};
int i,n,j;
f[0]=1;
for(i=1;i<=17;i++)
for(j=a[i];j<=300;j++)
f[j]+=f[j-a[i]];
while(scanf("%d",&n)!=EOF){
if(n==0)
break;
printf("%d\n",f[n]);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
hdu 1398 Square Coins(生成函数,完全背包)的更多相关文章
- hdu 1398 Square Coins 分钱币问题
Square Coins Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 题解报告: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 (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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 整数拆分变形 母函数
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- 杭电ACM hdu 1398 Square Coins
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(DP)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1398 Square Coins【生成函数】
预处理出完全平方数就和普通的生成函数解整数拆分一样了 #include<iostream> #include<cstdio> using namespace std; cons ...
随机推荐
- UE4的编程C++创建一个FPSproject(两)角色网格、动画、HUD、子弹类
立即归还,本文将总结所有这些整理UE4有关角色的网络格.动画.子弹类HUD一个简单的实现. (五)角色加入网格 Character类为我们默认创建了一个SkeletaMeshComponent组件,所 ...
- VirtualBox,Kernel driver not installed (rc=-1908)
http://hi.baidu.com/spt_form/item/316d6207b47b8ee03499020a VirtualBox,Kernel driver not installed (r ...
- VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用(简单明了)
一. 在字符串前加一个L作用: 如 L"我的字符串" 表示将ANSI字符串转换成unicode的字符串,就是每个字符占用两个字节. strlen("as ...
- NumPy简明教程
源地址:http://blog.csdn.net/sunny2038/article/details/9002531 http://blog.csdn.net/sunny2038/article/de ...
- jquery clone方法
引用自http://www.w3school.com.cn/tiy/t.asp?f=jquery_manipulation_clone <html> <head> <sc ...
- java中int,float,long,double取值范围,内存泄露
java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] ...
- hdu 4963(中途相遇法)
题目链接:Dividing a String 题意:给定一个2*n(n<=20)的字符串及每个位置的字符包含的权重,求将该字符串分成两个子序列S1.T1,要求S1=T1且abs(weight1- ...
- C++环形矩阵填充实现
#include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #incl ...
- poj 1872 A Dicey Problem WA的代码,望各位指教!!!
A Dicey Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 832 Accepted: 278 Des ...
- cisco路由器IPSEC VPN配置(隧道模式)
拓扑如下: R1配置hostname R1enable password cisco crypto isakmp policy 1 #创建IKE协商策略,编号为1 encr 3des ...