First check the number is prime or not using sieve-of-eratosthenes(https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html)
CASE 1 == if the number is prime then the number is equal to i or not
CASE 2 == in the case of not prime the given and i is equal to given number or not
Example:in the second case of the problem for the fourth position given number is 2 & i=4 gcd of (2,4)=2 so that is true
in the first case of the problem for the third position given number is 2 & i=3 gcdof (2,3)=1 & given in array is 2 so that is false
///Bismillahir Rahmanir Rahim
///Author: Tanvir Ahmmad
///CSE,Islamic University,Bangladesh
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<iterator>
#include <functional> ///sort(arr,arr+n,greater<int>()) for decrement of array
/*every external angle sum=360 degree
angle find using polygon hand(n) ((n-2)*180)/n*/
///Floor[Log(b) N] + 1 = the number of digits when any number is represented in base b
using namespace std;
typedef long long ll;
#define sz 100002
bool vis[100002]={true};
void seive()
{
for(int i=2;i<=sz;i+=2) vis[i]=false;
for(int i=3;i<=sz;i+=2)
if(vis[i]==true)
for(ll j=i*i;j<=sz;j+=i) vis[j]=false;
vis[1]=false;
}
int main()
{
seive();
ll tst,n;
cin>>tst;
while(tst--)
{
cin>>n;
ll arr[n+2],flag=0;
for(ll i=1;i<=n;i++) cin>>arr[i];
for(ll i=2;i<=n;i++)
{
if(arr[i]!=i)
{
if(vis[i]==true)
{
flag=1;
break;
}
if(arr[i]!=__gcd(i,arr[i]))
{
flag=1;
break;
}
}
}
if(flag==1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
///Alhamdulillah
problem link:https://www.codechef.com/LP2TO307/problems/GCDOPS
Comments
Post a Comment