codeforces722E
CF722E Research Rover
Unfortunately, the formal description of the task turned out to be too long, so here is the legend.
Research rover finally reached the surface of Mars and is ready to complete its mission. Unfortunately, due to the mistake in the navigation system design, the rover is located in the wrong place.
The rover will operate on the grid consisting of n rows and m columns. We will define as (r, c) the cell located in the row r and column c. From each cell the rover is able to move to any cell that share a side with the current one.
The rover is currently located at cell (1, 1) and has to move to the cell (n, m). It will randomly follow some shortest path between these two cells. Each possible way is chosen equiprobably.
The cargo section of the rover contains the battery required to conduct the research. Initially, the battery charge is equal to s units of energy.
Some of the cells contain anomaly. Each time the rover gets to the cell with anomaly, the battery looses half of its charge rounded down. Formally, if the charge was equal to x before the rover gets to the cell with anomaly, the charge will change to .
While the rover picks a random shortest path to proceed, compute the expected value of the battery charge after it reaches cell (n, m). If the cells (1, 1) and (n, m)contain anomaly, they also affect the charge of the battery.
Input
The first line of the input contains four integers n, m, k and s (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 2000, 1 ≤ s ≤ 1 000 000) — the number of rows and columns of the field, the number of cells with anomaly and the initial charge of the battery respectively.
The follow k lines containing two integers ri and ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m) — coordinates of the cells, containing anomaly. It's guaranteed that each cell appears in this list no more than once.
Output
The answer can always be represented as an irreducible fraction . Print the only integer P·Q - 1 modulo 109 + 7.
Examples
3 3 2 11
2 1
2 3
333333342
4 5 3 17
1 2
3 3
4 1
514285727
1 6 2 15
1 1
1 5
4
Note
In the first sample, the rover picks one of the following six routes:
- , after passing cell (2, 3) charge is equal to 6.
- , after passing cell (2, 3) charge is equal to 6.
- , charge remains unchanged and equals 11.
- , after passing cells (2, 1) and (2, 3)charge equals 6 and then 3.
- , after passing cell (2, 1) charge is equal to 6.
- , after passing cell (2, 1) charge is equal to 6.
Expected value of the battery charge is calculated by the following formula:
.
Thus P = 19, and Q = 3.
3 - 1 modulo 109 + 7 equals 333333336.
19·333333336 = 333333342 (mod 109 + 7)
题意:给出一个n*m的方格阵,从(1,1)走到(n,m),只能向下或向右走,(1,1)会有一个初始分数s。在整个图中有k个特殊点,每经过一个点,都会将目前剩余的分数除以2且向上取整。求(1,1)到(n,m)的期望得分。
sol:
dp[i][j]表示从第i个障碍走到(n,m)经过j个障碍的方案数
因为经过最多20个S就会变成1,所以不用算下去了
转移容易,就是用所有方案数减去所有经过点超过j个的方案数在减去经过点小于j个的方案数即可
#include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();} while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();} return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) {putchar('-'); x=-x;} if(x<10) {putchar(x+'0'); return;} write(x/10); putchar((x%10)+'0'); } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar('\n') const ll Mod=1000000007; const int N=2005,M=200005; int n,m,K,S; ll fac[M],invf[M]; ll dp[N][25],ans=0; /* dp[i][j]表示从第i个障碍走到(n,m)经过j个障碍的方案数 因为经过最多20个S就会变成1,所以不用算下去了 转移容易,就是用所有方案数减去所有经过点超过j个的方案数在减去经过点小于j个的方案数即可 */ struct Node{int x,y;}P[2005]; inline bool cmpx(Node p,Node q) { return (p.x!=q.x)?(p.x<q.x):(p.y<q.y); } inline ll ksm(ll x,ll y) { ll res=1; while(y) { if(y&1) res=res*x%Mod; x=x*x%Mod; y>>=1; }return res; } inline ll C(ll n,ll m) { if(n<m)return 0; return fac[n]*invf[m]%Mod*invf[n-m]%Mod; } int main() { freopen("data.in","r",stdin); int i,j,k; R(n); R(m); R(K); R(S); fac[0]=invf[0]=1; for(i=1;i<=n+m;i++) fac[i]=fac[i-1]*i%Mod; invf[n+m]=ksm(fac[n+m],Mod-2); for(i=n+m-1;i>=1;i--) invf[i]=invf[i+1]*(i+1)%Mod; for(i=1;i<=K;i++) { R(P[i].x); R(P[i].y); } P[++K]=(Node){1,1}; // cout<<"K="<<K<<endl; sort(P+1,P+K+1,cmpx); for(i=K;i>=1;i--) { dp[i][0]=C(n-P[i].x+m-P[i].y,n-P[i].x); for(j=i+1;j<=K;j++) if(P[j].y>=P[i].y) { dp[i][0]=(dp[i][0]-C(P[j].x-P[i].x+P[j].y-P[i].y,P[j].x-P[i].x)*dp[j][0]%Mod+Mod)%Mod; } } for(i=K;i>=1;i--) { for(j=1;j<=20;j++) { dp[i][j]=C(n-P[i].x+m-P[i].y,n-P[i].x); for(k=i+1;k<=K;k++) if(P[k].y>=P[i].y) { dp[i][j]=(dp[i][j]-C(P[k].x-P[i].x+P[k].y-P[i].y,P[k].x-P[i].x)*dp[k][j]%Mod+Mod)%Mod; } for(k=0;k<=j-1;k++) dp[i][j]=(dp[i][j]-dp[i][k]+Mod)%Mod; } } // cout<<dp[1][0]<<" "<<dp[1][1]<<" "<<dp[2][0]<<" "<<dp[2][1]<<endl; ll ff=0,num=0,oo; while(S>1) { ff=(ff+dp[1][num])%Mod; ans=(ans+dp[1][num]*S%Mod)%Mod; num++; S=(S%2)+(S/2); } oo=C(n-1+m-1,n-1); ans=(ans+oo-ff+Mod)%Mod; ans=ans*ksm(oo,Mod-2)%Mod; Wl(ans); return 0; }