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. 推荐10款非常优秀的 HTML5 开发工具

      HTML5 发展如火如荼,随着各大浏览器对 HTML5 技术支持的不断完善以及 HTML5 技术的不断成熟,未来 HTML5 必将改变我们创建 Web 应用程序的方式.今天这篇文章向大家推荐10款 ...

  2. 初学ExtJs 表格显示后台数据

    最近开始接触ExtJs,贴出自己的代码,一个简单的表格显示 版本 3.4.1 需要json包 代码清单1.jsp引入的ExtJs文件 <!-- 资源文件 ExtJs --> <lin ...

  3. 从QQ网站中提取的纯JS省市区三级联动

    在 http://ip.qq.com/ 的网站中有QQ自己的JS省市区三级联动 QQ是使用引用外部JS来实现三级联动的.JS如下:http://ip.qq.com/js/geo.js <!DOC ...

  4. 戴文的Linux内核专题:10配置内核(6)

    转自Linux中国 欢迎来到下一篇关于内核配置文章!还有大量的选项需要配置.这篇文章将主要讨论PCI和ACPI. 这里我们可以启用由ACPI控制的扩展坞和可移动驱动器槽的支持(Dock).记住,ACP ...

  5. 单人SVN提交bug

    The working copy "初识tableVIew" failed to commit files. fatal: Unable to create '/Users/zjj ...

  6. [vijos P1023] Victoria的舞会3

    这… 本来想学习一下Tarjan算法的,没想到码都码好了发现这题不是求强连通分量而是简单的连通分量…图论基础都还给老师了啊啊啊!最后深搜通通解决! v标记是否被访问过,scc标记每个的祖先(本来想写T ...

  7. MonoRail学习-入门实例篇

    1.到官方网站下载安装文件,地址如下: http://www.castleproject.org/index.php/Castle:Download目前最新版本Beta5(您也可以不需要下载,直接使用 ...

  8. C++二叉查找树实现及转化为双向链表

    二叉树首先要有树节点 template<class T> class BinaryNode { public: T element; BinaryNode *left; BinaryNod ...

  9. java基础之 垃圾回收机制

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  10. 教学目标的表述方式──行为目标的ABCD表述法

    教学目标应规定学生在教学活动结束后能表现出什么样的学业行为,并限定学生学习过程中知识.技能的获得和情感态度发展的层次.范围.方式及变化效果的量度.对每节课教学目标的准确表述,可以充分发挥教学目标在教学 ...