Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess 定理Lucas求大組合數,
E. Gerald and Giant Chesstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on anh × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?
The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.
Input
The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).
Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell.
It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.
Output
Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo109 + 7.
Sample test(s)input
3 4 22 22 3
output
2
input
100 100 315 1616 1599 88
output
545732279
題意,給一個n * m 的網格,其只有一些點是壞的不能走,要求從0,0點起到n,m的總方案數。
設dp[i] 表示到達第i個壞點且不經過其他壞點的總方案數。增加一個點(n-1,m-1),則dp[k]即為答案。
壯態轉移方程 dp[i] = C[x[i]+y[i],x[i]) - sum(dp[j] * C(x[i] - x[j] + y[i] - y[j],x[i] - x[j]),j表示,所有在i之前的壞點,由於這裡的dp[i]都是不經過別的壞點,不會有重複的路徑,所以減去以前的壞點到達i的路徑就是答案。
先排序,就可以得到每個點的先後順序,這樣在更新狀態的時候,可以從小到大枚舉。總的複雜度為 o(k * k);
現在剩下的問題 就是要求c(n,m),由於n,m很大,一般求組合數的方法是n * m,這樣複雜度太高,即使用遞推o(n)的複雜度,也是太高的。這裡引入了 定理Lucas,這個就是專門求大組合數的一種方法。
Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) 設 aa = n % p,bb = m % p;這裡可以看出來把n,m很大的數轉化了相對比較小的aa,bb,
這個公式把n - n/p m - m/p,這樣可以遞迴解決。求c(aa,bb)就可以直接用組合公式aa!/(bb! * (aa - bb)! ),由於這裡有除法,所以要求bb! * (aa - bb)!的逆。由歐拉公式p為素數a的逆為pow_mod(a,p-2,p);即a^(p-2) % p;
所以整個公式轉化成ret=(ret*fac[a]*pow_mod(fac[b]*fac[a-b]%p,p-2,p))%p; 這樣就可以遞迴求解了。
總的說一下,雖然這題,由於n,m很小隻有10^5,所以不用lucas定理也可以解決,但有可能會出現一種情況n,m很大,達到10^9,而MoD很小,只有10^5,這樣,這個lucas定理,就可以有真正的用場了,而且要求MOD是質數才可以的,因為是質數,那個歐拉定理才適用的。
#define N 2050#define M 100005#define maxn 205#define MOD 1000000007int n,m,k,r,c;ll a[N],ta[M + M];pii p[N];ll pow_mod(ll a,ll n,ll p){ ll res = 1; while(n){ if(n & 1){ res = (res * a)%p; } n = n>>1; a = ( a * a ) % p; } return res;}ll Lucas(ll a,ll b,ll p){ ll res = 1; while(a && b){ ll aa = a % p,bb = b % p; if(aa < bb) return 0; res = (((res * ta[aa]) % p ) * pow_mod(ta[bb] * ta[aa - bb] % p,p - 2,p)) % p; a = a/p;b = b/p; } return res;}void init(){ ta[0] = 1; for(int i = 1;i<M + M;i++){ ta[i] = (ta[i-1] * i) % MOD; }}int main(){ init(); while(S2(n,m)!=EOF) { S(k); FI(k){ S2(r,c);r--,c--; p[i].first = r,p[i].second = c; } p[k].first = n-1;p[k].second = m-1;k++; sort(p,p+k); FI(k){ a[i] = Lucas(p[i].first + p[i].second,p[i].second,MOD); FJ(i){ if(p[j].second <= p[i].second){ a[i] -= ( a[j] * Lucas(p[i].first - p[j].first + p[i].second - p[j].second,p[i].first - p[j].first,MOD)) % MOD; a[i] = (a[i] + MOD)%MOD; } } } cout<<a[k-1]<<endl; } return 0;}
第二種方法
這裡不用lucas定理,直接用預先處理,和歐拉公式求逆元的方法,也是可以的,
#define N 2050#define M 100005#define maxn 205#define MOD 1000000007int n,m,k,r,c;ll a[N],ta[M + M];pii p[N];ll pow_mod(ll a,ll n,ll p){ ll res = 1; while(n){ if(n & 1){ res = (res * a)%p; } n = n>>1; a = ( a * a ) % p; } return res;}ll Lucas(ll a,ll b,ll p){ if(a < b) return 0; return ta[a]* pow_mod(ta[b] * ta[a - b] % p,p - 2,p) % p;}void init(){ ta[0] = 1; for(int i = 1;i<M + M;i++){ ta[i] = (ta[i-1] * i) % MOD; }}int main(){ init(); while(S2(n,m)!=EOF) { S(k); FI(k){ S2(r,c);r--,c--; p[i].first = r,p[i].second = c; } p[k].first = n-1;p[k].second = m-1;k++; sort(p,p+k); FI(k){ a[i] = Lucas(p[i].first + p[i].second,p[i].second,MOD); FJ(i){ if(p[j].second <= p[i].second){ a[i] -= ( a[j] * Lucas(p[i].first - p[j].first + p[i].second - p[j].second,p[i].first - p[j].first,MOD)) % MOD; a[i] = (a[i] + MOD)%MOD; } } } cout<<a[k-1]<<endl; } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。