题目链接:

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

Examples
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
  2.  
  3. 题意:
  4.  
  5. n层楼,不能去b层,一开始在a层,每次与要选的楼层的距离只能比去b层的小,现在问走k步的方案数是多少;
  6.  
  7. 思路:
  8.  
  9. dp[i][j]表示走了i次,第i次在j层的方案数,这样转移很简单,但是复杂度太高,是O(k*n*n);
    可以发现在枚举下一次的层数的时候更新是更新一段的,用线段树啥的还有个log的复杂度,我们可以把dp[i][j]分解成s[i][j]-s[i][j-1];
    那么dp[i][j]就是s的前缀和了;好神奇啊;学习到了;
  10.  
  11. AC代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <bits/stdc++.h>
  7. #include <stack>
  8. #include <map>
  9.  
  10. using namespace std;
  11.  
  12. #define For(i,j,n) for(int i=j;i<=n;i++)
  13. #define mst(ss,b) memset(ss,b,sizeof(ss));
  14.  
  15. typedef long long LL;
  16.  
  17. template<class T> void read(T&num) {
  18. char CH; bool F=false;
  19. for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
  20. for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
  21. F && (num=-num);
  22. }
  23. int stk[70], tp;
  24. template<class T> inline void print(T p) {
  25. if(!p) { puts("0"); return; }
  26. while(p) stk[++ tp] = p%10, p/=10;
  27. while(tp) putchar(stk[tp--] + '0');
  28. putchar('\n');
  29. }
  30.  
  31. const int mod=1e9+7;
  32. const double PI=acos(-1.0);
  33. const int inf=1e9;
  34. const int N=1e6+20;
  35. const int maxn=5e3+5;
  36. const double eps=1e-12;
  37.  
  38. int n,a,b,k;
  39. int dp[maxn][maxn];
  40.  
  41. inline void solve(int d,int l,int r,int val)
  42. {
  43. if(l>r)return ;
  44. dp[d][l]+=val;
  45. if(dp[d][l]>=mod)dp[d][l]-=mod;
  46. dp[d][r+1]-=val;
  47. if(dp[d][r+1]<0)dp[d][r+1]+=mod;
  48. }
  49. int main()
  50. {
  51. read(n);read(a);read(b);read(k);
  52. for(int i=0;i<=n;i++)for(int j=0;j<=k;j++)dp[i][j]=0;
  53. int len=abs(b-a)-1;
  54. int l=max(1,a-len),r=min(n,a+len);
  55. for(int i=l;i<=r;i++)if(i!=b&&i!=a)dp[1][i]=1;
  56. for(int i=n;i>0;i--)
  57. {
  58. dp[1][i]=dp[1][i]-dp[1][i-1];
  59. if(dp[1][i]<0)dp[1][i]+=mod;
  60. }
  61. for(int i=1;i<k;i++)
  62. {
  63. int sum=0;
  64. for(int j=1;j<=n;j++)
  65. {
  66. sum=sum+dp[i][j];
  67. if(sum>=mod)sum-=mod;
  68. if(j==b)continue;
  69. len=abs(j-b)-1;
  70. l=max(1,j-len);r=min(n,j+len);
  71. solve(i+1,l,j-1,sum);
  72. solve(i+1,j+1,r,sum);
  73. }
  74. }
  75. int ans=0,sum=0;
  76. for(int i=1;i<=n;i++)
  77. {
  78. sum+=dp[k][i];
  79. if(sum>=mod)sum-=mod;
  80. ans+=sum;
  81. if(ans>=mod)ans-=mod;
  82. }
  83. cout<<ans<<"\n";
  84. return 0;
  85. }

  

codeforces 480C C. Riding in a Lift(dp)的更多相关文章

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

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

  2. Codeforces 480C Riding in a Lift dp

    主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...

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

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

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

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

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

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

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

  8. cf479E Riding in a Lift

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

  9. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

随机推荐

  1. 开发机多用户 xdebug 远程调试 PhpStorm

    在公司都用的远程开发机开发,每次有错误调试就得dd(xxx)然后保存真是,让我在本地开发用惯xdebug的情何以堪,所以有了下文. 1.安装配置xdebug 直接使用pecl安装即可 # pecl i ...

  2. 六个创建模式之简单工厂模式(Simple Factory Pattern)

    定义: 定义一个工厂类,它可以根据参数的不同生成对应的类的实例:被创建的类的实例通常有相同的父类.因为该工厂方法尝尝是静态的,所以又被称为静态工厂方法(Static Factory Method) 结 ...

  3. C++之内联函数与constexpr

    inline 函数 规模小,流程直接且频繁调用 cout<<shortString(s1,s2)<<endl; = cout<<(s1.size()<s2.s ...

  4. pace.js和NProgress.js两个加载进度插件的一点小总结

    这两个插件都是关于加载进度动画的,应该说各有特点吧,最起码对我来说是各有优势的.今天一天就捣鼓了加载进度动画,也研究了大量的(也就这两个)加载进度动画,也算对加载进度动画有了一个初步的了解了吧. NP ...

  5. ArcGIS使用Python脚本工具

    在Pyhton写的一些代码,用户交互不方便,用户体验比较差,不方便重用.在ArcGIS中可以将用写的Python代码导入到ToolBox中,这样用起来就比较方便了.这里用按要素裁剪栅格的Python来 ...

  6. Android使用layer-list实现三面边框

    layer-list可以将多个图片或形状按照顺序层叠起来 <?xml version="1.0" encoding="utf-8"?> <la ...

  7. 搭建Android 5.0开发环境

    1.Android SDK的安装 下载地址:http://developer.android.com/index.html 访问网站的话请自备梯子 选择:adt-bundle-windows-x86_ ...

  8. Python基础(3)--列表和元组

    Python包含6种内建序列:列表.元组.字符串.Unicode字符串.buffer对象.xrange对象 本篇主要讨论最常用的两种类型:列表.元组 本文地址:http://www.cnblogs.c ...

  9. 关于EntityFramework在vs2012无法引用的问题

    这段时间学习MVC,发现一个问题,我公司的电脑可以直接引用EntityFrameWork这个命名空间,但我家里面的电脑就不能直接引用,刚开始以为是我电脑配置问题,后重装电脑,发现问题并没有解决. 今天 ...

  10. mysql给数据库授权 GRANT ALL PRIVILEGES ON

    mysql> grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’; show grants for mustang@ ...