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.
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.
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.
3 4 2 2 2 2 3
2
100 100 3 15 16 16 15 99 88
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 1000000007 int 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 1000000007 int 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; }