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. 分解实数(求素数指数)

详见:组合数的素数算法(ACM基础教程1010)

可以筛出

2~n

范围里的所有素数。

1)

将所有候选数

2~n

放入筛中

;

2)

找出筛中最小数

P

P

一定为素数。

3)

宣布

P

为素数,并将

P

的所有倍数从筛中筛去

;

4)

重复

2)

3)

直到筛空

.

其实,当

P>sqrt(n)

时筛中剩下的数就已经都是素数了。

【题解】【排列组合】【素数】【Leetcode】Unique Paths的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [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 ...

  4. 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  ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [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 ...

  8. LeetCode—Unique Paths

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  9. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  10. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. netty4 Handler的执行顺序

    转载:https://my.oschina.net/jamaly/blog/272385 Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通 ...

  2. fr

    8.3 credit sales(bad debt , ar) method1:direct write off method2:allowance method for bad debt allow ...

  3. jQuery插件之ajaxFileUpload 2

      ajaxFileUpload.js 很多同名的,因为做出来一个很容易. 我用的是这个:https://github.com/carlcarl/AjaxFileUpload 下载地址在这里:http ...

  4. display和visibility的区别

    一.display和visibility的相同与不同点 1.相同点:display和visibility都有讲元素隐藏的意思 2.不同点:display是元素隐藏,隐藏的元素不占文档流 而visibi ...

  5. 关于jquery计算页面元素数量

    这段jquery计算页面元素数量代码,能不能刷新页面直接输出数量,而不用点计算按钮 <scriptsrc="http://ajax.googleapis.com/ajax/libs/j ...

  6. NDK JNI 的关键点

    1.System.loadLibrary 的名字是在Android.mk里面设定的   LOCAL_MODULE    := httpdown,MODULE   后面跟的就是了 2.如何正确调用到关键 ...

  7. POJ 2482 扫描线(面积覆盖最大次数)

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10806   Accepted:  ...

  8. 使用WBI SAP Adapter 实现IDoc的同步处理(转)

    1. 应用背景 某汽车制造企业(以下称为厂商A)与其仓储系统提供商(以下称为厂商B)需要进行数据交换.汽车厂商A使用SAP系统作ERP管理,所有数据都要进入SAP进行处理,仓储系统提供商使用的是自有的 ...

  9. C++-指针和引用的区别

    1,不存在空引用,指针可以为空 2,引用更高效,使用前不需要测试是否为空 3,指针可以被赋给别的对象,引用则不可以更改 总之,在对象有可能什么也不指向或者指向不同的对象的时候应该使用指针.

  10. tornado介绍

    一.定义 tornado是一个异步非阻塞模型的服务器(tcp/http).web框架. 二.特性 1.高并发 原因:其一,网络事件循环部分根据操作系统选择最高效的,如Linux会是epoll: 其二, ...