By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to n. However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated.
In 2312 there was a startling discovery: a collider’s activity is safe if and only if all numbers of activated colliders are pairwise relatively prime to each other (two numbers are relatively prime if their greatest common divisor equals 1)! If two colliders with relatively nonprime numbers are activated, it will cause a global collapse.
Upon learning this, physicists rushed to turn the colliders on and off and carry out all sorts of experiments. To make sure than the scientists’ quickness doesn’t end with big trouble, the Large Hadron Colliders’ Large Remote Control was created. You are commissioned to write the software for the remote (well, you do not expect anybody to operate it manually, do you?).
Initially, all colliders are deactivated. Your program receives multiple requests of the form “activate/deactivate the i-th collider”. The program should handle requests in the order of receiving them. The program should print the processed results in the format described below.
To the request of “+ i” (that is, to activate the i-th collider), the program should print exactly one of the following responses:
“Success” if the activation was successful.
“Already on”, if the i-th collider was already activated before the request.
“Conflict with j”, if there is a conflict with the j-th collider (that is, the j-th collider is on, and numbers i and j are not relatively prime). In this case, the i-th collider shouldn’t be activated. If a conflict occurs with several colliders simultaneously, you should print the number of any of them.
The request of “- i” (that is, to deactivate the i-th collider), should receive one of the following responses from the program:
“Success”, if the deactivation was successful.
“Already off”, if the i-th collider was already deactivated before the request.
You don’t need to print quotes in the output of the responses to the requests.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of colliders and the number of requests, correspondingly.
Next m lines contain numbers of requests, one per line, in the form of either “+ i” (without the quotes) — activate the i-th collider, or “- i” (without the quotes) — deactivate the i-th collider (1 ≤ i ≤ n).
Output
Print m lines — the results of executing requests in the above given format. The requests should be processed in the order, in which they are given in the input. Don’t forget that the responses to the requests should be printed without quotes.
Example
Input
10 10
+ 6
+ 10
+ 5
- 10
- 5
- 6
+ 10
+ 3
+ 6
+ 3
Output
Success
Conflict with 6
Success
Already off
Success
Success
Success
Success
Conflict with 10
Already on
Note
Note that in the sample the colliders don’t turn on after the second and ninth requests. The ninth request could also receive response “Conflict with 3”.
题目意思可以理解为添加一个数或者删除一个数,如果添加的数已经存在,输出一个提示,如果添加的数与已经添加的数的gcd != 1,则输出Conflict with xx,否则添加成功输出提示;如果是删除,则如果没有添加,输出提示,如果删除成功,输出提示;
这道题主要在于判断添加的数与已经添加的数是否互质。判断是否互质可以将已经添加的数的质因数存起来,当添加某个数时,将这个数分解成质因数,如果这些质因数都没有出现,则添加成功,将这些质因数也存起来;
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 100007 // 1e5+7
using namespace std;
set<int> s;
int prime[maxn];//存放素数
int mark[maxn];
int vis[maxn];
int a[maxn]; // 存放某个质因数对应的那个数
int cnt=0;
void find_prime(int n)// 找素数
{
for(int i=2;i<=n;i++)
{
if(vis[i] == 0)
{
prime[cnt++] = i;
for(int j=i+i;j<=n;j+=i)
vis[j] = 1;
}
}
}
int is_exit(int x)
{
int tmp = x;
for(int i=0; i < cnt && prime[i]*prime[i] <= tmp; i ++)
{
if( x % prime[i] == 0)
{
if( s.find( prime[i] ) != s.end() )
return prime[i];
while( x % prime[i] == 0)
x /= prime[i];
}
}
if( x > 1)
{
if(s.find(x) != s.end())
return x;
}
return 0;
}
void add(int x)
{
int tmp = x;
for(int i=0;i<cnt && prime[i]*prime[i] <= x; i ++)
{
if( x % prime[i] == 0)
{
a[ prime[i] ] = tmp;
s.insert(prime[i]);
}
while( x % prime[i] == 0)
x /= prime[i];
}
if( x > 1)
{
a[ x ] = tmp;
s.insert(x);
}
}
void del(int x)
{
for(int i=0;i<cnt && prime[i] * prime[i] <= x; i++)
{
if( x % prime[i] == 0)
{
s.erase(prime[i]);
a[ prime[i] ] = 0;
}
while( x % prime[i] == 0)
x /= prime[i];
}
if( x > 1)
{
s.erase(x);
a[ x ] = 0;
}
}
int main()
{
int n,m;
find_prime(maxn);
scanf("%d%d",&n,&m);
char ch[2];
int num;
for(int i=0;i<m;i++)
{
scanf("%s %d",ch,&num);
if( ch[0] == '+')
{
if(mark[num] == 1)
printf("Already on\n");
else
{
int tmp = is_exit(num);
if( tmp == 0)
{
printf("Success\n");
add(num);
mark[num] = 1;
}
else printf("Conflict with %d\n",a[ tmp ]);
}
}
else if( ch[0] == '-' )
{
if(mark[num] == 0)
printf("Already off\n");
else
{
del(num);
mark[num] = 0;
printf("Success\n");
}
}
}
return 0;
}