本题来自 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:方格路径的更多相关文章

  1. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

  4. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  5. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  6. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  7. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  8. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  9. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

随机推荐

  1. [CSP-S2019]划分 题解

    CSP-S2 2019 D2T2 考场上读完题感觉是DP就直接扔了开T3了,考完才发现部分分好像不难拿,枯了 题意分析 给出一个数列,要求将其分成几段,使每段的和非严格递增,求最小的每段的和的平方和. ...

  2. React技术实践(1)

    随着系统越来越庞大,前端也变得越来越复杂,因此,构建一套组件化的前端变得很重要了. 之前一直在使用Asp.net来进行前端的组件化,Asp.net组件化有个很大的缺陷,就是和后台代码绑定太紧密了,不符 ...

  3. python中os.path下模块总结

    import os path =os.path.abspath("abc.text") # 返回绝对路径 print("path:",path) # path: ...

  4. 05_Python的文件操作

    1.文件操作概述 # 文件是用于数据存储的单位通常用来长期存储设置,文件中的数据是以字节为单位进行顺序存储的     1.打开文件: f = open("xxx") 或 with ...

  5. 使用grep命令查找文件中符合”.stg.“行

    某目录下有个test.txt,内容如下: www.stg.comwwstgcom 如果我这样去查找: $ grep '.stg.' test.txtwww.stg.comwwstgcom 发现第二个匹 ...

  6. [Oracle/sql]查看当前用户名下有多少表 以及查看表字段信息

    SQL> select table_name from user_tables order by table_name; TABLE_NAME ------------------------- ...

  7. elasticsearch跨集群数据迁移

    写这篇文章,主要是目前公司要把ES从2.4.1升级到最新版本7.8,不过现在是7.9了,官方的文档:https://www.elastic.co/guide/en/elasticsearch/refe ...

  8. 【系统之音】Android进程的创建及启动简述

    Android系统中的进程(这里不包括init等底层的进程)都是通过Zygote fork而来的,那这些进程的启动流程都是怎样的呢? 这里将Android进程分为两个部分: (1)系统框架进程Syst ...

  9. C、算法、操作系统杂记《malloc 0大小是什么行为》

    linux手册上的说明 If size is 0, then malloc() returns either NULL, or a unique pointer value that can late ...

  10. Linux幽灵漏洞修复

    1. 漏洞说明 1.1 漏洞原理 glibc是GNU发布的libc库,即c运行库,在glibc库中的__nss_hostname_digits_dots()函数存在一个缓冲区溢出的漏洞,这个漏洞可以经 ...