fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define all(c) begin(c),end(c)
  6. #define p(i,j) pair<i,j>
  7. #define v(i) vector<i>
  8. #define int long long
  9.  
  10.  
  11. /* input */
  12. template<typename T>istream& operator >> ( std::istream& in,v(T)&a){for(auto &it:a)in>>it;return in;}
  13. template<typename T1,typename T2>istream& operator >>( std::istream& in,p(T1,T2)&a){in>>a.first>>a.second;return in;}
  14.  
  15. /* output */
  16. template<typename T>ostream& operator << ( std::ostream& out,v(T)&a){for(auto it:a){out<<" "<<it;}out<<endl;return out;}
  17. template<typename T1,typename T2>ostream& operator <<( std::ostream& out,p(T1,T2)&a){out<<a.first<<' '<<a.second;return out;}
  18.  
  19. const int MOD=1e9+7;
  20.  
  21. main()
  22. {
  23. //freopen("input.txt","rt",stdin);
  24. //freopen("output.txt","wt",stdout);
  25. ios::sync_with_stdio(0);
  26. cin.tie(0);
  27. int n;
  28. cin>>n;
  29. v(int) P(n);
  30. cin>>P;
  31.  
  32. v(int) DP(n);
  33. v(int) pre(n+1,0);
  34. DP[0]=2;
  35. pre[1]=2;
  36. for(int i=1;i<n;i++)
  37. {
  38. DP[i]=(2+pre[i]-pre[P[i]-1])%MOD;
  39. pre[i+1]=(pre[i]+DP[i])%MOD;
  40. }
  41. cout<<pre[n]<<endl;
  42. }
  43.  
Success #stdin #stdout 0s 3432KB
stdin
5
1 1 1 1 1
stdout
62