日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)

發(fā)布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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=a?2+2ab+b?2). 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列的矩陣,讓你從左下角走到右上角,每次只能向上或者向右走,問你有多少種方法數(shù)。

解題報告:

? 一道高中數(shù)學(xué)題啊,,,一共肯定走n+m步,我們挑n步向上走,剩下m步都向右走就可以了。所以其實就是求C(n+m,n)或者C(n+m,m)。他說范圍不會超Unsigned int。我們這里用longlong去存。剛開始想著打表,,因為C(20,10)這樣的數(shù)就很大了,,肯定不會超1000吧。。。然后就RE了,,一想發(fā)現(xiàn)確實是這樣,因為我也可以C(100000,1),這樣的數(shù)也是不超int范圍的,,但是你打表就打不出來了。。

考慮用公式C(n,m)公式寫出來發(fā)現(xiàn)不能用啊,因為階乘這東西可不是鬧著玩的,,,10的階乘就300W(3e6)了。。所以我們只能提前除掉其中一部分。。

其實這題如果加個取模就好做了。。。一萬種方法可以求。。(甚至可以Lucas)但是這題不能用。

所以這題兩個解法,一個是用double暴力,最后四舍五入得到答案。

另一個是直接用longlong去約分,因為會發(fā)現(xiàn)有中間項可以約分并且一定可以整除。

此時發(fā)現(xiàn)中間項可以分母分子約掉,,所以是可以整除的不會有精度損失。?

RE代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5;ll C[1005][1005]; int main() {C[0][0] = 1;for(int i = 1; i<=1002; i++) {C[i][0] = 1;for(int j = 1; j<=1002; j++) {C[i][j] = C[i-1][j] + C[i-1][j-1];}}int n,m;while(~scanf("%d%d",&n,&m)) {if(n+m==0) break;printf("%lld\n",C[n+m][n]);}return 0 ;}

?

TLE代碼:(感覺TLE的原因就是因為沒有去取m和n中的較小值)

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5;//ll C[2005][2005]; ll c(ll n,ll m) {ll x = n-m;ll all = x;double res = 1;for(ll i = 1; i<=all; i++) {res *= (1.0*n)/x;x--,n--;}return round(res); } int main() { // C[0][0] = 1; // for(int i = 1; i<=1000; i++) { // C[i][0] = 1; // for(int j = 1; j<=1000; j++) { // C[i][j] = C[i-1][j] + C[i-1][j-1]; // } // }ll n,m;while(~scanf("%lld%lld",&n,&m)) {if(n+m==0) break;printf("%lld\n",c(n+m,min(n,m)));}return 0 ;}

?

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5;//ll C[2005][2005]; ll c(ll n,ll m) {//c(5,2)ll cha1 = n-m;ll cha2 = m;ll cha = min(cha1,cha2);ll j = n-cha+1;ll i = 1;ll res = 1;for(;i<=cha;i++,j++) {res = res*j/i;}return res; } int main() { // C[0][0] = 1; // for(int i = 1; i<=1000; i++) { // C[i][0] = 1; // for(int j = 1; j<=1000; j++) { // C[i][j] = C[i-1][j] + C[i-1][j-1]; // } // }ll n,m;while(~scanf("%lld%lld",&n,&m)) {if(n+m==0) break;printf("%lld\n",c(n+m,min(n,m)));}return 0 ;}

?

總結(jié)

以上是生活随笔為你收集整理的【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。