Python练习题 043:Project Euler 015:方格路径
本题来自 Project Euler 第15题:https://projecteuler.net/problem=15
'''
Project Euler: Problem 15: Lattice paths
Starting in the top left corner of a 2×2 grid,
and only being able to move to the right and down,
there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid? Answer: 137846528820
''' import itertools, time startTime = time.clock() # print(len(list(itertools.combinations([i for i in range(40)], 20)))) def factorial(x):
t = 1
for i in range(1, x+1):
t *= i
return t print(factorial(40)//`(factorial(20)*factorial(40-20))) print('Time used: %.2d' % (time.clock()-startTime))
原谅我智商低,这题我思考了一两天了也没想出来。参考网上 这篇文章 的分析,说是:
20*20的方格中,从左上角到右下角,不论怎么走,都只需要40步,其中必然有20步时横着走,20步时竖着走,你可以先全部先横着走,然后竖着走。所以这个问题变成了从40步中取出20步一共有多少种方法?用排列组合C(20上)(40下)。
个中算法俺真心不懂,不过求 C20/40 的组合值,不是有 itertools.combinations() 吗?我一试(也就是上面代码中被注释掉的那一行),结果电脑立马就死掉了——这得是多大的计算量啊~~~~~
后来查了下组合的计算公式:

代入数值的话,就是:

n! 表示计算阶乘(1 * 2 * 3 * ... * n),于是先定义个阶乘的函数,之后一引用计算就OK了,而且还死快!唉,看来以后还是得少用 itertools 里的方法了……
P.S.: 上述代入数值后的公式,因为分子分母都是乘来乘去的,40! = 20! * 21 * 22 * ... * 40,所以其实还可以把分子分母中的 20! 都约掉,这样还能减少点计算量。不过,对于计算机而言,这点优化几乎可以忽略不计,所以……就不折腾了~
那么,问题来了:为啥“这个问题变成了从40步中取出20步一共有多少种方法”呢?
Python练习题 043:Project Euler 015:方格路径的更多相关文章
- Python练习题 032:Project Euler 004:最大的回文积
本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 033:Project Euler 005:最小公倍数
本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...
- 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练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
随机推荐
- 趣味vi:Do you love me?
看到网上有很多这样的小趣味exe,自己用labview也做了一个,可能有很多bug,马马虎虎能用,大家可以发给自己滴那个人,哈哈哈.源码vi和exe文件都在链接中https://files.cnblo ...
- Java Android程序员软件开发知识:枚举的介绍,以及代码的编写教程。
Java枚举 Java 5.0引入了枚举,枚举限制变量只能是预先设定好的值.使用枚举可以减少代码中的bug. 例如,我们为果汁店设计一个程序,它将限制果汁为小杯.中杯.大杯.这就意味着它不允许顾客点除 ...
- java基础语法(一)
一.注释: 行内注释 //这是行内注释 多行注释 /* *这是多行注释 */ 文档注释 /** *这是文档注释 */ 二.标识符 标识符也就是我们所说的关键字 三.数据类型 1.基本数据类型 数据 ...
- 简单说说TCP三次握手、四次挥手机制
1.什么是TCP TCP全称Transmission Control Protocol(传输控制协议),是一种面向连接的.可靠的.基于字节流的传输层通信协议.是为了在不可靠的互联网络上提供可靠的端到端 ...
- C010:书号分解ISBN
代码: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { int prefix,groupIndentifier ...
- 在遍历ResultSet的循环中再执行SQL会发生什么(前提:同一个Statement)
如下面代码: Class.forName(DBParam.Driver).newInstance(); conn = DriverManager.getConnection(DBParam.DbUrl ...
- leetcode刷题-40组合总和2
题目 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用 ...
- Mybatis-日志
日志 目录 日志 1. 日志工厂 1. STDOUT_LOGGING标准日志输出 2. LOG4J 1. 先导入Log4J的包 2. log4j.properties 3. 配置log4j为日志的实现 ...
- Springboot中WebMvcConfigurer接口详解
Springboot 使用越来越多,企业的基本框架,到Springcloud分布式,可以说无论面试还是平常技术学习,一说到spring几乎就就代替了Java,可以说spring,springboot的 ...
- 备忘录:SQL SERVER2014 出现:“Cannot find one or more components”
目录 1. 起因 2. 解决方案 3. 备注 4. 参考 2020年9月13日 00:40:09-shanzm 1. 起因 因为卸载vs2015的时候,使用了一个VS2013/2015卸载工具Tota ...