杭电ACM OJ 1028 Ignatius and the Princess III 母函数+优化
来源:互联网 发布:mobi 软件 编辑:程序博客网 时间:2024/06/06 00:00
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
41020
Sample Output
542627
翻译:
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); }}
阅读全文
0 0
- 杭电ACM OJ 1028 Ignatius and the Princess III 母函数+优化
- 【杭电oj】1028 - Ignatius and the Princess III(母函数打表)
- 杭电ACM 1028 Ignatius and the Princess III(纯母函数模板)
- 杭电OJ 1028:Ignatius and the Princess III
- acm 杭电 ignatius and the princess iii 1028
- 杭电 ACM 1028 Ignatius and the Princess III
- 杭电 HDU ACM 1028 Ignatius and the Princess III
- 杭电hdu 1028 Ignatius and the Princess III母函数
- 杭电1028 Ignatius and the Princess III(母函数)
- Ignatius and the Princess III(杭电1028)(母函数)
- [ACM] hdu 1028 Ignatius and the Princess III (母函数)
- HDU ACM 1 1028 Ignatius and the Princess III(母函数)
- HDOJ 1028 Ignatius and the Princess III [母函数]
- HDU/HDOJ 1028 母函数 Ignatius and the Princess III
- HDU 1028 Ignatius and the Princess III(母函数)
- hdoj 1028 Ignatius and the Princess III(母函数)
- HDU 1028 Ignatius and the Princess III (母函数)
- HDU-1028Ignatius and the Princess III(母函数)
- 三、Tango API 之 Tangle内容
- ubuntu14.04下配置tomcat8
- react 配置stylus的方式。
- Hbase Region Load Balance on Table Level
- jQuery教程 4 事件
- 杭电ACM OJ 1028 Ignatius and the Princess III 母函数+优化
- Java设计模式-设计模式的六种原则
- OpenStack-M版(Mitaka)搭建基于(Centos7.2)+++十、Openstack对象存储服务(swift)中
- <<跟着老男孩学linux运维>>第三章总结
- LeetCode 728. Self Dividing Numbers with Java
- ubuntu设置GUI程序开机启动
- 图像语义分割简介
- GROUP BY表分组的用法
- IOS语音采集(采集数据大小对齐算法)