Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23133 Accepted Submission(s): 16129
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
4 10 20
Sample Output
5 42627
翻译:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;例如就是把4的所有加出来的情况都列出来,看看一共有几种。
思路:
粗粗看了一眼,动态规划可以做。不过显然,用母函数是更为优雅,标准的做法。
想深刻学习下母函数的,可以看
http://blog.csdn.net/qq_36523667/article/details/78682444
用两个经典例子+一个附加例子把母函数讲的十分透彻,可以说是目前最易于理解的母函数的文章了。
本题母函数思路:
就是上面文章里的最后一题。我直接拷一下思路。
[代码思路:
传进来的是n,开n个数组即可。就好比这个和为5的这个例子
数组1:0 1 2 3 4 5
数组2:0 2 4
数组3:0 3
数组4:0 4
数组5:0 5
就解决了。题外话,难道n个数组就傻傻地开n个for循环吗,最后我看了一下。开3重循环,从最后一层开始找就很快了。这样一来时间复杂度远没有O(n^3)。]
代码:(就是弄一个栈不断地去加每一行的值,并保存下来,从最后一行开始是效率最高的)
package ACM1000_1099; // TODO: 2017/12/1 母函数+优化 import java.util.ArrayList; import java.util.List; import java.util.Map; public class IgnatiusAndThePrincessThree1028 { IgnatiusAndThePrincessThree1028(int n) { calculate(n); } int calculate(int n) { int[][] a = new int[n][n + 1]; //给n个数组赋值 for (int row = 0; row < n; row++) { int index = row + 1;//倍数 for (int col = 0; col < n + 1; col++) { int num = col * index; if (num <= n) { a[row][col] = num; } } } // for (int row = 0; row < n; row++) { // for (int col = 0; col < n + 1; col++) { // System.out.print(a[row][col] + " "); // } // System.out.println(); // } List<Integer> list = new ArrayList<>(); list.add(0); List<Integer> tempList = new ArrayList<>(); // for (int col1 = 0; col1 < n + 1; col1 ++) { // if (col1 != 0 && a[4][col1] == 0) { // break; // } else { // for (int col2 = 0; col2 < n + 1; col2 ++) { // if (col2 != 0 && a[3][col2] == 0) { // break; // } else { // int num = a[4][col1] + a[3][col2]; // if (num <= n) { // list.add(num); // } // } // } // } // } for (int row = 0; row < n; row ++) { for (int col = 0; col < n + 1; col ++) { if (col != 0 && a[row][col] == 0) { break; } else { for (int i = 0; i < list.size(); i ++) { int num = list.get(i) + a[row][col]; if (num <= n) { tempList.add(num); } } } } list.clear(); list.addAll(tempList); tempList.clear(); } int result = 0; for (int i = 0; i < list.size(); i ++) { if (list.get(i) == n) { result ++; } } System.out.println(result + ""); return 0; } public static void main(String[] args) throws Exception { IgnatiusAndThePrincessThree1028 i = new IgnatiusAndThePrincessThree1028(20); } }