#include #include #include using namespace std; int main(void) { int N; cin >> N; vector M(N + 1); for(int i = 1; i <= N; i++) { cin >> M[i]; } vector V(N + 1, 0); for(int i = 1; i <= N; i++) { if(0 < M[i] - i) { V[i] = 1; } else if(0 > M[i] - i) { V[i] = -1; } } int maxt = 0; for(int i = 1; i <= N; i++) { maxt = max(maxt, abs(M[i] - i)); } int ans = 0; for(int t = 1; t <= maxt; t++) { vector NCatStay(30001, 0); vector NCatComeFromL(30001, 0); vector NCatComeFromR(30001, 0); cerr << t << endl; for(int i = 1; i <= N; i++) { if(0 == V[i]) { cerr << i << ' ' << M[i] << endl; NCatStay[M[i]]++; } else { int pos = i + V[i] * t; cerr << i << ' ' << pos << endl; if(V[i] > 0) { NCatComeFromL[pos]++; } else { NCatComeFromR[pos]++; } if(M[i] == pos) { V[i] = 0; } } } for(int i = 1; i < 30001; i++) { int NCatCome = NCatComeFromL[i] + NCatComeFromR[i]; ans += NCatStay[i] * NCatCome; ans += NCatCome * (NCatCome - 1) / 2; cerr << i << ' ' << NCatStay[i] << ' ' << NCatCome; if(i > 1) { ans += NCatComeFromR[i - 1] * NCatComeFromL[i]; cerr << ' ' << NCatComeFromR[i - 1] << ' ' << NCatComeFromL[i]; } cerr << ' ' << ans << endl; } } cout << ans << endl; return 0; }