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=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const ll Mod=;
const int N=,M=;
int n,m,K,S;
ll fac[M],invf[M];
ll dp[N][],ans=;
/*
dp[i][j]表示从第i个障碍走到(n,m)经过j个障碍的方案数
因为经过最多20个S就会变成1,所以不用算下去了
转移容易,就是用所有方案数减去所有经过点超过j个的方案数在减去经过点小于j个的方案数即可
*/
struct Node{int x,y;}P[];
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=;
while(y)
{
if(y&) res=res*x%Mod; x=x*x%Mod; y>>=;
}return res;
}
inline ll C(ll n,ll m)
{
if(n<m)return ;
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[]=invf[]=;
for(i=;i<=n+m;i++) fac[i]=fac[i-]*i%Mod;
invf[n+m]=ksm(fac[n+m],Mod-);
for(i=n+m-;i>=;i--) invf[i]=invf[i+]*(i+)%Mod;
for(i=;i<=K;i++)
{
R(P[i].x); R(P[i].y);
}
P[++K]=(Node){,};
// cout<<"K="<<K<<endl;
sort(P+,P+K+,cmpx);
for(i=K;i>=;i--)
{
dp[i][]=C(n-P[i].x+m-P[i].y,n-P[i].x);
for(j=i+;j<=K;j++) if(P[j].y>=P[i].y)
{
dp[i][]=(dp[i][]-C(P[j].x-P[i].x+P[j].y-P[i].y,P[j].x-P[i].x)*dp[j][]%Mod+Mod)%Mod;
}
}
for(i=K;i>=;i--)
{
for(j=;j<=;j++)
{
dp[i][j]=C(n-P[i].x+m-P[i].y,n-P[i].x);
for(k=i+;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=;k<=j-;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=,num=,oo;
while(S>)
{
ff=(ff+dp[][num])%Mod;
ans=(ans+dp[][num]*S%Mod)%Mod;
num++; S=(S%)+(S/);
}
oo=C(n-+m-,n-);
ans=(ans+oo-ff+Mod)%Mod;
ans=ans*ksm(oo,Mod-)%Mod;
Wl(ans);
return ;
}
codeforces722E的更多相关文章
- [Codeforces722E] Research Rover (dp+组合数学)
		
[Codeforces722E] Research Rover (dp+组合数学) 题面 给出一个N*M的方格阵,从(1,1)出发,到(N,M)结束,从(x,y)只能走到(x+1,y)或(x,y+1) ...
 
随机推荐
- 时间格式_java
			
@Test public void testDate(){ Date date=new Date(); System.out.println(date); /*日期格式*/ DateFormat df ...
 - Unity性能优化-DrawCall
			
1. DrawCall是啥?其实就是对底层图形程序(比如:OpenGL ES)接口的调用,以在屏幕上画出东西.所以,是谁去调用这些接口呢?CPU.比如有上千个物体,每一个的渲染都需要去调用一次底层接口 ...
 - ...:ES6中扩展运算符(spread)和剩余运算符(rest)详解
			
1.扩展运算符(spread) demo1:传递数据代替多个字符串的形式 let test= function(a,b,c){ console.log(a); console.log(b); cons ...
 - 【Transact-SQL】找出不包含字母、不包含汉字的数据
			
原文:[Transact-SQL]找出不包含字母.不包含汉字的数据 测试的同事,让我帮忙写个sql语句,找出表中xx列不包含汉字的行. 下面的代码就能实现. IF EXISTS(SELECT * FR ...
 - C# 中类的成员有哪些?
			
类(class)是C#类型中最基础的类型.类是一个数据结构,将状态(字段)和行为(方法和其他函数成员)组合在一个单元中.类提供了用于动态创建类实例的定义,也就是对象(object).类支持继承(inh ...
 - map自定义键值类型
			
map自定义键值类型 改变Map的默认比较方式 https://www.cnblogs.com/zjfdlut/archive/2011/08/12/2135698.html 大家知道,STL中的ma ...
 - http协议与soap协议之间的区别
			
http是标准超文本传输协议.使用对参数进行编码并将参数作为键值对传递,还使用关联的请求语义.每个协议都包含一系列HTTP请求标头及其他一些信息,定义客户端向服务器请求哪些内容,服务器用一系列HTTP ...
 - 最近公共祖先 LCA (Lowest Common Ancestors)-树上倍增
			
树上倍增是求解关于LCA问题的两个在线算法中的一个,在线算法即不需要开始全部读入查询,你给他什么查询,他都能返回它们的LCA. 树上倍增用到一个关键的数组F[i][j],这个表示第i个结点的向上2^j ...
 - 最详细的原生js实现ajax的封装
			
1. ajax的介绍 1.1 含义 ajax 的全称是Asynchronous JavaScript and XML 简单理解下:ajax就是异步的js和服务端的数据 1.2 组成 异步的js:事件, ...
 - \lib\cmsis\stm32f10x.h(298): error:  #67: expected a "}"
			
首先介绍一下csdn屏蔽广告 这个至关重要,请参考 https://blog.csdn.net/qq_40881680/article/details/82226562 更新KEIL5以后,原KEIL ...