题意:在20×20方阵中从起点出发只允许向右或向下移动到达终点的路径有多少条。

思路:每次只能向右或者向下,总共 40 步,也就是 40 步中每一步都有两种选择,也就是 C (40 , 20) 。

为什么能在计算分子的时候不断约分掉分母?首先,组合数是整数,也就是说到最后分子一定能整除分母。我们使用 m 记录当前还没有被约分掉的最大的数,如果分子能够整除掉 m 就进行约分并且 m 更新为下一个等待约分的值。这样做就可以避免在计算组合数中导致的数据溢出问题!


/*************************************************************************
> File Name: euler015.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月27日 星期二 20时05分45秒
************************************************************************/ #include <stdio.h>
#include <inttypes.h> int32_t main() {
int64_t ans = 1 , m = 20;
for (int32_t i = 40 ; i > 20 ; i--) { // 在计算过程中不断约分防止数据溢出
ans *= i;
while (ans % m == 0 && m != 1) {
ans /= m;
--m;
}
}
printf("%"PRId64"\n",ans);
return 0;
}

方法二:DP

/*************************************************************************
> File Name: test.cpp
> Author:
> Mail:
> Created Time: 2018年02月03日 星期六 08时42分28秒
************************************************************************/ #include <bits/stdc++.h>
using namespace std; typedef long long ll;
ll dp[21][21]; int main() {
for (int i = 0 ; i <= 20 ; ++i) {
for (int j = 0 ; j <= 20 ; ++j) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] += dp[i - 1][j] + dp[i][j - 1];
}
}
}
printf("%lld\n", dp[20][20]);
return 0;
}

Project Euler 15 Lattice paths的更多相关文章

  1. Project Euler 453 Lattice Quadrilaterals 困难的计数问题

    这是一道很综合的计数问题,对于思维的全面性,解法的过渡性,代码能力,细节处理,计数问题中的各种算法,像gcd.容斥.类欧几里德算法都有考察.在省选模拟赛中做到了这题,然而数据范围是n,m小于等于100 ...

  2. Project Euler Problem 15-Lattice paths

    组合数,2n中选n个.向右走有n步,向下走有n步.共2n步.有n步是向右走的,计算向右走的这n步的所有情况,即C(2n,n). 或者,每一步,只能从右边或者上边走过来,只有这两种情况,即step[i] ...

  3. Python练习题 043:Project Euler 015:方格路径

    本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice p ...

  4. (Problem 15)Lattice paths

    Starting in the top left corner of a 22 grid, and only being able to move to the right and down, the ...

  5. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  6. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

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

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

  8. Python练习题 044:Project Euler 016:乘方结果各个数值之和

    本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...

  9. Python练习题 040:Project Euler 012:有超过500个因子的三角形数

    本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...

随机推荐

  1. MySQL:浅析 Impossible WHERE noticed after reading const tables

    使用 EXPLAIN 执行计划的时候,在 Extra 中偶尔会看到这样的描述: Impossible WHERE noticed after reading const tables 字面上的意思是: ...

  2. How to pass external configuration properties to storm topology?

    How to pass external configuration properties to storm topology? I want to pass some custom configur ...

  3. pl/sql developer 自带汉化选项

    pl/sql developer 自带汉化选项 版本:11.0.2 工具 -> 选项 -> 用户界面 ->外观, 第一项就是选择语言: 选择Chinese.lang,如果有的话:

  4. Java使用JAVE获取MP4播放时长

  5. Cloud Card是否能干掉App

    算下来有一年没写blog了.这一年算是潜心做一件事情,随着云OS 3.0已公布.总算能够向外界表达了我们想做个啥,非常多人也開始质疑,Cloud Card究竟是个啥?云OS 3.0算不算自主研发的OS ...

  6. Python3 pymysql连接MySQL数据库

    #!/usr/bin/python # -*- coding:utf8 -*- import pymysql #取得数据库连接对象 conn = pymysql.connect(host='127.0 ...

  7. 《ASP.NET》数据绑定——GridView

    GirdView简单介绍: 名称:网络视图. 来源:GridView 是 DataGrid的后继控件.在.net framework 2 中,尽管还存在DataGrid,可是GridView已经走上了 ...

  8. HTML5和CSS3中的交互新特性

    当文章的标题是一副用photoshop制作的图片,那么势必在搜索引擎中无法搜索到.并且因为图片的体积不算小.可能在网速慢的的时候不得不耐心的等待图片的刷新. 所以.我们来谈谈有没有一种新的方法能够避免 ...

  9. 64bit Centos6.4搭建hadoop-2.5.1

    64bit Centos6.4搭建hadoop-2.5.1 1.分布式环境搭建 採用4台安装Linux环境的机器来构建一个小规模的分布式集群. 当中有一台机器是Master节点,即名称节点,另外三台是 ...

  10. android:关于主工程和library project

    .如何将一个android工程作为库工程(library project) library project是作为jar包被其它android工程使用的,首先它也是普通的android工程.然后: )在 ...