CodeForces 407B
来源:互联网 发布:部落冲突九本升级数据 编辑:程序博客网 时间:2024/06/05 18:45
const int mod = 1000000000 + 7;#include<stdio.h>#include<iostream>using namespace std;int main(){ long long dp[1111]; int a[1111],n,i; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; } dp[1]=0; for(i=1;i<=n;i++) { dp[i+1]=(2*dp[i]-dp[a[i]]+2+mod)%mod; } cout<<dp[n+1]<<endl; return 0;}
Description
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 numberi(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 numberpi, where1 ≤ 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 room1.
- 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 roompi), 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 containsn integers pi (1 ≤ pi ≤ i). Eachpi denotes the number of the room, that someone can reach, if he will use the second portal in thei-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 modulo1000000007(109 + 7).
Sample Input
21 2
4
41 1 2 3
20
51 1 1 1 1
62
- CodeForces 407B DP
- CodeForces 407B
- CodeForces - 407B
- codeforces 407B B. Long Path(dp)
- CodeForces 407B Long Path
- CodeForces 407B Long Path
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- codeforces Codeforces Round #407 Div2题解 B,C,D,E
- [杂题] Codeforces 407B #239 (Div. 1) B. Long Path
- Long Path - CodeForces 407B dp
- CodeForces 407B Long Path ( DP )
- codeforces-407B Long Path(dp)
- Codeforces Round #407 (Div. 2) A+B
- Codeforces Round #407 (Div. 2) B+C!
- Codeforces Round #407 (Div. 2) B
- leetcode总结-- 排序算法(内部排序)
- JAVA的extends用法
- Ajax 得到的值对全局变量赋值没有起作用解决办法和 Jquery append()进去的动态代码 点击的js没起作用
- XML基础
- Objective-C学习笔记(一)——OC语言的特点
- CodeForces 407B
- 【人脸识别】人脸识别必读论文
- php -> =>的问题
- 今后需要养成的习惯
- javaThread学习(chapter1—线程的创建)
- 【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring3
- C# winIO32位,64位的使用
- JAVA加密解密之DH(Diffie-Hellman)算法
- CodeForces 343A