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.
  1. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.


上次的cf今天才补题o(╯□╰)o,给n层楼。在a层開始,不能在b层停,且当在x层去y层时。|x - y| < |x - b|,求运行k
 
次的方案数。


有两种情况,dp[i][j],i为第i次,j为当前停的层数。



 当a<b时,此时全部的x不会超过b,当第i次停在j层。第i-1次肯定在[0,(b+j-1)/2],左端点不难想到,右端点推导过程:


设第i-1次停在x层。则第i层全部大于x小于b的点都能够取。我们仅仅考虑小于x的点。则x-j<=b-x-1,


整理得:   x<=(b+j-1)/2; 所以转移方程为:dp[i][j]=(sum[i-1][(j+b-1)/2]-dp[i-1][j]+mod)%mod;


当a>b时,同理得dp[i][j]=((sum[i-1][n]-sum[i-1][(j+b)/2]+mod)%mod-dp[i-1][j]+mod)%mod;


代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. using namespace std;
  6. const int maxn=5000+100;
  7. const int mod=1000000000+7;
  8. int dp[maxn][maxn];
  9. int sum[maxn][maxn];
  10. int n;
  11. void getsum(int x)
  12. {
  13. for(int i=1;i<=n;i++)
  14. {
  15. sum[x][i]=(sum[x][i-1]+dp[x][i])%mod;
  16. // printf("%I64d\n",sum[x][i]);
  17. }
  18. }
  19. int main()
  20. {
  21. int a,b,k;
  22. scanf("%d%d%d%d",&n,&a,&b,&k);
  23. memset(dp,0,sizeof(dp));
  24. memset(sum,0,sizeof(sum));
  25. dp[0][a]=1;
  26. if(a<b)
  27. {
  28. getsum(0);
  29. for(int i=1;i<=k;i++)
  30. {
  31. for(int j=1;j<b;j++)
  32. {
  33. dp[i][j]=(sum[i-1][(j+b-1)/2]-dp[i-1][j]+mod)%mod;
  34. // printf("%I64d ",dp[i][j]);
  35. }
  36. // printf("\n");
  37. getsum(i);
  38. }
  39. }
  40. else
  41. {
  42. getsum(0);
  43. for(int i=1;i<=k;i++)
  44. {
  45. for(int j=b+1;j<=n;j++)
  46. {
  47. //printf("%d %d\n",sum[i-1])
  48. dp[i][j]=((sum[i-1][n]-sum[i-1][(j+b)/2]+mod)%mod-dp[i-1][j]+mod)%mod;
  49. // printf("%d ",dp[i][j]);
  50. }
  51. getsum(i);
  52. }
  53. }
  54. long long ans=0;
  55. for(int i=1;i<=n;i++)
  56. {
  57. ans=(ans+dp[k][i])%mod;
  58. //printf("%d ",dp[k][i]);
  59. }
  60. printf("%I64d\n",ans);
  61. return 0;
  62. }

E. Riding in a Lift(Codeforces Round #274)的更多相关文章

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

  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. Pythagorean Triples(Codeforces Round #368 (Div. 2) + 构建直角三角形)

    题目链接: https://codeforces.com/contest/707/problem/C 题目: 题意: 告诉你直角三角形的一条边,要你输出另外两条边. 思路: 我们容易发现除2外的所有素 ...

  4. 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)

    题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...

  5. 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)

    题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...

  6. 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)

    题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...

  7. 「日常训练」Duff in the Army (Codeforces Round #326 Div.2 E)

    题意(CodeForces 588E) 给定一棵\(n\)个点的树,给定\(m\)个人(\(m\le n\))在哪个点上的信息,每个点可以有任意个人:然后给\(q\)个询问,每次问\(u\)到\(v\ ...

  8. 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)

    题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...

  9. 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)

    题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...

随机推荐

  1. Sicily-1438

    一.      题意 买二送一.排序之后隔三求和,求折扣的最大值. 二.      代码 // // main.cpp // sicily-1438 // // Created by ashley o ...

  2. Android学习笔记(十五)——碎片的生命周期(附源代码)

    碎片的生命周期 点击下载源代码 与活动类似.碎片具有自己的生命周期.理解了碎片的生命周期后.我们能够在碎片被销毁时正确地保存事实上例,在碎片被重建时将其还原到前一个状态. 1.使用上一篇的项目Frag ...

  3. Objective-c中@interface、@implementation、@protocal

    以下 void print(); }; class AC{ }; 这时候,AI和AC是独立存在,AC不会因为没有和AI建立关系而编译错误,将AC做以下修改后,AI才和AC建立了关系,AC必须实现AI中 ...

  4. cocos2d基础入门

    HelloCpp中Classes目录下放开发者自己的类: win32:平台相关,coco2d已默认创建:coco2d-x目录下,samples/cpp/HelloCpp/(工程根目录)图片放置位置:根 ...

  5. 网络收发之cycleBuf

    #pragma once #include <iostream> #include <string> class cyclebuffer { protected: volati ...

  6. Request url 各种属性值

    網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc Request.ApplicationPath / Request.Phys ...

  7. Basic REST API Design

  8. Python 連接 MySQL

    Python 連接 MySQL MySQL 是十分流行的開源資料庫系統,很多網站也是使用 MySQL 作為後台資料儲存,而 Python 要連接 MySQL 可以使用 MySQL 模組.MySQLdb ...

  9. Swap file ".Podfile.swp" already exists!

    解决Swap file ".ceshi.c.swp" already exists!问题 关于swp文件:使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一 ...

  10. iOS学习之NSAttributedString(富文本)

    NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体.字号.字体大小等各不相同的风格,还可以对段落进行格式化,一般都是对可变富文本(N ...