E. Riding in a Lift(Codeforces Round #274)「建议收藏」

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 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 ≤ 50001 ≤ k ≤ 50001 ≤ 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
5 2 4 1

output
2

input
5 2 4 2

output
2

input
5 3 4 1

output
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;

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=5000+100;
const int mod=1000000000+7;
int dp[maxn][maxn];
int sum[maxn][maxn];
int n;
void getsum(int x)
{
    for(int i=1;i<=n;i++)
    {
    sum[x][i]=(sum[x][i-1]+dp[x][i])%mod;
  //  printf("%I64d\n",sum[x][i]);
    }
}
int main()
{
    int a,b,k;
    scanf("%d%d%d%d",&n,&a,&b,&k);
    memset(dp,0,sizeof(dp));
    memset(sum,0,sizeof(sum));
    dp[0][a]=1;
    if(a<b)
    {
        getsum(0);
       for(int i=1;i<=k;i++)
       {
        for(int j=1;j<b;j++)
        {
           dp[i][j]=(sum[i-1][(j+b-1)/2]-dp[i-1][j]+mod)%mod;
          // printf("%I64d ",dp[i][j]);
        }
      // printf("\n");
        getsum(i);
       }
    }
    else
    {
        getsum(0);
        for(int i=1;i<=k;i++)
        {
            for(int j=b+1;j<=n;j++)
            {
                //printf("%d %d\n",sum[i-1])
                dp[i][j]=((sum[i-1][n]-sum[i-1][(j+b)/2]+mod)%mod-dp[i-1][j]+mod)%mod;
               // printf("%d ",dp[i][j]);
            }
            getsum(i);
        }
    }
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
    ans=(ans+dp[k][i])%mod;
    //printf("%d ",dp[k][i]);
    }
    printf("%I64d\n",ans);
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116125.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 系统可用性几个9

    系统可用性几个9经常看到各种技术文章或者分布式系统介绍说系统的可用性达到了多少个9,那么所谓”几个9“到底是怎么计算的?又意味着什么?我们简单计算分析下看看。所谓”1个9“是指90%,”2个9“是指99%,”3个9“是指99.9%,依次类推。可用性的反面是故障时间,网站或者分布式系统会因为很多原因导致不可用,比如:程序bug;运维更新错误;环境配置升级变化;机器硬件故障;被恶意攻击;网关不小心踢掉了网线/电源插座…

    2022年7月12日
    31
  • 在线写java代码

    在线写java代码前言蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力、培训、出国、大公司的经历,还有很多很好的朋友。但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为一个优秀的程序员,可惜我错过了,我用这五年时间和很多程序员一样在困惑和迷茫中找不到出路!路其实一直都在那里,只是我们看不到而已!以前我一直被公司和技术牵着走,并不是自己在选择技术,而是不自觉地被推到了这个位置上。想想有多少人对于自己将来要从事的职业和技术类型进行过深入思考和比较呢?当我跳出编码后,我开始思考和程序及程序员职

    2022年7月8日
    25
  • 谢烟客———Linux之Bash基础特性(1)

    谢烟客———Linux之Bash基础特性(1)

    2022年3月5日
    42
  • java maven 配置环境变量_maven 环境变量的配置详解

    java maven 配置环境变量_maven 环境变量的配置详解我的电脑是win10_64位的。一、安装,我使用的是免安装版的,直接解压缩就可以使用。二、配置环境变量。1.打开环境变量配置。右键计算机→属性→高级系统设置→高级→环境变量,在系统变量中配置。2.配置MAVEN_HOME。在系统变量中新建,变量名MAVEN_HOME,变量值,maven文件夹路径,我的路径是F:\Wab\资料\maven\资料\apache-maven-3.2.3,最好不要有中…

    2022年7月24日
    28
  • 使用phpmyadmin浏览库结构很卡的问题的解决方案

    使用phpmyadmin浏览库结构很卡的问题的解决方案

    2022年3月2日
    36
  • UDP协议解析

    UDP协议解析????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????????‍????UDP协议简介UDP是UserDatagramProtocol的简称,中文名是用户数据报协议,是OSI(OpenSystemInt

    2022年6月7日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号