codeforces 560E Lucas定理+dp
2016-08-06 22:38
489 查看
E. Gerald and Giant Chess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
Examples
input
output
input
output
题意:求从1,1到n,m的方法数,每次可以从当前的点向右边移动一格,或者向下移动一格,但是有的格子是坏的,不能经过这些点。
分析:考虑dp来做,把坏的点排序,dp[i]表示到第i个坏的点但是不经过其他坏点有多少种方法,最后添加一个坏点n,m正好最后求dp【k】就是答案,统计dp[i]的过程中先算出从1,1到这点的组合数,然后减去x跟y都要比当前的点小的坏点到这个点的组合数,由于组合数中n,m都比较大,因此要用lucas定理来求。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
Examples
input
3 4 2 2 2 2 3
output
2
input
100 100 3 15 16 16 15 99 88
output
545732279
题意:求从1,1到n,m的方法数,每次可以从当前的点向右边移动一格,或者向下移动一格,但是有的格子是坏的,不能经过这些点。
分析:考虑dp来做,把坏的点排序,dp[i]表示到第i个坏的点但是不经过其他坏点有多少种方法,最后添加一个坏点n,m正好最后求dp【k】就是答案,统计dp[i]的过程中先算出从1,1到这点的组合数,然后减去x跟y都要比当前的点小的坏点到这个点的组合数,由于组合数中n,m都比较大,因此要用lucas定理来求。
#include<bitset> #include<map> #include<vector> #include<cstdio> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<stack> #include<queue> #include<set> #define inf 0x3f3f3f3f #define mem(a,x) memset(a,x,sizeof(a)) #define F first #define S second using namespace std; typedef long long ll; typedef pair<int,int> pii; inline int in() { int res=0;char c;int f=1; while((c=getchar())<'0' || c>'9')if(c=='-')f=-1; while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res*f; } const int N=100010,MOD=1e9+7; ll dp[2345],fac[N<<1]; ll power(ll a,ll n,ll p) { ll ret=1; while(n) { if(n & 1) ret = ret*a%p; a = a*a%p; n >>= 1; } return ret; } ll C(ll n,ll m, ll p) { if(m>n) return 0; return fac *power(fac[m]*fac[n-m]%p,p-2,p)%p; } ll Lucas(ll n,ll m,ll p) { if(m==0) return 1; return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p; } pii a[2345]; int main() { fac[0]=1; for(int i=1;i<N<<1;i++){ ////// fac[i] = fac[i-1]*i%MOD; } int n=in(),m=in(),k=in(); for(int i=0;i<k;i++){ a[i].F=in(),a[i].S=in(); } a[k].F=n,a[k].S=m; sort(a,a+k); for(int i=0;i<=k;i++){ dp[i] = Lucas(a[i].F-1+a[i].S-1,a[i].F-1,MOD); for(int j=0;j<i;j++){ if(a[j].S > a[i].S) continue; dp[i] = (dp[i]-Lucas(a[i].F-a[j].F+a[i].S-a[j].S,a[i].F-a[j].F,MOD)*dp[j]+MOD)%MOD; } } dp[k] += MOD; dp[k] %= MOD; cout<<dp[k]<<endl; return 0; }
相关文章推荐
- codeforces 560 E. Gerald and Giant Chess (dp+lucas定理,求大组合数 mod p,p为质数)
- [HDU3944]DP? (组合数学Lucas定理)
- DP? HDU - 3944 Lucas定理+费马小定理
- BZOJ 3782: 上学路线 [Lucas定理 DP]
- [Codeforces 451E] Devu and Flowers (母函数+lucas定理)
- HDU 3944-DP?(Lucas定理+预处理)
- DP? HDU - 3944 Lucas定理+费马小定理
- Codeforces 451E Devu and Flowers 容斥原理暴力+Lucas定理
- [51NOD1119]机器人走方格 V2(dp,Lucas定理)
- [Lucas定理 数位DP 容斥原理] 2015 计蒜之道 复赛 360的产品试用体验
- BZOJ 2111: [ZJOI2010]Perm 排列计数|组合数学|Lucas定理|DP
- Codeforces Gym 100379J Move the (p, q)-knight to the corner! 组合数学DP, Lucas定理
- CodeForces-451E:Devu and Flowers (母函数+组合数+Lucas定理)
- DP? HDU - 3944 Lucas定理+费马小定理
- HDU 3944 DP? [Lucas定理 诡异的预处理]
- 多校第11场 HDU 3944 DP (lucas定理,大组合数取模)
- hdu 3944 DP? ——Lucas定理
- hdu5794A Simple Chess(lucas定理+dp)
- Codeforces 560E Gerald and Giant Chess 组合数学+DP
- HDU DP?(Lucas + 帕森斯定理)