cf479E Riding in a Lift
Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.
Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y ≠ x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.
Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).
The first line of the input contains four space-separated integers n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).
Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
5 2 4 1
2
5 2 4 2
2
5 3 4 1
0
Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≤ j ≤ k), that pj ≠ qj.
Notes to the samples:
- In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
- In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
- In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
唉卡在B题1个小时……最后发现C是sb题10分钟秒了
dp:f[i][j]表示走i步到j的方案数
f[i][j]=Σf[i-1][k] | k能到j
n^2k的时间效率会T,但是发现所有的k是一个连续的区间,所以我们可以用前缀和存所有f[i-1][k]的状态,然后O(1)递推
还可以更快
注意到b把1到n的区间分成两半,而且从a开始走一定只能到达a所在的一半,所以可以再优化。期望能缩掉一半复杂度
(其实我是因为2500w状态+取模很虚所以想出这不靠谱的优化)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
#define mod 1000000007
using namespace std;
int n,a,b,k,L,R;
LL f[][];
LL sum[],tot;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main()
{
n=read();a=read();b=read();k=read();
if (a<b)
{
L=;R=b-;
}else
{
L=b+;R=n;
}
f[][a]=;
for (int i=a;i<=n;i++)sum[i]=;
for (int i=;i<=k;i++)
{
for (int j=L;j<=R;j++)
{
int des=(b+j)>>;
if (j<b)
{
while(b-des<=des-j) des--;
while(b-(des+)>(des+)-j) des++;
f[i][j]=(sum[des]-f[i-][j]+mod)%mod;
}else
{
while (des-b<=j-des) des++;
while ((des-)-b>j-(des-)) des--;
f[i][j]=(sum[n]-sum[des-]-f[i-][j]+mod)%mod;
}
}
sum[]=;
for (int ll=;ll<=n;ll++)
sum[ll]=sum[ll-]+f[i][ll];
}
for (int i=L;i<=R;i++)
tot+=f[k][i];
printf("%lld\n",tot%mod);
}
cf479E
cf479E Riding in a Lift的更多相关文章
- codeforces 480C C. Riding in a Lift(dp)
题目链接: C. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp
C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...
- E. Riding in a Lift(Codeforces Round #274)
E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 479E Riding in a Lift(dp)
题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...
- Codeforces Round #274 (Div. 2) Riding in a Lift(DP 前缀和)
Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)
Imagine that you are in a building that has exactly n floors. You can move between the floors in a l ...
- Codeforces Round #274 Div.1 C Riding in a Lift --DP
题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces 479E Riding in a Lift
http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...
随机推荐
- 热点块引发的cache buffers cahins latch
热点块引发的Cache buffer Chains latch: SQL语句即便适当进行了调优,有时也无法解决cache buffers cahins latch,若在编写SQL语句时的SQL工作方式 ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- ubuntu 14.04 下试用Sublime Text 3
很多源代码都没有IDE支持的,尤其是开源的源代码.从github上下载的,很多也不用IDE.包括我自己公司的代码,基本都是脚本,也不用IDE.通常情况下,都是用notepad++.UE之类的文本编辑器 ...
- Javascript 解构的用处
对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量. let { log, sin, cos } = Math; 上面代码将Math对象的对数.正弦.余弦三个方法,赋值到对应的变量上,使用起 ...
- Spring Tool Suit 在Eclipse上的安装
登录http://spring.io/tools/sts/all 下载所需的Spring Tool Suit安装包 我用的是springsource-tool-suite-3.6.1.RELEASE- ...
- (转)25个增强iOS应用程序性能的提示和技巧--高级篇
高级当且仅当下面这些技巧能够解决问题的时候,才使用它们: 22.加速启动时间23.使用Autorelease Pool24.缓存图片 — 或者不缓存25.尽量避免Date格式化 高级性能提升 寻找一些 ...
- Android学习总结——TextView跑马灯效果
Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1.android:ellipsize="marquee" 2.TextView必须单行显示,即内容必须 ...
- angular的数据双向绑定秘密
Angular用户都想知道数据绑定是怎么实现的.你可能会看到各种各样的词汇:$watch,$apply,$digest,dirty-checking... 它们是什么?它们是如何工作的呢?这里我想回答 ...
- Sql server Lock
http://www.cnblogs.com/wuyifu/archive/2013/11/28/3447870.html
- Java Timer触发定时器
XML: <!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.sprin ...