Project Euler 76:Counting summations
原题:
It is possible to write five as a sum in exactly six different ways:
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
How many different ways can one hundred be written as a sum of at least two positive integers?
翻译:
加和计数
将5写成整数的和有6种不同的方式:
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
将100写成整数的和有多少种不同的方式?
解题思路:
将100能够拆成整数的和能够有多少种?
1.利用动态规划求解
2.利用数的拆分求解<组合数学中有讲解>
数的拆分法
理论基础,链接中讲到了拆分的理论
P(n,k)的意思就是n拆分成k份的数量
有递推公式:
P(n,k)= P(n-1,k-1) + P(n-k,k)
初始值:
k>n时:P(n,k) = 0
对所有的n,P(n,n)=1,P(n,0)=0
也很显然的发现:
对所有的n,P(n,1)=n,P(n,2) = n/2 的向下取整
证明:P(n,k)= P(n-1,k-1) + P(n-k,k)
忘了。。。
求出所以的P(n,k)矩阵
则n的所以拆分的和是
Java程序:
// 整数n 拆分k份
void partitions(){
int limit = 100;
int count = 0;
int[][] p = new int[limit+1][limit+1];
for(int n=0;n<=limit;n++){
p[n][n] = 1;
p[n][1] = 1;
p[n][0] = 0;
}
for(int n=1;n<=limit;n++){
for(int k=1;k<=n;k++){
p[n][k] = p[n-1][k-1] + p[n-k][k];
if(n==limit)
count+=p[n][k];
}
}
count = count - 1;
System.out.println(count);
}
结果:
// 190569291
// running time=0s0ms
利用递归程序:
void getPar(){
int limit = 100;
int count = 0;
for(int k=1;k<=limit;k++)
count+=Par(100,k);
count = count-1;
System.out.println(count);
} //递归形式 整数n 拆分k分
int Par(int n,int k){
if(k==1||n==k) return 1;
if(k>n) return 0;
return Par(n-1,k-1)+Par(n-k,k);
}
结果:
// 190569291
// running time=1s869ms
递归时间明显长了许多
动态规划求解
//动态规划求解
void dp(){
int limit = 100;
int[] ways = new int[limit+1];
ways[0] = 1 ;
for(int i=1;i<=limit-1;i++){
for(int j = i;j<=limit;j++)
ways[j] += ways[j-i];
}
System.out.println(ways[limit]);
}
// 190569291
// running time=0s0ms
还看不懂
完整java程序:
package Level3;
public class PE076{ void run(){ // dp();
// partitions();
// getPar();
int res = partitions3(100,100);
res = res - 1;
System.out.println(res);
}
int partitions3(int n,int m ){
if(n<=1) return 1;
if(m>n) return partitions3(n,n);
int sum = 0;
for(int k=1;k<=m;k++)
sum+=partitions3(n-k,k);
return sum;
}
// 190569291
// running time=10s845ms
void getPar(){
int limit = 100;
int count = 0;
for(int k=1;k<=limit;k++)
count+=Par(100,k);
count = count-1;
System.out.println(count);
}
// 190569291
// running time=1s869ms
//递归形式 整数n 拆分k分
int Par(int n,int k){
if(k==1||n==k) return 1;
if(k>n) return 0;
return Par(n-1,k-1)+Par(n-k,k);
}
// 整数n 拆分k份
void partitions(){
int limit = 100;
int count = 0;
int[][] p = new int[limit+1][limit+1];
for(int n=0;n<=limit;n++){
p[n][n] = 1;
p[n][1] = 1;
p[n][0] = 0;
}
for(int n=1;n<=limit;n++){
for(int k=1;k<=n;k++){
p[n][k] = p[n-1][k-1] + p[n-k][k];
if(n==limit)
count+=p[n][k];
}
}
count = count - 1;
System.out.println(count);
// for(int n=1;n<=6;n++){
// for(int k=1;k<=n;k++)
// System.out.print(p[n][k]+" ");
// System.out.println();
// } // for(int k=0;k<=limit;k++)
// count+= p[limit][k];
// count = count - 1;
// System.out.println(count); }
// 190569291
// running time=0s0ms
//动态规划求解
void dp(){
int limit = 100;
int[] ways = new int[limit+1];
ways[0] = 1 ;
for(int i=1;i<=limit-1;i++){
for(int j = i;j<=limit;j++)
ways[j] += ways[j-i];
}
System.out.println(ways[limit]);
}
// 190569291
// running time=0s0ms public static void main(String[] args){
long t0 = System.currentTimeMillis();
new PE076().run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms"); }
}
Python实现
动态规划的效率很高
递归的已经不能忍了
import time def PE076():
res = dp();
# res = partitions(100,100)
print res
def partitions(n,m):
# limit = 100
count = 0
if n<=1 : return 1
if m>n : return partitions(n,n)
for k in range(1,m+1):
count = count + partitions(n-k,k)
return count
#
# running time=1073.04099989s
def dp():
limit = 100
ways = [0]*(limit+1)
ways[0] = 1
for n in range(1,limit):
for k in range(n,limit+1):
ways[k] +=ways[k-n]
return ways[limit]
if __name__=='__main__':
t0 = time.time()
PE076()
print "running time={0}s".format((time.time()-t0))
Project Euler 76:Counting summations的更多相关文章
- Project Euler 77:Prime summations
原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...
- Project Euler 85 :Counting rectangles 数长方形
Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
- Python练习题 044:Project Euler 016:乘方结果各个数值之和
本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...
随机推荐
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/9401981 ADB server didn't ACK 这个问题会困恼很多的新手朋友, ...
- 11g RAC r2 的启停命令概述1
目标: 熟悉主要进程的启停顺序 了解独占模式 -excl crsctl start crs与crsctl start cluster 区别 1.熟悉主要进程的启停顺序 1.1 启动节点rac1: [r ...
- 关于C语言中的typedef
在C语言中定义一个结构体,要最好使用typedef,使用typedef,实际上就是为我们的结构体起了一个新的名字,即定义了一个新的类型,在后面书写自己代码的时候,就可以直接使用自己定义的新的类型第一变 ...
- Python 初学——V_Rename(第一个完整的python程序)
我在大一的时候就对python非常感兴趣,就是一直没有时间和机会去学习下,只是了解些表面的东西,今天早上整理电脑的时候发现文件夹里面的文件名是这样子的,有点小不舒服,特别想去除重复的"Str ...
- 【WPF学习日记——[DevExpress]】GridControl 行中使用按钮
想到的办法都试了,只有这个能用,不一定是最好的,但却是自己能想到的,记录一下. <dxg:GridColumn Header="操作" Width="134&quo ...
- The difference between Union & Union All in SQL Server/pOSTGRESQL
Following is test in SQL Server: USE [TestDB] CREATE TABLE [dbo].[UserInfoTest02]( [number] [bigint] ...
- matlab实现高斯消去法、LU分解
朴素高斯消去法: function x = GauElim(n, A, b) if nargin < 2 for i = 1 : 1 : n for j = 1 : 1 : n A(i, j) ...
- iOS常见问题(2)
一.模拟器黑屏 解决方法: 二.打代码时,Xcode没提示 解决方法: 0. 点击Preferences 1. 进入Text Editing 2. 勾选 三.有时候可能在勾选 Autolayout的时 ...
- Oracle 异常处理
1.什么是异常 在PL/SQL中的一个警告或错误的情形都可被称为异常.包括编译时错误(PLS)和运行时错误(ORA).一个异常通常包含一个错误代码和错误文本,分别指示异常的编号和具体错误信息. 异 ...
- 初见IOS的UI之:UI控件的属性frame bounds center 和transform
这些属性,内部都是结构体:CGRect CGPoint CGFloat 背景知识:所有的控件都是view的子类,屏幕就是一个大的view:每个view都有个viewController,它是view的 ...