【POJ - 1942 】Paths on a Grid (組合數學,求組合數的無數種方法)
題幹:
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列的矩陣,讓你從左下角走到右上角,每次只能向上或者向右走,問你有多少種方法數。
解題報告:
一道高中數學題啊,,,一共肯定走n+m步,我們挑n步向上走,剩下m步都向右走就可以了。所以其實就是求C(n+m,n)或者C(n+m,m)。他說範圍不會超Unsigned int。我們這裡用longlong去存。剛開始想著打表,,因為C(20,10)這樣的數就很大了,,肯定不會超1000吧。。。然後就RE了,,一想發現確實是這樣,因為我也可以C(100000,1),這樣的數也是不超int範圍的,,但是你打表就打不出來了。。
考慮用公式C(n,m)公式寫出來發現不能用啊,因為階乘這東西可不是鬧著玩的,,,10的階乘就300W(3e6)了。。所以我們只能提前除掉其中一部分。。
其實這題如果加個取模就好做了。。。一萬種方法可以求。。(甚至可以Lucas)但是這題不能用。
所以這題兩個解法,一個是用double暴力,最後四捨五入得到答案。
另一個是直接用longlong去約分,因為會發現有中間項可以約分並且一定可以整除。
此時發現中間項可以分母分子約掉,,所以是可以整除的不會有精度損失。
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 ;
}