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 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?≤?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).

Sample test(s)

input

5 2 4 1

output

2

input

5 2 4 2

output

2

input

5 3 4 1

output

0

题意:做电梯,刚开始的时候你在a层,不能到b层,每次你到新的地方的y,必须满足|x-y|<|x-b|,求坐k次有多少种可能

思路:比较容易想到的是dp[i][j]表示第i次到了j层的可能,分情况讨论,例如:当a<b的时候,下一次的层数i是不能超过j+(b-j-1)/2的,然后每次预先处理出前j层的可能。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int mod = 1000000007;
const int maxn = 5005;

int n, a, b, k, dp[maxn][maxn];
int sum[maxn];

int main() {
	scanf("%d%d%d%d", &n, &a, &b, &k);
	memset(dp, 0, sizeof(dp));
	if (a < b) {
		dp[0][a] = 1;
		for (int j = 1; j < b; j++)
			sum[j] = sum[j-1] + dp[0][j];
		for (int i = 1; i <= k; i++) {
			for (int j = 1; j < b; j++)
				dp[i][j] = (sum[(b-j-1)/2+j] - dp[i-1][j] + mod) % mod;
			sum[0] = 0;
			for (int j = 1; j < b; j++)
				sum[j] = (sum[j-1] + dp[i][j]) % mod;
		}
		printf("%d\n", sum[b-1]);
	}
	else {
		dp[0][a] = 1;
		for (int j = n; j >= b+1; j--)
			sum[j] = sum[j+1] + dp[0][j];
		for (int i = 1; i <= k; i++) {
			for (int j = b+1; j <= n; j++)
				dp[i][j] = (sum[j-(j-b-1)/2] - dp[i-1][j] + mod) % mod;
			sum[0] = 0;
			for (int j = n; j >= b+1; j--)
				sum[j] = (sum[j+1] + dp[i][j]) % mod;
		}
		printf("%d\n", sum[b+1]);
	}
	return 0;
}
时间: 2024-10-05 21:45:00

Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)的相关文章

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

题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种可能的数的序列. 解法: 定义:      dp[i][j] 表示第i步在j楼的不同序列的个数 转移方程: 当j<b时, 那么dp[i][j] += dp[i-1][0~(j与b的中点(以下))] 当j>b时, 那么dp[i][j] += dp[i-1][(j与b的中点(以下))~n] 由于dp[

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号个数比右括号多 2 2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数 3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2 (这里这么想,把左变成右,左-1,右+1) 右括号也是这样 代码: #include<bits/stdc++.h> usi

Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring" int r,c; char map[55][55]; int vis[55][55]; int mark; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; int judge(int x,int y) { if(x<0||x>=r||y<0||y>

【思维】Codeforces Round #485 (Div. 2) B. High School: Become Human(对数)

题目链接:http://codeforces.com/contest/987/problem/B 在运算的时候取对数就好了 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define ll long long 6 #define eps 1e-6 7 8 int main() 9 { 10 ll x,y; 11 double xx; 12 scanf("%lld %lld",&x,&y);

Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 1 ~ p 的顺序,问最后每个人占领的点的数量. 题解:用一个队列维护当前起点,用另一个队列模拟当前起点走 a_i 步可以到达的全部点.(60 ~ 63行关键代码,多源bfs,不可分开跑) 数据: 4 3 2 2 1 1.. 1.. ..2 ... output:10 2 1 #include <

Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)

链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a draw of n rating units is arranged. The rating will be distributed according to the following algorithm: if k participants take part in this event, t

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)

传送门 Description Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful.