codeforces 480C C. Riding in a Lift(dp)
题目链接:
2 seconds
256 megabytes
standard input
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).
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).
Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).
5 2 4 1
2
5 2 4 2
2
5 3 4 1
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)的更多相关文章
- Codeforces 479E Riding in a Lift(dp)
题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...
- Codeforces 480C Riding in a Lift dp
主题链接:点击打开链接 意甲冠军: 特定 n a b k 构造一个长度k该序列. 使得序列中 对于随意两个相邻的数 | w[i-1] - w[i] | < | w[i] - b | 且第一个数 ...
- Codeforces 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces Round #274 Div.1 C Riding in a Lift --DP
题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...
- 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 ...
- 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 ...
- 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 ...
- cf479E Riding in a Lift
E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
随机推荐
- 使scp不用输入密码
使scp不用输入密码 有些时候,我们在复制/移动文件 到另一台机器时会用到scp,因为它比较安全.但如果每次都要输入密码,就比较烦了,尤其是在script里.不过,ssh有另一种用密钥对来验证的方式. ...
- jquery fadeOut 异步
1. 概述 jquery实现动画效果的函数使用起来很方便,不过动画执行是异步的, 所以要把自定义的操作放在回调函数里. 2. example <html> <body> < ...
- linux查看rpm包创建的所有目录和文件
有不少时候,我们需要查看某个rpm创建的所有目录和文件,出于了解程序结构或者其他目的,但是对于这个rpm包我们又不怎么熟悉,这个时候可以通过rpm -ql rpm名称查看. 但是rpm名称有可能又忘了 ...
- webform(内置对象)
一.内置对象 (一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie ...
- html,xhtml和xml
html,xhtml和xml的定义: 1.html即是超文本标记语言(Hyper Text Markup Language),是最早写网页的语言,但是由于时间早,规范不是很好,大小写混写且编码不规范: ...
- JS中检测数据类型的几种方式及优缺点
1.typeof 用来检测数据类型的运算符 typeof value 返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."string&quo ...
- JavaScript基础10——node对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Atitit.atiInputMethod v2词库清理策略工具 q229
Atitit.atiInputMethod v2词库清理策略工具 q229 1.1. Foreigncode 外码清理1 1.2. 垃圾词澄清1 1.1. Foreigncode 外码清理 On ...
- Mybatis学习记录(五)----Mybatis的动态SQL
1. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...
- 2015第18本:从0到1,ZERO to ONE, Notes on startups, or how to build the future
<从0到1>中文版的副标题是”开创商业与未来的秘密“,题目大得吓人,英文副标题就谨慎了许多:Notes on startups, or how to build the future. 全 ...