您的位置:首页 > 其它

POJ1942 Paths on a Grid (组合数学)

2016-08-04 15:56 671 查看
Paths on a Grid

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 24593 Accepted: 6092

Description

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=a2+2ab+b2). 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个数的方案。

所有的全部long long就可以了。

/**************************
*Create time: Thu Aug 04 15:14:59 2016

*Author: Mymilkbottles

*File name:
**************************/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<time.h>

#define pi(x,y) printf("%d%c",(x),(y));
#define pin(x) printf("%d\n",(x));
#define si(x) scanf("%d",&(x))
#define sii(x,y) scanf("%d%d",&(x),&(y))
#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))
#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))
#define read int TcaseN;scanf("%d",&TcaseN);for(int Tcase=1;Tcase<=TcaseN;++Tcase)
#define cls(x,y) memset((x),(y),sizeof((x)));
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)
#define min3(value_a,value_b,value_c) min(min(value_a,value_b),value_c)
#define GT(x) (x)=clock();
#define fin(x) freopen(x,"r",stdin);
#define fout(x) freopen(x,"w",stdout);

///In This You Can Define Long Integer Type
#define LONGTYPE long long
typedef LONGTYPE LL;
typedef unsigned LONGTYPE ULL;
const int maxint=((~((unsigned)(0)))>>1);
const LL maxll=((~((unsigned LONGTYPE)(0)))>>1);

const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const int maxn=1e3+5;

const LL value=(LL)(0x7fffffff)<<1;

int main() {
#ifdef tangge
clock_t tSTART,tEND,t3;
GT(tSTART);
#endif // tangge

/*Input:*/
LL n,m;
while(cin>>n>>m) {
if(n+m==0)break;
LL t=(LL)n+m;
int k=min(n,m);
LL ans=1;
for(LL i=0;i<(LL)k;++i){
ans=ans*(t-i)/(i+1);
}
printf("%lld\n",ans);
}
#ifdef tangge
GT(tEND);
printf("%.8lf\n",(tEND-tSTART)/1000.0);
#endif // tangge
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: