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

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

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

2019-11-14 12:22:37
字體:
供稿:網(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

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

解題思路 因?yàn)殚L寬分別為 a,b,暫且設(shè) a 為長,則一共有 a+b 步,其中 a 步向右走,b步向上走,有點(diǎn)像初高中做的題 ,方法共有 c[a+b][b] 種。關(guān)鍵是如何求這個(gè)組合數(shù)了。對(duì)于c[m][n],即是在求 n!/(m!(n-m)!),仔細(xì)一點(diǎn)可以發(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ù)①式,若從前往后進(jìn)行求商累乘,每一步不一定整除,所以需要用 double 類型,我的代碼就是這種;若從后往前進(jìn)行求商累乘,直接用整型就好了,因?yàn)槊恳徊角蟪鰜淼亩际且粋€(gè)組合數(shù)必為正數(shù)。 另外很坑的一點(diǎn),就是存在一邊為 0 的情況,只有 0 0 才結(jié)束輸入。好好審題啊,wa跪了一條街,跪到懷疑自己的智商:(

代碼實(shí)現(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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中方县| 甘泉县| 秦皇岛市| 昭觉县| 定襄县| 崇义县| 上蔡县| 南靖县| 安岳县| 棋牌| 南丰县| 鲁甸县| 琼中| 乌拉特中旗| 清水河县| 德钦县| 镶黄旗| 新源县| 会理县| 澄迈县| 江都市| 南开区| 绿春县| 宜兰市| 始兴县| 扎鲁特旗| 福泉市| 阿拉善左旗| 景东| 特克斯县| 通城县| 措美县| 丘北县| 新密市| 九台市| 环江| 栖霞市| 民勤县| 含山县| 东源县| 正定县|