【题解】【排列组合】【素数】【Leetcode】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:
这题很简单,一看就是组合数C(n,m)=C(n-1,m)+C(n-1,m-1)或者跟Unique Paths II中P(i,j)=P(i-1,j)+P(i,j-1)一样递归求解OK
但是,如果考虑m和n再大一些的情况应该怎么办呢,递归会tle吧,直接求解下式看看

1. long long直接计算三个阶乘分分钟hold不住溢出啊,过1,100的时候就已经Runtime Error了。
2. 为了避免直接计算n的阶乘,对公式两边取对数,于是得到:C(m+n,m) = exp(ln(C(m+n,m))) = exp(ln((m+n)!) - ln(m!) - ln(n!))
(还可以进一步消去重叠的部分),算法时间复杂度仍然是 O( m ),虽然浮点计算比整数计算要慢。
不过引入浮点数计算,最终的结果可能不一定是精确的,m和n一大估计也过不了OJ
int uniquePaths(int m, int n) {
double prod = , pm, pn;
for(int i = ; i <= m+n-; i++){
prod += log(i);
if(i == m-) pm = prod;
if(i == n-) pn = prod;
}
return static_cast<int>(exp(prod-pm-pn)+0.5);//(int)exp(prod-pm-pn)误差大,过不了所有test case
}
3. ACM界还有种素数化简+取模的方法,n! = 2^p[i] * 3^p[i] * 5^p[i]*......
这用到的是哥德巴赫猜想:
1.任何一个实数都可以写成几个素数的和(1+1=2)
2.任何一个实数都可以写成几个素数的积(3!=3x2,6!=2^4*3^2*5^1)底数都是素数
3.a*b%c=(a%c)*(b%c)
这样我们就可以将C(n,m)分解为素数相乘的模式,如:C(6,3)=(2^4*3^2*5^1)/((3x2)*(3x2));
下面涉及到两个问题:
1. 素数打表:筛选法
筛出2~n 范围里的所有素数。
1)将所有候选数2~n放入筛中;
2)找出筛中最小数P
3)宣布P为素数,并将P的所有倍数从筛中筛去;
4)重复2)至3)直到筛空.
其实,当P>sqrt(n)时筛中剩下的数就已经都是素数了。
2. 分解实数(求素数指数)
可以筛出
2~n
范围里的所有素数。
1)
将所有候选数
2~n
放入筛中
;
2)
找出筛中最小数
P
,
P
一定为素数。
3)
宣布
P
为素数,并将
P
的所有倍数从筛中筛去
;
4)
重复
2)
至
3)
直到筛空
.
其实,当
P>sqrt(n)
时筛中剩下的数就已经都是素数了。
【题解】【排列组合】【素数】【Leetcode】Unique Paths的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode: Unique Paths I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- LeetCode—Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- 使用ASP.Net WebAPI构建REST服务(六)——Self-Host
Asp.Net WebAPI生成的是一个程序集,并不是独立的进程,因此,要运行的时候必须将其承载在相应的宿主上,一般比较常见的是IIS承载.很多时候,我们为了简化部署或者功能集成,需要将其承载到独立的 ...
- linux内核启动笔记
一. 1.解压 tar xjf linux-2.6.22.6.tar.bz2 2.打补丁 patch -p1 < ../linux-2.6.22.6_jz2440.patch 3.配置 ...
- not jquery
var divs = document.querySelectorAll('div'); [].forEach.call(divs, function(div) { // do whatever di ...
- 【STL】-function object
// Generic findMax, with a function object, version #1 // Precondition, a.size() > 0 #include < ...
- Linux-Gcc生成和使用静态库和动态库详解
一.基本概念 1.1什么是库 在windows平台和linux平台下都大量存在着库. 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行. 由于windows和linux的平台不同( ...
- Hibernate4 No Session found for current thread原因
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- Spring学习笔记之Constructor-based or setter-based DI?
如果是强制依赖,那么使用构造器注入,如果是可选依赖,那么使用set方法注入.Spring鼓励构造器注入,可以确保依赖项不为null, Since you can mix constructor-bas ...
- 隐藏与显示:display/visibility/visible区别
说到标签的隐藏,你们会用到什么呢?display?visibility?还是服务器控件的visible? 显然,这三者都能起到隐藏与显示的效果,但是用途确完全不一样,请看用法与区别: <div ...
- 完美替换win8.1 x64默认雅黑为宋体方法
由于显示器不好,安装win8.1 x64系统后,系统默认的雅黑字体实在是看不习惯, 时间稍一常眼睛就发累,还是一直用的xp宋体好,于是寻思着能不能将默认雅黑换成宋体. 网上倒是搜出了许多win7的修改 ...
- 《day13--异常的进阶和包的使用》
//101-finally的使用&102-finally的使用场景 /* 需求:有一些特定的代码,无论异常是否发生,都需要执行, 因为异常会引发程序的跳转,导致有些语句执行不到,无法满足这个需 ...