-
-
Save ArthurLoboLobo/167b6f95895bfd911184905e22f7fdf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
const int maxn = 5e5+10; | |
int n, a[maxn]; | |
int main() { | |
ios::sync_with_stdio(false); cin.tie(0); | |
cin >> n; | |
for(int i = 1; i <= n; i++) { | |
cin >> a[i]; | |
} | |
long long resp = 0; | |
int Q = 0; | |
for(int i = 1; i <= n; i++) { | |
if(a[i] == i) Q++; | |
} | |
resp+= (long long) Q*(Q-1)/2; | |
for(int i = 1; i <= n; i++) { | |
if(a[i] > i && a[a[i]] == i) resp++; | |
} | |
cout << resp << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment