题目:

One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.

The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≤ pi ≤ i.

In order not to get lost, Vasya decided to act as follows.

  • Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
  • Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal.

Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.

Input

The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.

Output

Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).

Examples
input
2
1 2
output
4
input
4
1 1 2 3
output
20
input
5
1 1 1 1 1
output
62

题意:
如果当前到达点i的进入次数cnt为奇数 则可以到达p[i]位置 如果是偶数 可以到达i+1位置 求从1走到n+1需要的总步数 思路:
第一次走进i房间的进入次数cnt为1 是奇数 会往后退回p[i]位置 但是此刻p[i]位置的cnt也变成了奇数 因为当时只有为偶数才能向前走 那么会退回到p[p[i]]位置 不断递归
因此 dp[i]记为走到当前位置需要的总步数 状态转移方程为dp[i+1]=dp[i]+1+(dp[i]-dp[p[i]])+1 即为dp[i+1]=2*dp[i]-dp[p[i]]+2

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=1e3+;
const int mod=1e9+;
int n;
int p[maxn];
ll dp[maxn]; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&p[i]);
}
dp[]=;
for(int i=;i<=n;i++){
dp[i+]=(*dp[i]-dp[p[i]]++mod)%mod;
}
printf("%lld\n",(dp[n+]+mod)%mod);
return ;
}

Codeforces 408D Long Path (DP)的更多相关文章

  1. CodeForces 407B Long Path (DP)

    题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[ ...

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

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

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

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

  4. Educational Codeforces Round 17 D. Maximum path DP

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

  5. hdu-5492 Find a path(dp)

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

  6. HDU5492 Find a path[DP 方差]

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

  7. codeforces Hill Number 数位dp

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

  8. codeforces 721C C. Journey(dp)

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

  9. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

随机推荐

  1. 8款压箱底的Mac屏幕截图和录音录像工具软件,请你务必低调使用

    以下几款是是Mac上优秀的屏幕截图.录像和录音工具,有了这些工具,在Mac上进行截屏.录制视频或者录音都会事半功倍. 1. Snagit Mac上最好用最强大的屏幕截图工具,支持各种方式的屏幕截图以及 ...

  2. 洛谷P2765魔术球问题 最小路径覆盖

    https://www.luogu.org/problemnew/show/P2765 看到这一题第一眼想到:这不是二分最大流吗,后来发现还有一种更快的方法. 首先如果知道要放多少个球求最少的柱子,很 ...

  3. Mybatis分页插件PageHelper

    application.properties配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.suppor ...

  4. Object的数据属性和访问器属性

    一.数据属性 1.数据属性:它包含的是一个数据值的位置,在这可以对数据值进行读写. 2.数据属性包含四个特性,分别是: configurable:表示能否通过delete删除属性从而重新定义属性,能否 ...

  5. perl trick

    一.die if以及文件的操作 #!/usr/bin/perl -w use strict; die "USAGE:\n\tperl $0 <><><>\ ...

  6. saltstack syndic

    #syndic 相当于master的代理,master通过syndic代理控制node主机 master <------ syndic+master <---------- node ma ...

  7. Kafka技术内幕 读书笔记之(五) 协调者——消费者加入消费组

    消费者客户端轮询的3个步骤:发送拉取请求,客户端轮询,获取拉取结果 . 消费者在发送拉取请求之前,必须首先满足下面的两个条件.- 确保消费者已经连接协调者, 即找到服务端中管理这个消费者的协调者节点 ...

  8. Linux记录- Linux下限制rm -rf /

    操作说明: 为了防止在linux下执行操作的时候误操作rm -rf /,或者rm -rf 一些比较重要的目录,我们做以下操作来限制rm的删除 1.下载源码安装包 https://raw.githubu ...

  9. 关于Ant脚本

    在开发中,一个项目要经历单元测试l,集成测试,系统测试,测试过程中可能要不断修改代码,Ant脚本,通过一个xml文件,封装一系列繁琐又常用的操作,通过Ant指令执行xml脚本来批处理创建删除任务,编译 ...

  10. SpringBoot系列: Spring MVC视图方法的补充

    SpringMVC 视图方法的参数, 已经在这个文章中写得非常清楚了, 链接为 https://www.cnblogs.com/morethink/p/8028664.html 这篇文章做一些补充. ...