【完全背包/母函数/递推】HDU1028-Ignatius and the Princess III
2016-08-14 23:43
411 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028
Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
完全背包代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=120;
int dp[maxn];
// 完全背包求方案数;
void CompletePack()
{
memset(dp,0,sizeof(dp));
dp[0]=1; // 初始化
for(int i=1;i<=maxn;i++){ // 填数字i;
for(int j=i;j<=maxn;j++){ // 容量为j的方案数更新;
dp[j]+=dp[j-i];
}
}
}
int main()
{
int n;
cin.sync_with_stdio(false);
CompletePack();
while(cin>>n){
cout<<dp
<<endl;
}
return 0;
}
母函数代码:
#include<iostream>
using namespace std;
// 母函数
const int maxn=120;
int f[maxn]={0}; // 保存对应指数的系数(方案数)
int tmp[maxn]={0}; // 临时保存分解一个因式的对应指数的系数
void faction(int N)
{
for(int i=0;i<=N;i++)
f[i]=1; // 第一个数,所构成的方案数都是1;
for(int i=2;i<=N;i++){ // 从第二个表达式开始
// 因式逐个分解;
for(int j=0;j<=N;j++) // 表达式内从第一个开始(j表示的是指数);
for(int k=0;k+j<=N;k+=i) // 表达式指数增加值;
tmp[j+k]+=f[j]; // 表示指数为j+k的系数;
// 更新指数系数;
for(int j=0;j<=N;j++){
f[j]=tmp[j];
tmp[j]=0;
}
}
}
int main()
{
int n;
cin.sync_with_stdio(false);
faction(maxn);
while(cin>>n){
cout<<f
<<endl;
}
return 0;
}
递推代码:
#include<iostream>
using namespace std;
const int maxn=121;
int f[maxn][maxn]={0}; // f[i][j]表示组合成i值且组合数中最小数为j的方案数;
void faction(int N)
{
for(int i=0;i<N;i++)
f[0][i]=1;
for(int i=1;i<N;i++){ // 所有可以组成的值;
for(int j=1;j<=i;j++){ // 所组成的值的可能存在的数字;
for(int k=j;k<=i;k++){ // 添加一个数字k,将更新方案数;
f[i][j]+=f[i-k][k]; // 组合值为i-k,切最小数为k的方案数直接加上一个k就构成了新的组合值为i的方案;
}
}
}
}
int main()
{
faction(maxn);
cin.sync_with_stdio(false);
int n;
while(cin>>n){
cout<<f
[1]<<endl;
}
return 0;
}
Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
完全背包代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=120;
int dp[maxn];
// 完全背包求方案数;
void CompletePack()
{
memset(dp,0,sizeof(dp));
dp[0]=1; // 初始化
for(int i=1;i<=maxn;i++){ // 填数字i;
for(int j=i;j<=maxn;j++){ // 容量为j的方案数更新;
dp[j]+=dp[j-i];
}
}
}
int main()
{
int n;
cin.sync_with_stdio(false);
CompletePack();
while(cin>>n){
cout<<dp
<<endl;
}
return 0;
}
母函数代码:
#include<iostream>
using namespace std;
// 母函数
const int maxn=120;
int f[maxn]={0}; // 保存对应指数的系数(方案数)
int tmp[maxn]={0}; // 临时保存分解一个因式的对应指数的系数
void faction(int N)
{
for(int i=0;i<=N;i++)
f[i]=1; // 第一个数,所构成的方案数都是1;
for(int i=2;i<=N;i++){ // 从第二个表达式开始
// 因式逐个分解;
for(int j=0;j<=N;j++) // 表达式内从第一个开始(j表示的是指数);
for(int k=0;k+j<=N;k+=i) // 表达式指数增加值;
tmp[j+k]+=f[j]; // 表示指数为j+k的系数;
// 更新指数系数;
for(int j=0;j<=N;j++){
f[j]=tmp[j];
tmp[j]=0;
}
}
}
int main()
{
int n;
cin.sync_with_stdio(false);
faction(maxn);
while(cin>>n){
cout<<f
<<endl;
}
return 0;
}
递推代码:
#include<iostream>
using namespace std;
const int maxn=121;
int f[maxn][maxn]={0}; // f[i][j]表示组合成i值且组合数中最小数为j的方案数;
void faction(int N)
{
for(int i=0;i<N;i++)
f[0][i]=1;
for(int i=1;i<N;i++){ // 所有可以组成的值;
for(int j=1;j<=i;j++){ // 所组成的值的可能存在的数字;
for(int k=j;k<=i;k++){ // 添加一个数字k,将更新方案数;
f[i][j]+=f[i-k][k]; // 组合值为i-k,切最小数为k的方案数直接加上一个k就构成了新的组合值为i的方案;
}
}
}
}
int main()
{
faction(maxn);
cin.sync_with_stdio(false);
int n;
while(cin>>n){
cout<<f
[1]<<endl;
}
return 0;
}
相关文章推荐
- test
- Javascript面向对象
- CSU1119/UVA 12510 Collecting Coins
- 103. Binary Tree Zigzag Level Order Traversal
- C和指针 第六章 指针6.2 6.3字符串中查找的两个版本
- Tornado(一) 安装及Demo
- PHP中this的绑定
- RxJava相关资料整理
- window平台下VS2013结合Qt的google breakpad异常捕捉
- 如何在Windows 7下安装Exchange 2010管理工具?
- Javascript学习笔记
- Hadoop 运行 yarn jar 单词统计问题解决
- USACO1.1Broken Necklace[环状DP作死]
- sql语句那些事-- 临时表
- 我的淘宝简单实现
- Mac 查看 80 端口被占的pid
- LeetCode--No.232--Implement Queue using Stacks
- 新环境中奋起的岁月(一)
- 浅析C语言中printf(),sprintf(),scanf(),sscanf()的用法和区别
- UVA - 1339 Ancient Cipher