1. 程式人生 > >poj1942 Paths on a Grid(無mod大組合數)

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