题解 | #附加题#
附加题
http://www.nowcoder.com/practice/58b04ed2865f4ff4921290f1bd4ee486
#include<stdio.h> #include<vector> using namespace std; const int mod = 1000000007; int main(){ int n, i; scanf("%d", &n); vector<unsigned int> p(n+1), dp(n+2); for (i = 1; i <= n; ++i) scanf("%d", &p[i]); dp[1] = 0; for (i = 1; i <= n; ++i) { dp[i+1] = (2*dp[i] - dp[p[i]] + 2 + mod) % mod; } printf("%d", dp[n+1]); return 0; }