Codeforces 155D(分解质因子+预处理)

来源:互联网 发布:北京ktv 知乎 编辑:程序博客网 时间:2024/05/17 15:39

问题描述:

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
SuccessConflict with 6SuccessAlready offSuccessSuccessSuccessSuccessConflict with 10Already on
题目题意:总共n盏灯,有如下处理:

+ num 表示打开num灯 三种情况 

1:灯已经打开

2:存在某一个s灯,s和num存在公因子,不能打开

3:成功打开

- num表示熄灭num灯 2种情况

1:灯已经熄灭

2:成功熄灭

题目分析:不能发现最难的操作是+ num的第二种情况,判断是否存在一个数与num有公共因子。我们预处理去1到n的所有数的质因子,每次开灯的时候去判断一下这个num的质因子在前面是否出现过,出现就不行,没有就可以。

代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<vector>using namespace std;const int maxn=1e5+100;int prime[maxn],cnt;bool vis[maxn];int ok[maxn],factor[maxn];vector<int> vec[maxn];void get_prime()//素数打表{    memset (vis,true,sizeof (vis));    vis[1]=false;    for (int i=2;i<maxn;i++) {        if (vis[i]) prime[cnt++]=i;        for (int j=0;j<cnt&&i*prime[j]<maxn;j++) {            vis[i*prime[j]]=false;            if (i%prime[j]==0) break;        }    }}void get_factor(int n)//分解质因子{    if (n==1) return vec[n].push_back(1);    int m=n;    for (int i=0;i<cnt&&prime[i]*prime[i]<=n;i++) {        if (n%prime[i]==0) {            vec[m].push_back(prime[i]);            while (n%prime[i]==0) {                n=n/prime[i];            }        }    }    if (n!=1) vec[m].push_back(n);}int main(){    get_prime();    int n,m;    while (scanf("%d%d",&n,&m)!=EOF) {        for (int i=1;i<=n;i++) {            get_factor(i);            ok[i]=factor[i]=0;        }        while (m--) {            char str[5];int num;            scanf("%s%d",str,&num);            if (str[0]=='-') {                if (ok[num]==0) {                    puts("Already off");                }                else {                    puts("Success");                    ok[num]=0;//记得删除这个数的因子                    for (int i=0;i<vec[num].size();i++) {                        factor[vec[num][i]]=0;                    }                }            }            else if (str[0]=='+') {                if (ok[num]==1) {                    puts("Already on");                }                else {                    bool flag=false;                    for (int i=0;i<vec[num].size();i++) {                        if (factor[vec[num][i]]!=0) {                            printf("Conflict with %d\n",factor[vec[num][i]]);                            flag=true;                            break;                        }                    }                    if (!flag) {                        puts("Success");                        ok[num]=1;                        for (int i=0;i<vec[num].size();i++) {                            factor[vec[num][i]]=num;                        }                    }                }            }        }    }    return 0;}