Skip to content

Instantly share code, notes, and snippets.

@edwardmjm
Created March 30, 2014 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardmjm/9874274 to your computer and use it in GitHub Desktop.
Save edwardmjm/9874274 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <string>
using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define foreach(it,v) for (__typeof((v).end()) it = (v).begin(); it != (v).end(); it++)
typedef long long ll;
typedef pair <int, int> PII;
const int N = 1005;
const int Mod = 1000000007;
int n;
int a[N];
int f[N][N];
int rec(int i, int j) {
if (i == j) return 0;
int &res = f[i][j];
if (res != -1) return res;
return res = (rec(i, j - 1) + rec(a[j - 1], j - 1) + 2) % Mod;
}
int main() {
scanf("%d", &n);
rep (i, n) {
scanf("%d", &a[i]);
a[i]--;
}
memset(f, 0xff, sizeof(f));
cout << rec(0, n) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment