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

题干:

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 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441271.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

兔子吃萝卜的c语言编程,狼追兔子的c语言实现

满意答案16guoyuming2013.03.05采纳率&#xff1a;49% 等级&#xff1a;13已帮助&#xff1a;8005人用单链表实现#include #includetypedef struct node{int cave;struct node * next;}node,*LinkList;void main(){int i0,j,count1; // 初始值为1&#xff1b;LinkList L,p,h…

android 动态换肤框架,GitHub - ss520k/Android-Skin-Loader: 一个通过动态加载本地皮肤包进行换肤的皮肤框架...

Android-Skin-Loader更新日志导入到Android Studio&#xff0c;使用gradle构建皮肤包(见7. 皮肤包是什么&#xff1f;如何生成&#xff1f;)(2015-12-02)解决Fragment换肤在某些版本的support-v4包下失效的问题(感谢javake同学)(2015-12-02)对textColor加入selector类型的资源的…

android吸附菜单,Android仿微博、人人Feed详情页吸附导航栏

仿微博、人人的feed详情页面&#xff1a;Listview上下滑动&#xff0c;导航栏view可吸附在顶部的效果。一、实现效果上图&#xff1a;效果图.gif欢迎拍砖&#xff0c;拍拍更进步。没有对比&#xff0c;怎么会有伤害&#xff0c;下面是 微博、人人的Feed详情页&#xff1a;微博、…

android 居右属性,使用layoutDirection属性设置布局靠左或靠右

通过设置layoutDirection属性值为mx.core.LayoutDirection.RTL(右到左)或mx.core.LayoutDirection.LTR(左到右)&#xff0c;使布局为靠左或靠右(如下图)。该属性可设置3种值&#xff0c;LayoutDirection.RTL、LayoutDirection.LTR和null(ILayoutDirectionElement时)/undefined(…

html表格全屏显示,tableView滑动全屏显示

今天要分享的一个效果是在一个页面弹出视图展示一个tableview&#xff0c;然后手指滑动tableview时&#xff0c;视图随着tableview偏移量增加而慢慢增加&#xff0c;到达临界时&#xff0c;全屏显示&#xff0c;然后再次向下滑动时&#xff0c;当偏移量到达临界点&#xff0c;视…

大量html乱码seo,HTTPS改造之后网页错位乱码,影响SEO和正常访问,应该这样改

有一些朋友可能不太知道https改造怎么做&#xff0c;就学着网站的步骤进行&#xff0c;实际操作过程中可能会遇到很多问题。比如说有的会出现网页错位、页面乱码、后台功能无法使用的情况。昨天我们就有一个客户他自己做了https改造&#xff0c;但是造成后台无法上传图片的情况…

微型计算机中最小的单位,微型计算机中最小的数据单位是

微型计算机中最小的数据单位是比特。微型计算机&#xff0c;是指由微处理器作为CPU的计算机。由大规模集成电路组成的、体积较小的电子计算机。由微处理机(核心)、存储片、输入和输出片、系统总线等组成。特点是体积小、灵活性大、价格便宜、使用方便。这类计算机的普遍特征就是…

琴生不等式一般形式_001.二次函数、方程和不等式知识点

学法指导&#xff1a;本专题讲授不等式内容&#xff0c;这部分内容是学生的难点&#xff0c;为此有几点说明&#xff1a;1.把握好学习的难度。按教材内不等式部分展现的内容看&#xff0c;它很简单&#xff0c;但学过的知道&#xff0c;这部分内容很难&#xff0c;直白的讲&…

伺服电机停的时候会冲一下_造成伺服电机抖动的原因竟然是它!内附解决方法...

伺服电机(servo motor )是指在伺服系统中控制机械元件运转的发动机&#xff0c;是一种补助马达间接变速装置。它可使控制速度&#xff0c;位置精度非常准确&#xff0c;可以将电压信号转化为转矩和转速以驱动控制对象。伺服电机转子转速受输入信号控制&#xff0c;并能快速反应…

【 HDU - 5363】Key Set(水题,快速幂,组合数学)

题干&#xff1a; soda has a set SS with nn integers {1,2,…,n}{1,2,…,n}. A set is called key set if the sum of integers in the set is an even number. He wants to know how many nonempty subsets of SS are key set. Input There are multiple test cases. The…

ajax 更新模型数据_DuangDuangDuang,重点来啦!高薪全靠它——百战Web前端课程更新03.11...

百战程序员九大专业运营&#xff0c;周周有课程更新&#xff0c;保持行业领先。本次更新课程Web前端第三十阶段经典面试题解析章节1—5及课程资料。本次更新可谓是诚意满满&#xff0c;针对市场面试需要&#xff0c;总结经典面试题集&#xff0c;为你揭开企业技术要求的神秘面纱…

combox 增加请选择_娱乐测试:选择四种花束中的一种,测试你对婚姻的看法

阅读本文前&#xff0c;请您先点击上面的“落落天使”&#xff0c;再点击“关注”&#xff0c;这样您就可以继续免费收到文章了。每天都有分享&#xff0c;完全是免费订阅&#xff0c;请放心关注。 …

计算机突然断电恢复供电后,电脑突然断电的坏处有哪些?

对于经常使用计算机进行办公的用户而言&#xff0c;最可怕的事情是计算机在保存文件之前突然断电关机。但是&#xff0c;这对于计算机本身根本不是问题&#xff0c;只要计算机能够正常运行。但是&#xff0c;如果断电导致以下任何一种情况&#xff0c;则需要小心&#xff01;1、…

手游 自建服务器,英灵神殿自己搭建服务器怎么弄

英灵神殿是一款冒险生存类游戏&#xff0c;在英灵神殿游戏中小伙伴要自己搭建服务器才能玩&#xff0c;那么要怎么搭建&#xff0c;有什么技巧吗?接下来和小编一起来看看吧!Valheim英灵神殿服务器搭建技巧第一步&#xff1a;SteamCMD和安装内容从这里下载SteamCMD。将其提取到…

【POJ - 1463】Strategic game (树上最小点覆盖,树形dp)

题干&#xff1a; Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree…

恐怖黎明稳定服务器,恐怖黎明新人联机图文教程 怎么联机-游侠网

恐怖黎明怎么联机?不少玩家想体验联机&#xff0c;但是不知道方法&#xff0c;小编这里给大家带来了新人联机图文教程&#xff0c;不会的萌新来学习下吧。联机图文教程:A1 联机&#xff1a;因为gd(grimdawn)没有自己的战网平台(就是专供联机玩的专职服务器 server)&#xff0c…

github 上传代码_leetcode爬虫:爬取代码;生成readme;上传github

Leetcode-Helper哪个程序员 不想一键下写过的代码&#xff0c;自动上传Github&#xff0c;并且还能生成好看的README呢&#xff1f;有用的话点个⭐吧&#xff0c;谢谢你。Leetcode-Helper传送门​github.com主要功能 模拟登陆力扣中国站(leetcode-cn)爬取每题提交的ac代码&…

绝地求生信号枪只能在服务器吗,绝地求生信号枪怎么用?信号枪刷新点及用法详解...

绝地求生信号枪怎么用&#xff1f;信号枪刷新点及用法详解2018-03-15 15:22:12来源&#xff1a;吃鸡小助手编辑&#xff1a;野狐禅评论(0)绝地求生近日更新中悄悄加入了信号枪&#xff0c;引得广大玩家热情满满的在游戏中寻找&#xff0c;信号枪到底怎么用呢&#xff1f;下面就…

文件服务器共享文件夹访问权限,5对文件服务器中的共享文件夹进行访问权限控制...

对文件服务器中的共享文件夹进行访问权限控制1. 实训目的在Windows Server 2003环境下设置文件服务器的目的是要对多用户进行资源共享&#xff0c;这其中经常遇到不同用户应该分配不同权限的问题&#xff0c;通过这个实训希望读者了解Windows Server 2003中访问权限设置方法和具…

渲染服务器位置,如何用服务器做渲染

如何用服务器做渲染 内容精选换一换&#xfffd;&#xfffd;&#xfffd;&#xfffd;BoostKit ARMԭ&#xfffd;&#xfffd;ʹ&#xfffd;&#xfffd;&#xfffd;׼&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;嵥&#xfffd;&#xfffd…