首页 技术 正文
技术 2022年11月16日
0 收藏 597 点赞 3,253 浏览 2187 个字

B. Long Pathtime limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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).

Examplesinput

2
1 2

output

4

input

4
1 1 2 3

output

20

input

5
1 1 1 1 1

output

62

官方题解
In this problem you had to simulate route of character in graph.
Note that if you are in vertice i, then edges in all vertices with numbers less than i are turned to pi. It gives us opportunity to see a recurrence formula: let dpi be number of steps, needed to get from vertice to vertice i, if all edges are rotated back, into pi. Then dpi +  = 2dpi +  - dppi. Answer will be dpn + .

很明显要用到DP

考虑d+1,只可能从i走到,但是第一次到达i,下一步会走到pi,然后又到达i

可以发现到i时i之前的全是偶数个叉,所以这两次的传送门数是一样的,第二次只是少了到p[i]到次数而已

d[i]表示第一次到达i的传送门次数,d[i]=d[i]+1+(d[i]-d[p[i]])+1

//
// main.cpp
// cf407b
//
// Created by Candy on 9/15/16.
// Copyright © 2016 Candy. All rights reserved.
//#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const ll N=,MOD=1e9+;
int n,p[N];
ll d[N];
void dp(){
for(int i=;i<=n;i++)
d[i+]=(d[i]++(d[i]-d[p[i]])++MOD)%MOD;
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&p[i]);
dp();
cout<<d[n+];
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,990
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,132
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,843