fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ll n,i,j;
  8. cin>>n;
  9. ll a[n];
  10. for(i=0;i<n;i++)
  11. {
  12. cin>>a[i];
  13. }
  14. ll b[n]={0};
  15. for(i=0;i<n;i++)
  16. {
  17. ll m=10000000;
  18. for(j=0;j<n;j++)
  19. {
  20. if((i!=j )&& (__gcd(a[i],a[j])>1))
  21. {
  22. //cout<<i<<" "<<j<<" "<<__gcd(a[i],a[j])<<endl;
  23. if(abs(i-j)<m)
  24. {
  25. //cout<<" yes i am in"<<endl;
  26. b[i]=j+1;
  27. m=abs(i-j);
  28. }
  29. }
  30. }
  31. if(b[i]==0)
  32. b[i]=-1;
  33. }
  34.  
  35.  
  36. for(i=0;i<n;i++)
  37. {
  38. cout<<b[i]<<" ";
  39. }
  40.  
  41.  
  42. }
  43.  
Success #stdin #stdout 0s 15232KB
stdin
5
2 3 4 9 17
stdout
3 4 1 2 -1