E. Riding in a Lift
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

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).

Input

The first line of the input contains four space-separated integers nabk (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ na ≠ b).

Output

Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).

Sample test(s)
input
5 2 4 1
output
2
input
5 2 4 2
output
2
input
5 3 4 1
output
0
Note

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:

  1. 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|.
  2. 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.
  3. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces 479E Riding in a Lift(dp)

    题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #274 Div.1 C Riding in a Lift --DP

    题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...

  8. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  9. Codeforces 479E Riding in a Lift

    http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...

随机推荐

  1. POJ3255--次短路

    求1到N的次短路,到某个顶点v的次短路有两种可能,一是到其他某个顶点u的最短路+edge(u,v)二是到其他某个点u的次短路+edge(u,v): 因此在松弛的时候不仅要记录最短路,同时也要记录次短路 ...

  2. error Infos

  3. 2016"百度之星" - 资格赛(Astar Round1) 1001

    思路:第一个做法就是:每读入起始位置i和结束位置j,就从这位置i到位置j计算,可是TLE了,后面我想想要是我输入一个最长的字符串,且以最大次数计算开始位置1到结束位置100000,那么这计算量是很大的 ...

  4. Appstore 创建App步骤

    .进入AppInformation界面 defaultLanguage:选择默认语言 AppName:填写在Appstore上显示的名字 SKUNumber:填写一个唯一标示符,这个只要唯一即可,不能 ...

  5. CodeForces 158 B. Taxi(模拟)

    [题目链接]click here~~ [题目大意]n组团体去包车,每组团体的人数<=4,一辆车最多容纳4人,求所求车的数目最小 [解题思路]:思路见代码~~ // C #ifndef _GLIB ...

  6. BZOJ 2648/2716(SJY把件-KD_Tree)[Template:KD_Tree]

    2648: SJY把件 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 1180  Solved: 391 [id=2648" style= ...

  7. JAVA反射机制示例,读取excel数据映射到JAVA对象中

    import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...

  8. 已解决 C# 调用 MySQLDriverCS 类库 报 vshost32-clr2.exe 已停止工作

    这几天修改一个项目是用C# 通过调用 MySQLDriverCS.dll 类库来操作 MySql数据库, 调试的会发生以上错误(直接运行是正常的),刚开始以为是兼容性问题,吧此错误百度上一粘贴有的人说 ...

  9. 2016 ACM/ICPC Asia Regional Shenyang Online

    I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值 ...

  10. Android-------手机屏幕适配之文件适配

    public class Main {         //定义文件本地存储路径,可按照需求更改         private final static String rootPath = &quo ...