Ignatius and the Princess III
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 29 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
做这题的时候我就想到了这题和放苹果有点像,
(放苹果)
只是变成了n个盘子n个苹果,所以我继续使用这个函数。
放苹果的函数代码
int fun(int m,int n)
{
if(m==0||n==1)
return 1;
if(m <n)
return fun(m,m);
else
return fun(m,n-1)+fun(m-n,n);
}
但是当这样写出来后发现如果是输入120的话就超时了,计算量太大了。
于是改进一下,先算出来,用一个二维数组存起来。
代码
#include <bits/stdc++.h>
using namespace std;
int a[125][125];
void fun()
{
for(int i=1; i<125; i++)
for(int j=1; j<125; j++)
{
if(i==1||j==1)
a[i][j]=1;
else if(i <j)
a[i][j]=a[i][i];
else if(i==j)
a[i][j]=a[i][j-1]+1;
else
a[i][j]=a[i][j-1]+a[i-j][j];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int m;
fun();
while(cin>>m)
{
cout<<a[m][m]<<endl;
}
return 0;
}
然后这样的话就不用每输入一个数都要重新算一遍,也就节省了时间。
本文详细解析了一个名为IgnatiusandthePrincessIII的数学问题,探讨了如何寻找一个正整数N的不同方程表示方法,并提供了一种高效的算法解决方案,避免了重复计算。

251

被折叠的 条评论
为什么被折叠?



