C. Riding in a Lift

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/480/problem/C

Description

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 n, a, b, k (2 ≤ n ≤ 5000, 1 ≤ k ≤ 5000, 1 ≤ a, b ≤ n, a ≠ b).

Output

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

Sample Input

  1. 5 2 4 1

Sample Output

  1. 2

HINT

题意

有n层楼,你一开始在a层楼,实验室在b层楼,然后你可以瞎走k次

每次只要满足|now-next|<|now-b|就可以从now走到next去

然后问你一共有多少种走法

题解:

最简单就是dp[i][j]表示我现在在第i层,瞎走了j次的方案数,但是这个转移是n^3的

我们得优化一下

每次我们可以看到,从上一个状态转移过来的是一个区间,所以大概用一个线段树优化成n^2logn或者用前缀和优化成n^2的就行了

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdio.h>
  4. using namespace std;
  5. #define maxn 5005
  6. #define mod 1000000007
  7. int dp[maxn][maxn];
  8. int sum[maxn];
  9. int main()
  10. {
  11. int n,a,b,k;
  12. scanf("%d%d%d%d",&n,&a,&b,&k);
  13. dp[a][]=;
  14. for(int i=;i<=n;i++)
  15. sum[i]=dp[i][]+sum[i-];
  16. for(int i=;i<=k;i++)
  17. {
  18. for(int j=;j<=n;j++)
  19. {
  20. if(j==b)continue;
  21. int L,R;
  22. if(j<b)
  23. {
  24. L = ,R = (j+b)/;
  25. if(R-j==b-R)R--;
  26. }
  27. else
  28. {
  29. L = (j+b)/+(j+b)%;R=n;
  30. if(j-L==L-b)L++;
  31. }
  32. L=max(,L);R=min(n,R);
  33. dp[j][i]=((sum[R]-sum[L-]+mod)%mod-dp[j][i-]+mod)%mod;
  34. }
  35. for(int j=;j<=n;j++)
  36. {
  37. sum[j]=dp[j][i]+sum[j-];
  38. while(sum[j]>=mod)sum[j]-=mod;
  39. }
  40. }
  41. printf("%d\n",sum[n]);
  42. }

Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp的更多相关文章

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

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

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

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

  4. Codeforces Round #274 (Div. 2)

    A http://codeforces.com/contest/479/problem/A 枚举情况 #include<cstdio> #include<algorithm> ...

  5. Codeforces Round #274 (Div. 1) B. Long Jumps 数学

    B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...

  6. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  7. codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)

    题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #inclu ...

  8. Codeforces Round #274 (Div. 2)-C. Exams

    http://codeforces.com/contest/479/problem/C C. Exams time limit per test 1 second memory limit per t ...

  9. Codeforces Round #274 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...

随机推荐

  1. iOS - NSLog、UncaughtException日志保存到文件

    转:http://blog.csdn.net/marujunyy/article/details/12005767 对于真机,日志没法保存,不好分析问题.所以有必要将日志保存到应用的Docunment ...

  2. Python抓取单个网页中所有的PDF文档

    Github博文地址,此处更新可能不是很及时. 1.背景 最近发现算法以及数据结构落下了不少(其实还是大学没怎么好好学,囧rz),考虑到最近的项目结构越来越复杂了,用它来练练思路,就打算复习下数据结构 ...

  3. java web 学习三(Tomcat 服务器学习和使用2)

    一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:

  4. RESTLET开发实例(一)基于JAX-RS的REST服务

    RESTLET介绍 Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架.它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务. Restlet项 ...

  5. Delphi 操作Word怎么控制光标的位置

    unit ControlWordS; interface uses Classes, Sysutils, Word97; type  TControlWord = class(TComponent)  ...

  6. IOS UIView(UIButton)通过显示动画移动的时候 响应点击的解决方案

    今天在做一个UIButton显示动画的时候,遇到一个问题,就是在移动的时候 ,需要相应它的点击时间(click) 通过CAKeyframeAnimation 来移动UIButton的layer ,效果 ...

  7. RHCE ext3文件系统故障一例

    好久没来了,博客长草了,我来除除草. 给我分了两人,一个统招,一个Java两年开发经验的社招,让我这从工具平台运维往Python开发方向转的工作是举步维艰啊~ 领导看人还是真特么的不准,希望今年招聘的 ...

  8. Linux时间与Windows差8个时区的问题解决方法

    我的Debian7.1的时间与windows上的时间不一致,正好差8个时区,原因是Debian将机器的物理时间理解为UTC时间了.去网上找了好多文章,基本上都是说要改/etc/default/rcS, ...

  9. char[]数组与char *指针的区别

    char[]数组与char *指针的区别 问题描述 虽然很久之前有看过关于char指针和char数组的区别,但是当时没有系统的整理,到现在频繁遇到,在string,char[], char *中迷失了 ...

  10. HTML 5的消息通知机制

    译文来源:http://www.ido321.com/1130.html 原文:HTML 5 Notification 译文:HTML 5 的消息通知机制 译者:dwqs HTML 5 已经被应用到W ...