Project Euler 15 Lattice paths
题意:在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的更多相关文章
- Project Euler 453 Lattice Quadrilaterals 困难的计数问题
这是一道很综合的计数问题,对于思维的全面性,解法的过渡性,代码能力,细节处理,计数问题中的各种算法,像gcd.容斥.类欧几里德算法都有考察.在省选模拟赛中做到了这题,然而数据范围是n,m小于等于100 ...
- Project Euler Problem 15-Lattice paths
组合数,2n中选n个.向右走有n步,向下走有n步.共2n步.有n步是向右走的,计算向右走的这n步的所有情况,即C(2n,n). 或者,每一步,只能从右边或者上边走过来,只有这两种情况,即step[i] ...
- Python练习题 043:Project Euler 015:方格路径
本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice p ...
- (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 ...
- 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 ...
- Project Euler 第一题效率分析
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 044:Project Euler 016:乘方结果各个数值之和
本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
随机推荐
- POJ 2375
BFS+强连通.输出max(缩点后出度为0的点数,缩点后入度为0的点数). #include <cstdio> #include <iostream> #include < ...
- [React] Refactor componentWillReceiveProps() to getDerivedStateFromProps() in React 16.3
The componentWillReceiveProps() method is being deprecated in future version of React (17). Many of ...
- UML的基本图(二)
Both sequence diagrams and communication diagrams are kinds of interaction diagrams. An interacti ...
- JavaScript 和Ajax跨域问题
json格式: { "message":"获取成功", "state":"1", "result": ...
- Android更新带进度条的通知栏
在网上查询了下.Android版本号更新通知栏带进度条,醉了,基本都是复制过来.有的代码不全,连源代码下载都没有.有下载也须要积分,还不能用,真黑心啊!!之前自己也写过自己定义通知栏Notificat ...
- java.util.ComparableTimSort中的sort()方法简单分析
TimSort算法是一种起源于归并排序和插入排序的混合排序算法,设计初衷是为了在真实世界中的各种数据中能够有较好的性能. 该算法最初是由Tim Peters于2002年在Python语言中提出的. T ...
- POJ1837 Balance 背包
题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子(每个钩子有相对于中心的距离,左负右正),有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 将每个砝码看作一组,组内各个物品 ...
- ubantu安装jdk
环境:ubantu16.04下安装jdk1.8 1,在当前用户根目录下创建目录,本人所用的用户为bruce: mkdir /home/bruce/jdk 2,官网下载jdk1.8,网址为http:// ...
- 康少带你玩转JavaScript
目录 1.JavaScript简述 2.JavaScript引入方式 3.JavaScript语法基础 4.JavaScript数据类型 5.运算符 6.流程控制 7.函数 8.内置对象和方法 1.J ...
- 28. Implement strStr()[E]实现strStr()
题目 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if need ...