Skip to content

Instantly share code, notes, and snippets.

@ArthurLoboLobo
Created May 1, 2023 00:53
Show Gist options
  • Save ArthurLoboLobo/167b6f95895bfd911184905e22f7fdf5 to your computer and use it in GitHub Desktop.
Save ArthurLoboLobo/167b6f95895bfd911184905e22f7fdf5 to your computer and use it in GitHub Desktop.
#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