cf479E Riding in a Lift
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).
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
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:
- 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|.
- 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.
- In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.
唉卡在B题1个小时……最后发现C是sb题10分钟秒了
dp:f[i][j]表示走i步到j的方案数
f[i][j]=Σf[i-1][k] | k能到j
n^2k的时间效率会T,但是发现所有的k是一个连续的区间,所以我们可以用前缀和存所有f[i-1][k]的状态,然后O(1)递推
还可以更快
注意到b把1到n的区间分成两半,而且从a开始走一定只能到达a所在的一半,所以可以再优化。期望能缩掉一半复杂度
(其实我是因为2500w状态+取模很虚所以想出这不靠谱的优化)
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<cmath>
- #include<queue>
- #include<deque>
- #include<set>
- #include<map>
- #include<ctime>
- #define LL long long
- #define inf 0x7ffffff
- #define pa pair<int,int>
- #define pi 3.1415926535897932384626433832795028841971
- #define mod 1000000007
- using namespace std;
- int n,a,b,k,L,R;
- LL f[][];
- LL sum[],tot;
- inline LL read()
- {
- LL x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int main()
- {
- n=read();a=read();b=read();k=read();
- if (a<b)
- {
- L=;R=b-;
- }else
- {
- L=b+;R=n;
- }
- f[][a]=;
- for (int i=a;i<=n;i++)sum[i]=;
- for (int i=;i<=k;i++)
- {
- for (int j=L;j<=R;j++)
- {
- int des=(b+j)>>;
- if (j<b)
- {
- while(b-des<=des-j) des--;
- while(b-(des+)>(des+)-j) des++;
- f[i][j]=(sum[des]-f[i-][j]+mod)%mod;
- }else
- {
- while (des-b<=j-des) des++;
- while ((des-)-b>j-(des-)) des--;
- f[i][j]=(sum[n]-sum[des-]-f[i-][j]+mod)%mod;
- }
- }
- sum[]=;
- for (int ll=;ll<=n;ll++)
- sum[ll]=sum[ll-]+f[i][ll];
- }
- for (int i=L;i<=R;i++)
- tot+=f[k][i];
- printf("%lld\n",tot%mod);
- }
cf479E
cf479E Riding in a Lift的更多相关文章
- codeforces 480C C. Riding in a Lift(dp)
题目链接: C. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 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 ...
- 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 ...
- Codeforces 479E Riding in a Lift(dp)
题目链接:Codeforces 479E Riding in a Lift 题目大意:有一栋高N层的楼,有个无聊的人在A层,他喜欢玩电梯,每次会做电梯到另外一层.可是这栋楼里有个秘 密实验室在B层,所 ...
- 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 ...
- 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 l ...
- 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 479E. Riding in a Lift (dp + 前缀和优化)
题目链接:http://codeforces.com/contest/479/problem/E 题意: 给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...
- Codeforces 479E Riding in a Lift
http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...
随机推荐
- mysql use mysql hang
uat-db03:/root# mysql -A -uroot -p1234567 Warning: Using a password on the command line interface ca ...
- SATA1.0,2.0,3.0区别
外观没区别,接口都一样,线也一样,就是传输速率不一样,控制芯片不一样SATA1.0理论传输速度为1.5Gbit/s SATA2.0理论传输速度为3Gbit/sSATA2.0理论传输速度为6Gbit/s ...
- 2014.11.12模拟赛【最小公倍数】| vijos1047最小公倍数
最小公倍数(lcm.c/.cpp/.pas) 题目描述 给定两个正整数,求他们的最小公倍数. 样例输入 28 12 样例输出 84 数据范围 对于40%数据:1<=a,b<=10^9 对于 ...
- 什么是内存泄漏?(What is a memory leak?)
程序中的内存泄漏是怎么回事呢? 我们写过很多带有关键词free()的程序.比如我在这篇博文关于链表的一些重要操作(Important operations on a Linked List)中删除整个 ...
- 虚函数virtual
简单地说,那些被virtual关键字修饰的成员函数,就是虚函数.虚函数的作用,用专业术语来解释就是实现多态性(Polymorphism),多态性是将接口与实现进行分离:用形象的语言来解释就是实现以共同 ...
- 构建高性能WEB站点笔记二
构建高性能WEB站点笔记 因为是跳着看的,后面看到有提到啥epoll模型,那就补充下前面的知识. 第三章 服务器并发处理能力 3.2 CPU并发计算 进程 好处:cpu 时间的轮流使用.对CPU计算和 ...
- JUnit三分钟教程 ---- 实际应用
JUnit三分钟教程 ---- 实际应用 摘自http://lavasoft.blog.51cto.com/62575/65775 接上文"JUnit三分钟教程 ---- 快速起步&qu ...
- 自己写jstl标签解析long时间
数据库里存储的是long型的时间,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个fmt:formatDate可以转化,但是只能转date型,long型则不可 ...
- poj 1759 Garland (二分搜索之其他)
Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...
- [android开发之内容更新类APP]二、这几日的结果
android教程即将開始 话说这开了blog之后,就一直在试用自己的app,发现.TM的真的非常不爽,不好用,好吧.本来打算放弃了.只是看到手机里还有还有一个坑,干脆又一次做一个吧. 原来的神回复A ...