题干:

Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he's explaining that (a+b) 2=a 2+2ab+b 2). So you decide to waste your time with drawing modern art instead.

Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let's call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:

Really a masterpiece, isn't it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?

Input

The input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.

Output

For each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right or one unit up? You may safely assume that this number fits into a 32-bit unsigned integer.

Sample Input

5 4
1 1
0 0

Sample Output

126
2

题目大意:

一个n行m列的矩阵,让你从左下角走到右上角,每次只能向上或者向右走,问你有多少种方法数。

解题报告:

一道高中数学题啊,,,一共肯定走n+m步,我们挑n步向上走,剩下m步都向右走就可以了。所以其实就是求C(n+m,n)或者C(n+m,m)。他说范围不会超Unsigned int。我们这里用longlong去存。刚开始想着打表,,因为C(20,10)这样的数就很大了,,肯定不会超1000吧。。。然后就RE了,,一想发现确实是这样,因为我也可以C(100000,1),这样的数也是不超int范围的,,但是你打表就打不出来了。。

考虑用公式C(n,m)公式写出来发现不能用啊,因为阶乘这东西可不是闹着玩的,,,10的阶乘就300W(3e6)了。。所以我们只能提前除掉其中一部分。。

其实这题如果加个取模就好做了。。。一万种方法可以求。。(甚至可以Lucas)但是这题不能用。

所以这题两个解法,一个是用double暴力,最后四舍五入得到答案。

另一个是直接用longlong去约分,因为会发现有中间项可以约分并且一定可以整除。

此时发现中间项可以分母分子约掉,,所以是可以整除的不会有精度损失。

RE代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;ll C[1005][1005];
int main()
{C[0][0] = 1;for(int i = 1; i<=1002; i++) {C[i][0] = 1;for(int j = 1; j<=1002; j++) {C[i][j] = C[i-1][j] + C[i-1][j-1];}}int n,m;while(~scanf("%d%d",&n,&m)) {if(n+m==0) break;printf("%lld\n",C[n+m][n]);}return 0 ;}

TLE代码:(感觉TLE的原因就是因为没有去取m和n中的较小值)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;//ll C[2005][2005];
ll c(ll n,ll m) {ll x = n-m;ll all = x;double res = 1;for(ll i = 1; i<=all; i++) {res *= (1.0*n)/x;x--,n--;}return round(res);
}
int main()
{
//  C[0][0] = 1;
//  for(int i = 1; i<=1000; i++) {
//      C[i][0] = 1;
//      for(int j = 1; j<=1000; j++) {
//          C[i][j] = C[i-1][j] + C[i-1][j-1];
//      }
//  }ll n,m;while(~scanf("%lld%lld",&n,&m)) {if(n+m==0) break;printf("%lld\n",c(n+m,min(n,m)));}return 0 ;}

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;//ll C[2005][2005];
ll c(ll n,ll m) {//c(5,2)ll cha1 = n-m;ll cha2 = m;ll cha = min(cha1,cha2);ll j = n-cha+1;ll i = 1;ll res = 1;for(;i<=cha;i++,j++) {res = res*j/i;}return res;
}
int main()
{
//  C[0][0] = 1;
//  for(int i = 1; i<=1000; i++) {
//      C[i][0] = 1;
//      for(int j = 1; j<=1000; j++) {
//          C[i][j] = C[i-1][j] + C[i-1][j-1];
//      }
//  }ll n,m;while(~scanf("%lld%lld",&n,&m)) {if(n+m==0) break;printf("%lld\n",c(n+m,min(n,m)));}return 0 ;}

【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)相关推荐

  1. 求组合数(四种方法)

    文章目录 求组合数(四种方法) 递推(杨辉三角) 快速幂+乘法逆元 卢卡斯定理 高精度组合数 求组合数(四种方法) 文章首发于我的个人博客:欢迎大佬们来逛逛 递推(杨辉三角) 对于求一个数字的组合数: ...

  2. 【数论】求组合数的四种方法

    组合数的常用公式 零.纯暴力法 根据第一个公式,将的分子和分母求出,再相除即可. 适用范围:n,m较小的情况下. 时间复杂度: 第一种部分代码如下: for(int i = n; i >= n ...

  3. [组合数]求组合数的几种方法总结

    求C(n,m)%mod的方法总结 1.当n,m都非常小的时候能够利用杨辉三角直接求. C(n,m)=C(n-1,m)+C(n-1,m-1). 2.利用乘法逆元. 乘法逆元:(a/b)%mod=a*(b ...

  4. 求组合数(不同类型的组合数C++)

    求组合数有许多种不同的算法,要根据不同的数据量大小选择不同的算法 类型1 给定 n 组询问,每组询问给定两个整数 a,b,请你输出 Cba mod(109+7)的值. 输入格式 第一行包含整数 n. ...

  5. HEU 2036 Paths on a Grid

     1/**//**************************************  2Problem: HEU 2036 Paths on a Grid  3Time: 0.0020 s   ...

  6. poj——3177Redundant Paths

    poj--3177Redundant Paths      洛谷-- P2860 [USACO06JAN]冗余路径Redundant Paths Time Limit: 1000MS   Memory ...

  7. 算法刷题-数论-组合数、快速幂、逆元、递推求组合数、逆元求组合数

    文章目录 acwing885. 求组合数 I(递推:数据范围:2000) acwing875. 快速幂(a的k次方 模 b) acwing876. 快速幂求逆元 acwing886. 求组合数 II( ...

  8. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  9. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

最新文章

  1. wso2_使用WSO2 ESB进行邮件内容过滤
  2. 华东交通大学2017年ACM双基程序设计大赛题解
  3. (转)Cairngorm初学者入门教程 第四节--通过 Model Locator 控制管理 Views
  4. Mysql| Mysql函数,聚集函数的介绍与使用(Lower,Date,Mod,AVG,...)
  5. 搜索引擎关键字智能提示的一种实现
  6. Hadoop入门基础教程 Hadoop之单词计数
  7. java 调用 .net dll_c# – 如何从Java调用.NET dll
  8. list删除null
  9. 2021-秋招你准备好了吗?软件测试面试题
  10. 职场新鲜人必读:那些被“误读”的真经
  11. 网络编程遇到的一个错误?
  12. 利用Web of Science创建引文跟踪、检索词跟踪
  13. XMPP即时通讯协议使用(十)——好友关系状态
  14. 答题小程序后台使用方法
  15. 43种名车标志及来历
  16. 应用程序正常初始化(0xc0000160)失败
  17. Java常用英语汇总(面试必备)
  18. 2021年人工智能五大趋势预测
  19. 服务器抓不到mrcp协议,MRCP协议学习笔记-MRCP背景知识介绍
  20. WPS如何并排放置两张图片_动图演示如何制作XRD叠图与PDF卡线图

热门文章

  1. 790. Domino and Tromino Tiling
  2. 动态规划几种状态剪裁比较
  3. 为什么移动卡上到手机上显示无服务器,移动手机卡加密失败然后就没有服务器无聊的时候给手机卡加密因为不知? 爱问知识人...
  4. mysql编译安装后目录空_MySQL源码安装完成后修改安装路径启动问题
  5. Spring MVC访问不到静态资源
  6. matlab程序设计图像匹配,灰度,归一化算法,快速匹配。有代码好用。转载
  7. delphi 中如果不进行 closehandle 会怎么样_心理学:当你迷茫了,请坚持做三件事,你的未来会越来越好...
  8. ANTLR VS FLEXBISON
  9. wince6.0编译命令分析
  10. mysql linux 优化_mysql在linux中内核优化