国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1942-Paths on a Grid(組合數學-C(m+n,m))

2019-11-08 02:48:12
字體:
來源:轉載
供稿:網友
Paths on a Grid
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 25425 Accepted: 6347

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 41 10 0

Sample Output

1262

Source

Ulm Local 2002

題目意思:

給出m行n列的矩陣,含m*n個方格,每次可以向上或向右走一步,計算從矩陣左下方走到右上方共有多少種不同的方法。

解題思路:

組合數學,向上有m步可走,向右有n步可走,從矩陣左下方走到右上方共要走m+n步,選出m步,則剩下的n步一定是固定的(反之亦然),所以步數是C(m+n,max(m,n))。
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>#include<vector>#include<queue>using namespace std;#define INF 0x3f3f3f3f#define MAXN 5050unsigned long long c(unsigned long long x,unsigned long long y)//計算C(x,y){    unsigned long long s=1,i,j;    for (i=y+1, j=1; i<=x; i++,j++)        s=s*i/j;    return s;}int main(){#ifdef ONLINE_JUDGE#else    freopen("F:/cb/read.txt","r",stdin);    //freopen("F:/cb/out.txt","w",stdout);#endif    ios::sync_with_stdio(false);    cin.tie(0);    unsigned long long n,m,x;    while(cin>>n>>m)    {        if(n==0&&m==0) break;        x=max(m,n);        cout<<c(n+m,x)<<endl;    }    return 0;}/*5 41 10 0*/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涟水县| 东乡族自治县| 安福县| 成武县| 清镇市| 兴和县| 永年县| 紫云| 濮阳县| 德钦县| 三河市| 大石桥市| 中山市| 阳谷县| 三原县| 宜兰市| 吐鲁番市| 油尖旺区| 镇坪县| 如皋市| 扎兰屯市| 米林县| 汝阳县| 西贡区| 安溪县| 栾川县| 洛阳市| 莱州市| 黄冈市| 沙洋县| 理塘县| 札达县| 鹤庆县| 融水| 马尔康县| 铁岭市| 新巴尔虎左旗| 荥阳市| 上饶市| 邳州市| 东阿县|