欢迎使用CSDN-markdown编辑器

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

Sample Input
Input
2
1 2
Output
4
Input
4
1 1 2 3
Output
20
Input
5
1 1 1 1 1
Output
62
基本题意:总共有n+1个房间,每个房间有两个门,在这里我假设为A门和B门,这两个门其中第一个只能回到前边几个房间,假设该房间为第i个房间,那么他只能回到对应的前边的1到i号房间(ps这里为什么会有i号房间呢,后边一句来解释),具体是多少,得看输入的时候对应的是多少(也就是说如果你输入的就是i,那么也就是代表又再次回到原来的房间,回到同一个房间),第二道门是只能够到达下边一个房间,假设现在就在第i个房间,那么只能前往第i+1号房间,我在这里假设这两个房间为A门和 B门,A门往前边走,B门往后边走。而且一走进一个房间之后 ,这个房间就会有个计数器,这个计数器是只要你进去就会记一次数,如果计数之后为奇数(odd number),那么就只能走A门出去,即回到前边几个房间
数学公式的考察:这道题主要注意有个循环的过程,每次从一个门里边出来(如果你再次回到同一个房间,那么这个房间的计数器会变成奇数,(因为一开始是从这个房间的而A门出去的(ps因为你是通过回到同一个房间的,也就是说你是从A门走到1到i号房间的才到这歌儿同一个房间),那么也就是说那个时候计数器就是奇数,那么当你一进来那么就变成偶数了)这个时候就会走B门,到达那个i+1号房间)这就得出一个结论,就是只要你第一次走进一个房间(这个房间计数器变为奇数1),那么你一定是先走A门出去,前往前边几个房间(包括自己的房间),然后就再回来这个房间(这个房间计数器变成2(偶数)),然后就走B们出去走到i+1号房间最后要求你到达i+1号房间总共花多少步骤。上边说的都是理解题意得思路;
下边来说解题思路:记s[i]为在第i号房间从B门出来前往i+1号门的时候所已经用过的步子数。所求的s[i]就是从i号房间的A们出去再回到这个房间所需的步子数加上回到这个房间之后从B门出去前往i+1号房间的步子数(当然这里包括了到达第i-1号房间已经走过的步子数)。最后求得就是s[n];s[i]=2*s[i-1]-s[a[i]-1]+2;这个就是递推关系式,下边简单解释一下,这个柿子变下心就是s[i]=(s[i-1]+1)+(s[i-1]-s[a[i]-1]+1),前边一个就是从第i-1号房间直接走到i号房间,然后直接走B门到i+1号房间,中间没有算A号门,即没有考虑从i号门回到前边的第a[i]号门,你理解一下下,后边一个柿子就是从第i号门通过走A号门回到第a[i]号门,然后再回来的所花的步子数,在这里有个小技巧,从第i号房间回到第a[i]号房间其实就等价于从a[i]-1号房间到达a[i]号房间的,也就是s[a[i]-1],这里读者好好细想一下,应该可以想明白的。
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <iostream>
using namespace std;
int n;
int a[1005];
long long s[1005];
int main()
{
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
memset(s,0,sizeof s);
s[0]=0;
for(int i=1;i<=n;i++)
s[i]=(2*s[i-1]%1000000007-s[a[i]-1]%1000000007+1000000009)%1000000007;
printf("%I64d\n",s[n]%1000000007);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值