Topic Portal (internal title 125)
Input Format
A first line integer $ n $, in the meaning of the same title.
The second line n-$ $ integer, denotes the number of $ I $ $ $ P_i, the meaning of the same title.
Output Format
A line integer, $ represents the result modulo the answer to $ 1,000,000,007 (10 ^ 7 + 9).
Sample
Sample input 1:
2
1 2
Sample output 1:
4
Sample input 2:
4
1 1 2 3
Sample Output 2:
20
Sample input 3:
5
1 1 1 1 1
Sample output 3:
62
Data range and tips
Sample $ 1 $ explanation:
Olga in the initial position $ 1 $, because this is his first time to reach $ 1 $ 1 $ $ position, so the first step he would come to $ p_i = $ 1, $ 1 $ position at this time has reached twice, so the second step Olga will come to $ 1 + 1 = 2 $ position.
Similarly, Olga will go next $ p_2 = 2 $, $ 2 + 1 = 3 $, total cost $ 4 $ Step $ n + 1 $ reaches position.
Sample $ 3 $ explanation:
Do you really have the heart to let me list them all right, say so, do not stop ah (refer lazy).
data range:
For $ 10 \% $ data, to ensure that $ 1 \ leqslant n \ leqslant 20 $;
for additional $ 10 \% $ data satisfies $ p_i = i $;
for data additional $ 20 \% $ meet $ p_i = 1 $;
Further data for $ 20 \% $ ensure $ 1 \ leqslant n \ leqslant 1,000 $;
for $ 100 \ $% of the data, to ensure that $ 1 \ leqslant n \ leqslant 1,000,000,1 \ leqslant p_i \ leqslant i $.
answer
Consider $ DP $, defined $ dp [i] $ $ 1 represents the first $ $ I $ to the number of steps.
Then we can list the state transition equation:
$$dp[i+1]=2\times dp[i]-dp[p[i]]+2$$
因为$p[i]\leqslant i$,所以直接递推即可。
时间复杂度:$\Theta(n)$。
期望得分:$100$分。
实际得分:$100$分。
代码时刻
#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int n;
long long dp[1000002];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{int x;scanf("%d",&x);dp[i+1]=(2*dp[i]-dp[x]+2+mod)%mod;}
printf("%d",dp[n+1]);
return 0;
}
rp++