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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

組合數(shù)學(xué) POJ 1942 Paths on a Grid

2019-11-14 12:20:46
字體:
供稿:網(wǎng)友

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

題目大意 一個長寬為 a、b 的網(wǎng)格矩形,要求從左下角抵達右上角,每次可以有右和上兩個方向,可以走任意個格子。輸出有多少種抵達的辦法。

解題思路 因為長寬分別為 a,b,暫且設(shè) a 為長,則一共有 a+b 步,其中 a 步向右走,b步向上走,有點像初高中做的題 ,方法共有 c[a+b][b] 種。關(guān)鍵是如何求這個組合數(shù)了。對于c[m][n],即是在求 n!/(m!(n-m)!),仔細一點可以發(fā)現(xiàn)分母上的 m! 可以與分子上 n! 的后 m 位約分,留下 n! 的前(n-m)位除以 (n-m)!。如 c[7][3] =7!/(3!*4!)= (7*6*5*4)/(4*3*2*1)=(7/4)(6/3)(5/2)(4/1)①,即分成兩兩相除的格式。 根據(jù)①式,若從前往后進行求商累乘,每一步不一定整除,所以需要用 double 類型,我的代碼就是這種;若從后往前進行求商累乘,直接用整型就好了,因為每一步求出來的都是一個組合數(shù)必為正數(shù)。 另外很坑的一點,就是存在一邊為 0 的情況,只有 0 0 才結(jié)束輸入。好好審題啊,wa跪了一條街,跪到懷疑自己的智商:(

代碼實現(xiàn)

#include <iostream>#include<cstdio>using namespace std;int main(){ __int64 a,b; while(~scanf("%I64d%I64d",&a,&b)) { if(a==0&&b==0) break; __int64 t,d; t=a>b?b:a; d=a+b; double mul=1.0; while(t>0) { mul*=double(d--)/double(t--); } printf("%.0f/n",mul); } return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 北辰区| 安福县| 宁都县| 金寨县| 浮山县| 潞西市| 桦川县| 延长县| 贞丰县| 江山市| 庄河市| 临沭县| 正定县| 民县| 松潘县| 神木县| 河曲县| 海宁市| 陇川县| 黄龙县| 探索| 河北区| 宜昌市| 徐水县| 承德市| 辽源市| 固原市| 轮台县| 峨眉山市| 肃宁县| 桐城市| 巨鹿县| 武宁县| 镇江市| 凤山县| 巴楚县| 临清市| 分宜县| 托克逊县| 观塘区| 宁国市|