【题解】【排列组合】【素数】【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 ...
 
随机推荐
- 修改weblogic PermGen
			
vim /weblogic/Oracle/Middleware/wlserver_10.3/common/bin/commEnv.sh 在第144行,增加环境变量:JAVA_VENDOR=Sun #根 ...
 - Python 2.7教程
			
参考:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000
 - 一个app中保持程序全屏的方法。
			
public void toggleFullscreen(boolean fullScreen) { //fullScreen为true时全屏 WindowManager.LayoutParams a ...
 - hdu  4614  Vases and Flowers
			
http://acm.hdu.edu.cn/showproblem.php?pid=4614 直接线段树维护 代码: #include<iostream> #include<cstd ...
 - MM 不哭 (tyvj 1097)
			
题目大意: 一条数轴上有 n 个 MM 在哭,需要tcboy去安慰,tcboy 一开始站在第k个MM身边,每个MM 哭都会减掉tcboy的RP. 确定安慰MM的顺序使得RP扣得最少.求 min(Rp_ ...
 - 【同行说技术】Python程序员小白变大神必读资料汇总(  三)
			
在文章<Python开发.调试.爬虫类工具大全>里面向大家总结了各种实用工具和爬虫技术,今天小编收集了5篇带有实例干货的资料,赶紧来看看吧!另外,喜欢写博客的博主可以申请加工程师博主交流群 ...
 - poj 3264 Balanced Lineup (线段树)
			
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42489 Accepted: 20000 ...
 - 学好C++必须要注意的十八个问题
			
转自 http://blog.chinaunix.net/uid-7396260-id-2056691.html 一.#include "filename.h"和#i nclud ...
 - ToolBar+DrawerLayout + NavigationView
			
http://www.jianshu.com/p/9471b87f2c61 很好的博客可以瞅瞅 <android.support.design.widget.NavigationView and ...
 - php中的include()的使用技巧
			
php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...