多种解法:Ignatius and the Princess III
来源:互联网 发布:淘宝被投诉盗图没申诉 编辑:程序博客网 时间:2024/06/06 07:30
Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22033 Accepted Submission(s): 15385
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!"
"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
41020
Sample Output
542627“嗯,好像第一个问题太容易了,我会让你知道你以后会多么愚蠢。” feng5166说。
“第二个问题是,给定一个正整数N,我们定义一个这样的公式:
N = a [1] + a [2] + a [3] + ... + a [m];
a [i] 0,1 <= m <= N;
我的问题是给定N可以找到多少个不同的方程。
例如,假设N为4,我们可以发现:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
所以当N为4时,结果为5。注意,“4 = 3 + 1”和“4 = 1 + 3”在这个问题上也是一样的,现在,你做到了!1,利用母函数,g(x)=(1+x+x^2+x^3+…)(1+x^2+x^4+…)(1+x^3+x^6+…)….代码如下:#include<iostream>using namespace std;int sol(int n){int a[130],b[130];for(int i=0;i<=n;i++){ a[i]=1; b[i]=0; }for(int i=2;i<=n;i++){for(int j=0;j<=n;j++)for(int k=0;k*i+j<=n;k++)b[k*i+j] += a[j];for(int p=0;p<=n;p++)a[p]=b[p],b[p]=0;}return a[n];}int main(){int n;while(cin >> n){cout << sol(n) <<endl;}return 0;}2,整数划分问题是将一个正整数n拆成一组数连加并等于n的形式,且这组数中的最大加数不大于n。
如6的整数划分为
6
5 + 1
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1
共11种。下面介绍一种通过递归方法得到一个正整数的划分数。 递归函数的声明为int split(int n, int m);其中n为要划分的正整数,m是划分中的最大加数(当m > n时,最大加数为n),
当n = 1或m = 1时,split的值为1,可根据上例看出,只有一个划分1 或 1 + 1 + 1 + 1 + 1 + 1 1
可用程序表示为if(n == 1 || m == 1) return 1;
下面看一看m 和 n的关系。它们有三种关系 2
(1) m > n
在整数划分中实际上最大加数不能大于n,因此在这种情况可以等价为split(n, n);
可用程序表示为if(m > n) return split(n, n);
(2) m = n
这种情况可用递归表示为split(n, m - 1) + 1,从以上例子中可以看出,就是最大加
数为6和小于6的划分之和
用程序表示为if(m == n) return (split(n, m - 1) + 1);
(3) m < n
这是最一般的情况,在划分的大多数时都是这种情况。
从上例可以看出,设m = 4,那split(6, 4)的值是最大加数小于4划分数和整数2的划分数的和。
因此,split(n, m)可表示为split(n, m - 1) + split(n - m, m) 代码如下:#include<iostream>#include<cstdio>using namespace std;int s[130][130];int split (int n,int m){ if(s[n][m]) return s[n][m]; if(n<1||m<1) return s[n][m]=0; if(n==1||m==0) return 1; if(n<m) return s[n][m]=split(n,n); if(n==m) return s[n][m]=(split(n,m-1)+1); else return s[n][m]=(split(n,m-1)+split(n-m,m));}int main (){ int n; split(120,120); while(scanf("%d",&n)!=EOF) printf("%d\n",split(n,n)); return 0;}
阅读全文
0 0
- 多种解法:Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- Ignatius and the Princess III
- hdu1028 Ignatius and the Princess III
- 1028 Ignatius and the Princess III
- hdu 1028 Ignatius and the Princess III
- 如何在一个div中使其子div居中
- 解决Ubuntu Windows共存 8个小时时差问题
- Sublime Text 3 快捷键汇总
- 远程连接ubuntu mysql出现2003错误 cant connect to mysql
- Mysql保留字问题 出现1064(42000)错误
- 多种解法:Ignatius and the Princess III
- webpack的使用(1) ----简单快速入门
- 【备忘】传智播客黑马程序员28期JavaEE基础及就业班视频教程
- 面向对象基础学习心得
- mysql自增主键在大量删除后如何重新设置避免断层
- 用表情符号解释比特币
- 锐捷for Linux
- STM32库文件systemInit 晶振更改默认晶振8M到12M方法
- c语言笔记——scanf函数