154B Codeforces - Colliders
Solution in c++
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli n,q,val;
vector<lli> status(100005),store(100005),show(100005);
set<int> factors;
char id;
void factorize(int x)
{
int xx=x;
if(x%2==0)factors.insert(2);
while(xx%2==0)xx/=2;
for(int i=3;i<=sqrt(x)&&xx!=1;i+=2)
if(xx%i==0)
{
factors.insert(i);
xx/=i;
i-=2;
}
if(xx!=1)factors.insert(xx);
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>q;bool lol=1;
for(int i=0;i<q;i++)
{
cin>>id>>val;
lol=0;
if(id=='+')
{
if(status[val])cout<<"Already on\n";
else
{
factorize(val);
for(auto u : factors)
{
if(store[u])
{
lol=store[u];
cout<<"Conflict with "<<show[u]<<"\n";
lol=1;
break;
}
}
if(!lol)
{
for(auto u : factors){store[u]=1;show[u]=val;}
cout<<"Success\n";
status[val]=1;
}
}
factors.clear();
}
else
{
if(!status[val])cout<<"Already off\n";
else
{
cout<<"Success\n";
status[val]=0;
factorize(val);
for(auto u : factors){store[u]=0;show[u]=0;}
factors.clear();
}
}
}
return 0;
}
0 التعليقات:
إرسال تعليق