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

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); }
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define re register
 5 using namespace std;
 6 typedef unsigned us;
 7 us min(us a,us b){return a<b?a:b;}
 8 us n,m;
 9 us C(us a,us b){
10     double cnt=1.0;
11     while(b) cnt*=(double)(a--)/(double)(b--);
12     return (us)(cnt+0.5);
13 }
14 int main(){
15     while(cin>>n>>m){
16         if(!n&&!m) return 0;
17         cout<<C(n+m,min(n,m))<<endl;
18     }
19 }
View Code
原文地址:https://www.cnblogs.com/kafuuchino/p/9882901.html