您的位置:首页 > 其它

codeforces 722E

2016-10-09 22:31 513 查看

题目大意

在一个n∗m的网格图中,你要从(1,1)走到(n,m),每次只能向右或者向下。

其中有k个障碍点,一条路径每经过1个障碍分数就会从s,变成⌈s2⌉.

你最开始的分数为s,问期望的分数,对1000000007取模。

解题思路

可知分数只有logs种。

那么我们只需求出每种>1的分数共有多少种走法即可。

我们先把障碍按x为第一关键字,y为第二关键字排序。

令fi为第i个障碍直接走到(n,m)中间不经过其他障碍的方案数。

显然fi=way(xi,yi,n,m)−∑kj=i+1way(xi,yi,xj,yj)∗fj

现在经过的障碍数不一定为0,怎么做?

我们设gi,v为i个障碍直接走到(n,m)中间经过v个其他障碍的方案数。

则gi,0=fi.

对于v>0,我们枚举中间经过的倒数第v+1个障碍是什么,容斥一下即可。

gi,v=way(xi,yi,n,m)−∑kj=i+1way(xi,yi,xj,yj)∗gj,v−∑v−1j=0gi,j

这样行了。

参考代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define maxn 100005
#define maxm 2005
#define mo 1000000007
#define ll long long
#define maxsq 22
using namespace std;

struct note{
int x,y;
}a[maxm];

int n,m,tot,s;

ll p[2*maxn],q[2*maxn];

ll g[maxm][maxsq];

ll mul(ll x,ll y){
ll ret=1;
while (y) {
if (y % 2==1) ret=ret*x % mo;
x=x*x % mo;
y /=2 ;
}
return ret;
}

bool cmp(note i,note j){
return i.x<j.x || i.x==j.x && i.y<j.y;
}

ll C(ll x,ll y){
if (x==y) return 1;
if (y==0) return 1;
if (x==0) return 0;
return p[x]*q[y] % mo * q[x-y] % mo;
}

ll way(ll x1,ll y1,ll x2,ll y2) {
if (x2<x1 || y2<y1) return 0;
return C(x2-x1+y2-y1,x2-x1);
}

int main(){
p[0]=q[0]=1;
fo(i,1,200000) p[i]=p[i-1] * i % mo;
q[200000]=mul(p[200000],mo-2);
fd(i,199999,1) q[i]=q[i+1] * (i+1) % mo;
scanf("%d%d%d%d",&n,&m,&tot,&s);
fo(i,1,tot) scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+tot+1,cmp);
a[0].x=1;
a[0].y=1;
fd(i,tot,0) {
fo(j,0,20) {
g[i][j]=way(a[i].x,a[i].y,n,m);
fo(k,i+1,tot) {
g[i][j]=((g[i][j]-g[k][j]*way(a[i].x,a[i].y,a[k].x,a[k].y)) % mo+mo) % mo;
}
fo(k,0,j-1) g[i][j]=(g[i][j]-g[i][k]+mo) % mo;
}
}
ll ans1=0,ans2=0,totw=0;
int num=0;
while (s!=1) {
ans1=(ans1+s*g[0][num]) % mo;
totw=(totw+g[0][num]) % mo;
s=s / 2+(s % 2);
num++;
}
ans2=way(1,1,n,m);
totw=(ans2-totw+mo) % mo;
ans1=(ans1+totw) % mo;
ans2=mul(ans2,mo-2);
ans1=(ans1*ans2) % mo;
printf("%lld",ans1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces