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

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

組合數學 POJ 1942 Paths on a Grid

2019-11-14 11:50:48
字體:
來源:轉載
供稿:網友

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 的網格矩形,要求從左下角抵達右上角,每次可以有右和上兩個方向,可以走任意個格子。輸出有多少種抵達的辦法。

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

代碼實現

#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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 重庆市| 凌海市| 姚安县| 锡林浩特市| 景宁| 贵州省| 盐津县| 澳门| 石台县| 高阳县| 呼伦贝尔市| 钦州市| 巴中市| 新绛县| 德格县| 读书| 勐海县| 廉江市| 青海省| 仁怀市| 翁源县| 西乌| 海原县| 陆丰市| 江川县| 巴里| 南开区| 鄂托克旗| 顺昌县| 图片| 确山县| 慈溪市| 本溪| 义乌市| 礼泉县| 湖北省| 永宁县| 河西区| 连州市| 青岛市| 丰城市|