题目链接

题意:一共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)的更多相关文章

  1. Codeforces 407B Long Path(好题 DP+思维)

    题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...

  2. Codeforces 408D Long Path (DP)

    题目: One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n ...

  3. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  4. CF 407B Long Path[观察性质 DP]

    B. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Educational Codeforces Round 17 D. Maximum path DP

    题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...

  6. hdu-5492 Find a path(dp)

    题目链接: Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. HDU5492 Find a path[DP 方差]

    Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  9. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

随机推荐

  1. 基础知识系列☞IList ←vs→ List

    原文地址→http://www.cnblogs.com/zbphot/archive/2011/11/04/2235933.html IList接口→表示可按照索引单独访问的对象的非泛型集合. ILi ...

  2. PHP基础封装简单的MysqliHelper类

    MysqliHelper.class.php 1: <?php 2:  3: /** 4: * MysqliHelper 5: * [面向对象的MysqliHelper的简单封装] 6: */ ...

  3. Thank you for your resubmission. Performance - 2.3.10 We noticed that your app or its metadata includes irrelevant third-party platform information. Specifically, Android logo is mentioned in the

    被拒很多次,各种修改,最后发现是提交的时候,含有安卓的图标!欲哭无泪呀! Thank you for your resubmission. Performance - 2.3.10 We notice ...

  4. 大数据之Ganglia安装1

    0.前期准备修改主机名.ip.iptables关闭:时间同步:ntpdate -s time.windows.com;软件准备ganglia-3.7.1.tar.gz.ganglia-web-3.7. ...

  5. Android高仿微信(一)——如何消除启动时的白屏

    默认情况下,APP启动时会先把屏幕刷成白色,然后才绘制第一个Activity中的View,这两个步骤之间的延迟会造成启动后先看到白屏(时间大概为1秒左右).时间不长,但是我们也看到,一般的APP时不存 ...

  6. leetcode 173. Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. thinkphp加载第三方类库

    http://www.cnblogs.com/jiqing9006/p/5041389.html

  8. CSS 继承深度解析

    FROM ME: 之前在研究前端性能优化的时候,就有学习关于CSS中“善用CSS中的继承”. 原文:CSS Inheritance, The Cascade And Global Scope: You ...

  9. [KOJ6997]旅行商问题二

    [COJ6997]旅行商问题二 试题描述 Bob是一名旅行商,Bob同时也是一个哲学家,他深知到了一个地方就要掏出钱包把所有景点都玩到.一个城市有N个景点,其中N-1条无向道路链接成一个连通图.Bob ...

  10. mysql bin-log 使用说明

    如何开启bin-log日志? vi /etc/my.cnf [mysqld] log-bin=mysql-bin 默认开启 ls /usr/local/mysql/data/ mysql-bin.00 ...