Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

时间:2021-10-07 18:09:48

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

5 2 4 1

Sample Output

2

HINT

题意

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

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

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

题解:

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

我们得优化一下

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

代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define maxn 5005
#define mod 1000000007
int dp[maxn][maxn];
int sum[maxn];
int main()
{
int n,a,b,k;
scanf("%d%d%d%d",&n,&a,&b,&k);
dp[a][]=;
for(int i=;i<=n;i++)
sum[i]=dp[i][]+sum[i-];
for(int i=;i<=k;i++)
{
for(int j=;j<=n;j++)
{
if(j==b)continue;
int L,R;
if(j<b)
{
L = ,R = (j+b)/;
if(R-j==b-R)R--;
}
else
{
L = (j+b)/+(j+b)%;R=n;
if(j-L==L-b)L++;
}
L=max(,L);R=min(n,R);
dp[j][i]=((sum[R]-sum[L-]+mod)%mod-dp[j][i-]+mod)%mod;
}
for(int j=;j<=n;j++)
{
sum[j]=dp[j][i]+sum[j-];
while(sum[j]>=mod)sum[j]-=mod;
}
}
printf("%d\n",sum[n]);
}

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

  1. Codeforces Round &num;274 Div&period;1 C Riding in a Lift --DP

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

  2. Codeforces Round &num;274 &lpar;Div&period; 2&rpar; E&period; 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 &num;274 &lpar;Div&period; 2&rpar; 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 &num;274 &lpar;Div&period; 2&rpar;

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

  5. Codeforces Round &num;274 &lpar;Div&period; 1&rpar; B&period; Long Jumps 数学

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

  6. Codeforces Round &num;274 &lpar;Div&period; 1&rpar; A&period; Exams 贪心

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

  7. codeforces水题100道 第八题 Codeforces Round &num;274 &lpar;Div&period; 2&rpar; A&period; Expression &lpar;math&rpar;

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

  8. Codeforces Round &num;274 &lpar;Div&period; 2&rpar;-C&period; Exams

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

  9. Codeforces Round &num;274 &lpar;Div&period; 2&rpar; 解题报告

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

随机推荐

  1. windows 下使用Nginx替代apache作为服务器

    说实话, 在windows下使用Nginx 着实有点不太方便, 但因项目需求, 又不想换系统(虽然可以搞个虚拟机玩), 只能用Nginx了 好了, 不多说了. 开始... 首先我用的是xampp包(A ...

  2. 2016021801 - Java内存区域学习笔记

    根据<深入理解java虚拟机>学习归纳整理学习笔记 程序计数器 用途:当前线程的字节码文件的行号指示器.(当前机场负责控制飞机降落的空管员:当前线程表示当前机场, 所执行的字节码等同于被等 ...

  3. STM32学习笔记——DMA控制器(向原子哥学习)

    一.DMA简介 DMA,全称为:Direct Memory Access,即直接存储器访问,DMA 用来提供在外设和存储器之间或者存储器和存储器之间的高速数据传输.当 CPU 初始化这个传输动作,传输 ...

  4. C&num;入门经典第十章接口的实现

  5. 从网络上获取图片,并写入excel文件

    package com.weChat.utils; import com.manage.utils.DateUtil;import com.manage.utils.MD5Util;import or ...

  6. Python Web开发框架Django

    花了两周时间,利用工作间隙时间,开发了一个基于Django的项目任务管理Web应用.项目计划的实时动态,可以方便地被项目成员查看(^_^又重复发明*了).从前台到后台,好好折腾了一把,用到:HTML ...

  7. Code first 数据迁移

    前段时间用到了EF,整理一下 EF ,全称Entity FramWork.就是微软以ADO.NET为基础发展的所谓ORM(对象关系映射框架,或者说是数据持久化框架). 简单说就是根据实体对象操作数据库 ...

  8. jquery validator

    jQuery.validate是一款非常不错的表单验证工具,简单易上手,而且能达到很好的体验效果,虽然说在项目中早已用过,但看到这篇文章写得还是不错的,转载下与大家共同分享. 一.用前必备 官方网站: ...

  9. 3&period;4 Templates -- Displaying A List of Items&lpar;展示一个集合&rpar;

    一. 概述 1. example 如果你需要遍历一个对象集合,使用Handlebars的{{#each}}. <ul> {{#each people key="id" ...

  10. 【jquery操作cookie】JQuery中&dollar;&period;cookie&lpar;&rpar;方法的使用(同名cookie会覆盖)

    jquery.cookie.js插件: <script type="text/javascript" src="js/jquery-1.6.2.min.js&quo ...