题目链接:

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
5 2 4 1
output
2
input
5 2 4 2
output
2
input
5 3 4 1
output
0

题意:

有n层楼,不能去b层,一开始在a层,每次与要选的楼层的距离只能比去b层的小,现在问走k步的方案数是多少;

思路:

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的前缀和了;好神奇啊;学习到了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const int mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+20;
const int maxn=5e3+5;
const double eps=1e-12; int n,a,b,k;
int dp[maxn][maxn]; inline void solve(int d,int l,int r,int val)
{
if(l>r)return ;
dp[d][l]+=val;
if(dp[d][l]>=mod)dp[d][l]-=mod;
dp[d][r+1]-=val;
if(dp[d][r+1]<0)dp[d][r+1]+=mod;
}
int main()
{
read(n);read(a);read(b);read(k);
for(int i=0;i<=n;i++)for(int j=0;j<=k;j++)dp[i][j]=0;
int len=abs(b-a)-1;
int l=max(1,a-len),r=min(n,a+len);
for(int i=l;i<=r;i++)if(i!=b&&i!=a)dp[1][i]=1;
for(int i=n;i>0;i--)
{
dp[1][i]=dp[1][i]-dp[1][i-1];
if(dp[1][i]<0)dp[1][i]+=mod;
}
for(int i=1;i<k;i++)
{
int sum=0;
for(int j=1;j<=n;j++)
{
sum=sum+dp[i][j];
if(sum>=mod)sum-=mod;
if(j==b)continue;
len=abs(j-b)-1;
l=max(1,j-len);r=min(n,j+len);
solve(i+1,l,j-1,sum);
solve(i+1,j+1,r,sum);
}
}
int ans=0,sum=0;
for(int i=1;i<=n;i++)
{
sum+=dp[k][i];
if(sum>=mod)sum-=mod;
ans+=sum;
if(ans>=mod)ans-=mod;
}
cout<<ans<<"\n";
return 0;
}

  

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. 【OpenCV】选择ROI区域

    问题描述:在测试目标跟踪算法时,需要选择不同区域作为目标,进行目标跟踪,测试目标跟踪的效果. 解决思路: 1.OpenCV中提供了鼠标交互控制,利用setMouseCallback()给固定的窗口设置 ...

  2. SwipeRefreshLayout下拉刷新

    1.SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便. 弊端:只有下拉 //设置刷新控件圈圈的颜色 sw ...

  3. C# WM_NCMOUSELEAVE 消息触发

    public static extern bool TrackMouseEvent([In, Out] TRACKMOUSEEVENT lpEventTrack); [DllImport(" ...

  4. Android 多语言

    Android 多语言 在res文件上右击创建新的values文件 在strings文件中设置多语言 3.在layout文件中使用 @strings/key 引用相应资源

  5. SharePoint 2013 内容部署功能简介

    在之前的项目中,当客户有新的需求的时候,我们通常在测试环境上开发或者实施,然后手动在生产环境再弄一次.当发现内容部署这个东西,才知道这样是多么不合理的.我们可以创建两个网站集,一个用来修改,然后通过计 ...

  6. Fix Internet Explorer Crashes with SharePoint 2013 Online Presence Indicators

    IE中,只要是鼠标浮动到人名字上面的状态的时候,这个状态是与Lync相连接的,IE就会出现停止工作. 以下是解决方法. Until the other day when I figured this ...

  7. 递归练习(C语言)

    本文地址:http://www.cnblogs.com/archimedes/p/recursive-practice.html,转载请注明源地址. 1.炮弹一样的球状物体,能够堆积成一个金字塔,在顶 ...

  8. 不小心改了Xcode系统的头文件,运行报错,解决办法

  9. 数据库性能调优——sql语句优化(转载及整理) —— 篇2

    下面是在网上搜集的一些个人认为比较正确的调优方案,如有错误望指出,定虚心改正 (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中 ...

  10. [Derby]数据库操作说明

    1. 创建新数据库 connect 'jdbc:derby:mydb;create=true'; ij> connect 'jdbc:derby:mydb;create=true'; ij> ...