DP: Room

Source: Internet
Author: User

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 guest and to get out of the maze, he needs to get to the (n + 1)-th one.

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

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

Each time Vasya enters some and he paints a cross in its ceiling. Initially, Vasya paints a cross at the ceiling of the 1.
Let's assume that Vasya are in the class I and have already painted a cross over its ceiling. Then, if the ceiling now contains a odd number of crosses, Vasya uses the second portal (it leads to the hostel pi), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to the guest (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 "the", that someone can reach, if he'll use the second portal in the i-th.

Output
Print a single number-the number of portals 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

1, to go forward only through the first way (the number of markers is even), that is, the first time you go to the current position, the number of other positions are even;
2, remember to go to the first position step of dp[i], must be through the i-1 walk over, the first walk to I position will go back, until the second time to go to this position will go forward so dp[i]=dp[i-1]+1+ (again from the back to the location of the position to go to the first position) +1;
3, the second time to reach the backtracking position and the first time to reach the position is even, two times the same state, then the number of steps is the same, then the number of steps in the bracket should be dp[i]-dp[num[i]];

Comprehensive dp[i]=dp[i-1]*2-dp[num[i-1]]+2;

#include <iostream>
#include <cstdio>
using namespace std;
int num[10000];
Long long dp[10000];
const int mod=1e9+7;
int main ()
{
    int n;
    cin>>n;
    for (int i=1;i<=n;i++)
    scanf ("%d", &num[i]);
    for (int i=1;i<=n;i++)
    dp[i+1]= ((2*dp[i])%mod-dp[num[i]]+2+mod)%mod;//be aware that there is a negative number when you can take the modulo again
        cout<< dp[n+1]<<endl;
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.