fork(1) download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int b[100001]={0},count1=0;
  5. bool arr[100001]{1,1,0,0};
  6. int func(int i,int *a)
  7. { if(b[i]!=0)
  8. return 1;
  9. if(a[i]==0)
  10. {b[i]=1;
  11. return 1;
  12. }
  13. if(b[a[i]]==0)
  14. b[i]=1+func(a[i],a);
  15. else
  16. b[i]=1+b[a[i]];
  17. if(b[i]+i-1==2 || ((b[i]+i-1)%2!=0 && arr[(b[i]+i-1)]==0))
  18. count1++;
  19. return b[i];
  20.  
  21. }
  22. int main() {
  23. // your code goes here
  24. int t,i,j;
  25. cin>>t;
  26. arr[1]=1;
  27. for(i=3;i*i<316;i=i+2)
  28. {
  29. if(arr[i]!=1)
  30. {
  31. for(j=i*i;j<=10000;j=j+2*i)
  32. {
  33. arr[j]=1;
  34. }
  35. }
  36. }
  37. while(t--)
  38. {
  39. int a[100002],i,n;
  40. count1=0;
  41. cin>>n;
  42. for(i=1;i<=n;i++)
  43. {
  44. cin>>a[i];
  45. b[i]=0;
  46. }
  47. for(i=1;i<=n;i++)
  48. {func(i,a);
  49. }
  50. cout<<count1<<"\n";
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 15992KB
stdin
1
5
3 3 0 5 3
stdout
2