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
  1. 5 2 4 1
output
  1. 2
input
  1. 5 2 4 2
output
  1. 2
input
  1. 5 3 4 1
output
  1. 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状态+取模很虚所以想出这不靠谱的优化)

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<deque>
  9. #include<set>
  10. #include<map>
  11. #include<ctime>
  12. #define LL long long
  13. #define inf 0x7ffffff
  14. #define pa pair<int,int>
  15. #define pi 3.1415926535897932384626433832795028841971
  16. #define mod 1000000007
  17. using namespace std;
  18. int n,a,b,k,L,R;
  19. LL f[][];
  20. LL sum[],tot;
  21. inline LL read()
  22. {
  23. LL x=,f=;char ch=getchar();
  24. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  25. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  26. return x*f;
  27. }
  28. int main()
  29. {
  30. n=read();a=read();b=read();k=read();
  31. if (a<b)
  32. {
  33. L=;R=b-;
  34. }else
  35. {
  36. L=b+;R=n;
  37. }
  38. f[][a]=;
  39. for (int i=a;i<=n;i++)sum[i]=;
  40. for (int i=;i<=k;i++)
  41. {
  42. for (int j=L;j<=R;j++)
  43. {
  44. int des=(b+j)>>;
  45. if (j<b)
  46. {
  47. while(b-des<=des-j) des--;
  48. while(b-(des+)>(des+)-j) des++;
  49. f[i][j]=(sum[des]-f[i-][j]+mod)%mod;
  50. }else
  51. {
  52. while (des-b<=j-des) des++;
  53. while ((des-)-b>j-(des-)) des--;
  54. f[i][j]=(sum[n]-sum[des-]-f[i-][j]+mod)%mod;
  55. }
  56. }
  57. sum[]=;
  58. for (int ll=;ll<=n;ll++)
  59. sum[ll]=sum[ll-]+f[i][ll];
  60. }
  61. for (int i=L;i<=R;i++)
  62. tot+=f[k][i];
  63. printf("%lld\n",tot%mod);
  64. }

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. mysql use mysql hang

    uat-db03:/root# mysql -A -uroot -p1234567 Warning: Using a password on the command line interface ca ...

  2. SATA1.0,2.0,3.0区别

    外观没区别,接口都一样,线也一样,就是传输速率不一样,控制芯片不一样SATA1.0理论传输速度为1.5Gbit/s SATA2.0理论传输速度为3Gbit/sSATA2.0理论传输速度为6Gbit/s ...

  3. 2014.11.12模拟赛【最小公倍数】| vijos1047最小公倍数

    最小公倍数(lcm.c/.cpp/.pas) 题目描述 给定两个正整数,求他们的最小公倍数. 样例输入 28 12 样例输出 84 数据范围 对于40%数据:1<=a,b<=10^9 对于 ...

  4. 什么是内存泄漏?(What is a memory leak?)

    程序中的内存泄漏是怎么回事呢? 我们写过很多带有关键词free()的程序.比如我在这篇博文关于链表的一些重要操作(Important operations on a Linked List)中删除整个 ...

  5. 虚函数virtual

    简单地说,那些被virtual关键字修饰的成员函数,就是虚函数.虚函数的作用,用专业术语来解释就是实现多态性(Polymorphism),多态性是将接口与实现进行分离:用形象的语言来解释就是实现以共同 ...

  6. 构建高性能WEB站点笔记二

    构建高性能WEB站点笔记 因为是跳着看的,后面看到有提到啥epoll模型,那就补充下前面的知识. 第三章 服务器并发处理能力 3.2 CPU并发计算 进程 好处:cpu 时间的轮流使用.对CPU计算和 ...

  7. JUnit三分钟教程 ---- 实际应用

    JUnit三分钟教程 ---- 实际应用 摘自http://lavasoft.blog.51cto.com/62575/65775   接上文"JUnit三分钟教程 ---- 快速起步&qu ...

  8. 自己写jstl标签解析long时间

    数据库里存储的是long型的时间,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个fmt:formatDate可以转化,但是只能转date型,long型则不可 ...

  9. poj 1759 Garland (二分搜索之其他)

    Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...

  10. [android开发之内容更新类APP]二、这几日的结果

    android教程即将開始 话说这开了blog之后,就一直在试用自己的app,发现.TM的真的非常不爽,不好用,好吧.本来打算放弃了.只是看到手机里还有还有一个坑,干脆又一次做一个吧. 原来的神回复A ...