Codeforces 560E Gerald and Giant Chess
2016-08-11 08:54
363 查看
题目链接:http://codeforces.com/problemset/problem/560/E
题意:在一个有n个坏点的h*w的棋盘上,起点为(1,1),终点为(h,w),每次可以使x+1或者y+1,求不同的路径数。
思路:如果从(x0,y0)到(x1,y1),没有任何坏点,有C(x1-x0+y1-y0,x1-x0)种方案,那么我们可以设f[i]为从起点,不经过任何坏点到达第i个坏点的路径数,然后把终点也加进坏点中,直接计算即可。f[i] = cal(i) - ∑f[j]*cal(j,i) ( j < i && xi>=xj && yi >= yj )。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 1000000007
const int maxn = 2009;
int h,w,m;
struct node
{
LL x,y;
LL tot;
}p[maxn];
LL fact[200009] , inv[200009] , facinv[200009];
bool cmp(node a , node b)
{
if ( a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
bool check()
{
rep(i,1,m)
if ( p[i].x == h && p[i].y == w ) return false;
return true;
}
LL C( int n , int m )
{
if(n < 0 || m < 0 || m > n)
return 0;
return fact
* facinv[m] % mod * facinv[n - m] % mod;
}
LL cal( LL x , LL y )
{
LL step = x + y - 2;
return C( step , x-1 );
}
void init()
{
fact[0] = fact[1] = inv[1] = facinv[0] = facinv[1] = 1;
for(int i = 2; i < 200000; ++i) {
fact[i] = fact[i - 1] * i % mod;
inv[i] = inv[mod % i] * (mod - mod / i) % mod;
facinv[i] = facinv[i - 1] * inv[i] % mod;
}
}
int main()
{
init();
cin>>h>>w>>m;
rep(i,1,m)
{
scanf("%I64d %I64d",&p[i].x,&p[i].y);
}
if ( !check() )
{
puts("0");
return 0;
}
sort(p+1,p+1+m,cmp);
m++;
p[m].x = h , p[m].y = w;
rep(i,1,m)
{
p[i].tot = cal( p[i].x , p[i].y );
rep(j,1,i-1)
if( p[i].x >= p[j].x && p[i].y >= p[j].y )s
{
p[i].tot = ( p[i].tot + mod - p[j].tot * cal( p[i].x - p[j].x + 1 , p[i].y - p[j].y + 1 ) % mod ) % mod;
}
}
printf("%I64d\n",p[m].tot);
return 0;
}
题意:在一个有n个坏点的h*w的棋盘上,起点为(1,1),终点为(h,w),每次可以使x+1或者y+1,求不同的路径数。
思路:如果从(x0,y0)到(x1,y1),没有任何坏点,有C(x1-x0+y1-y0,x1-x0)种方案,那么我们可以设f[i]为从起点,不经过任何坏点到达第i个坏点的路径数,然后把终点也加进坏点中,直接计算即可。f[i] = cal(i) - ∑f[j]*cal(j,i) ( j < i && xi>=xj && yi >= yj )。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 1000000007
const int maxn = 2009;
int h,w,m;
struct node
{
LL x,y;
LL tot;
}p[maxn];
LL fact[200009] , inv[200009] , facinv[200009];
bool cmp(node a , node b)
{
if ( a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
bool check()
{
rep(i,1,m)
if ( p[i].x == h && p[i].y == w ) return false;
return true;
}
LL C( int n , int m )
{
if(n < 0 || m < 0 || m > n)
return 0;
return fact
* facinv[m] % mod * facinv[n - m] % mod;
}
LL cal( LL x , LL y )
{
LL step = x + y - 2;
return C( step , x-1 );
}
void init()
{
fact[0] = fact[1] = inv[1] = facinv[0] = facinv[1] = 1;
for(int i = 2; i < 200000; ++i) {
fact[i] = fact[i - 1] * i % mod;
inv[i] = inv[mod % i] * (mod - mod / i) % mod;
facinv[i] = facinv[i - 1] * inv[i] % mod;
}
}
int main()
{
init();
cin>>h>>w>>m;
rep(i,1,m)
{
scanf("%I64d %I64d",&p[i].x,&p[i].y);
}
if ( !check() )
{
puts("0");
return 0;
}
sort(p+1,p+1+m,cmp);
m++;
p[m].x = h , p[m].y = w;
rep(i,1,m)
{
p[i].tot = cal( p[i].x , p[i].y );
rep(j,1,i-1)
if( p[i].x >= p[j].x && p[i].y >= p[j].y )s
{
p[i].tot = ( p[i].tot + mod - p[j].tot * cal( p[i].x - p[j].x + 1 , p[i].y - p[j].y + 1 ) % mod ) % mod;
}
}
printf("%I64d\n",p[m].tot);
return 0;
}
相关文章推荐
- Gerald and Giant Chess (CodeForces - 560E)(组合数)
- codeforces 560E Gerald and Giant Chess (dp + 组合数)
- Codeforces 560E Gerald and Giant Chess 组合数学+DP
- 【CodeForces】559C - Gerald and Giant Chess(dp & 逆元 & 容斥原理)
- CodeForces 540D--E - Gerald and Giant Chess(数论)
- Codeforces 560 E. Gerald and Giant Chess (dp,组合数学)
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
- codeforces 560 E. Gerald and Giant Chess (dp+lucas定理,求大组合数 mod p,p为质数)
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
- [CodeForces] Round #313 Div 2 E / Div 1 C Gerald and Giant Chess 不用容斥的递推组合数学
- Codeforces 559C Gerald and Giant Chess 组合数学 DP
- 【DP+组合数学】Codeforces559C[Gerald and Giant Chess]题解
- codeforces 559C Gerald and Giant Chess(dp+组合数学)
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP
- Codeforces Round #313 (Div. 2)E. Gerald and Giant Chess(Lucas定理+dp)
- codeforce 559 C Gerald and Giant Chess
- Codeforces Round #313 Gerald and Giant Chess (dp+组合数取模)
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
- Codeforces Round #313 (Div. 2) Problem E - Gerald and Giant Chess
- Codeforces 559C Gerald and Giant Chess(DP+乘法逆元求大组合数)