poj1942 Paths on a Grid(无mod大组合数)

时间:2024-07-09 21:03:02

poj1942 Paths on a Grid

题意:给定一个长m高n$(n,m \in unsigned 32-bit)$的矩形,问有几种走法。$n=m=0$时终止。

显然的$C(m+n,n)$

但是没有取模,n,m的范围又在unsigned int 范围内

于是有一种神奇的方法↓↓

typedef unsigned us;
us C(us a,us b){//C(a,b)
double cnt=1.0;
while(b) cnt*=(double)(a--)/(double)(b--);
return (us)(cnt+0.5);
}
 #include<iostream>
#include<cstdio>
#include<cstring>
#define re register
using namespace std;
typedef unsigned us;
us min(us a,us b){return a<b?a:b;}
us n,m;
us C(us a,us b){
double cnt=1.0;
while(b) cnt*=(double)(a--)/(double)(b--);
return (us)(cnt+0.5);
}
int main(){
while(cin>>n>>m){
if(!n&&!m) return ;
cout<<C(n+m,min(n,m))<<endl;
}
}