CodeForces 407B Long Path (DP)
题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模。
题解:设dp[i]表示从1走到i需要多少步,那么走到房间i+1需要dp[i+1]=dp[i]+1+x+1,这里面第一个1表示走完这步退回到Pi,这个x表示退回到房间Pi再走回来的步数,第二个1表示走完这步到达i+1。我们发现:1.当走到房间Pi的时候,Pi+1到i都是没有走过的即为0次;2.当走到房间i的时候,Pi+1到i这段一定都走过了偶数次,因为奇数次是不会放你过来的,一定都是偶数次才能走到i这步,所以说状态相同。x就是从Pi走到i所需要的步数,那么x=dp[i]-dp[p[i]],所以状态转移方程是dp[i+1]=dp[i]+1+dp[i]-dp[p[i]]+1。值得注意在结果对1e9+7取模,取模的时候要先+mod再%mod,其中涉及到减法可能有负数,前面每个数都是取模过的数,dp[i]是很可能小于dp[p[i]]的,当然如果都不取模的话dp数组肯定是递增的。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
const ll mod=1e9+;
ll p[maxn],dp[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&p[i]);
dp[]=;
for(int i=;i<=n;i++)
dp[i+]=(dp[i]++dp[i]-dp[p[i]]++mod)%mod;
printf("%lld\n",dp[n+]);
return ;
}
CodeForces 407B Long Path (DP)的更多相关文章
- Codeforces 407B Long Path(好题 DP+思维)
题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...
- Codeforces 408D Long Path (DP)
题目: One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- CF 407B Long Path[观察性质 DP]
B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- hdu-5492 Find a path(dp)
题目链接: Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU5492 Find a path[DP 方差]
Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- codeforces 721C C. Journey(dp)
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- [译]了解AngularJS $resource
原文: https://learnable.com/books/angularjs-novice-to-ninja/preview/understanding-angularjs-resource-e ...
- Js设置及获取Cookie的方法
Login页面设置Cookie: <script type="text/javascript"> if(json.result=="true") { ...
- Lexicography(数学推论>>求按字典序排第k个排列)
Lexicography Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit ...
- 分享一个c#t的网页抓取类
using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...
- MySQL Python教程(4)
Class cursor.MySQLCursorBuffered 该类从Class cursor.MySQLCursorBuffered继承,如果需要,可以在执行完SQL语句后自动缓冲结果集合.imp ...
- Win7 x64bit安装Oracle10g
解决方案: 步骤一:在解压出的oracle文件夹中搜索refhost.xml文件,搜索结果出现2条符合条件文件,这两个文件均需要修改. 打开文件发现内容中有包含...5.0 6.0等系统说明, ...
- ex6的选择器
前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuery的称赞,很多是由于jQuery方便的元素选择器 ...
- Android学习笔记(十六)——数据库操作(上)
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 为了让我们能够更加方便地管理数据库,专门提供了一个 SQLiteOpenHelper帮助类, ...
- jquery的change():下拉框值改变时触发
<script src="jquery.min.js" type="text/javascript"></script> <scr ...
- 小白死去活来的安装ros_qtc_plugin
在距离写上一篇有关ROS的文章已经过去了很久了.在这段时间内一直在积累,盼望着能够厚积薄发,但还是被无情的社会折磨的死去活来,深深的体会到了一般学校和重点学校找工作的差别,以及用人单位的区别对待.说到 ...