xtu summer individual 2 D - Colliders

Colliders

Time Limit: 2000ms

Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 155D
64-bit integer IO format: %I64d      Java class name: (Any)

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.

Sample Input

10 10+ 6+ 10+ 5- 10- 5- 6+ 10+ 3+ 6+ 3

Output

SuccessConflict with 6SuccessAlready offSuccessSuccessSuccessSuccessConflict with 10Already on

Hint

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".

Source

Codeforces Round #109 (Div. 2)

解题:质因数分解。任何两个互质的数,两个数的所有的素因子必定没有交集。利用这一个性质,就能判断了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 #define INF 0x3f3f3f
11 using namespace std;
12 const int maxn = 100010;
13 int prime[maxn],index[maxn],tot = 0;
14 bool npm[maxn] = {true,true};
15 bool vis[maxn];
16 vector<int>pm[maxn];
17 void getprime() {
18     int i,j,temp;
19     for(i = 2; i < maxn; i++) {
20         if(!npm[i]) prime[tot++] = i;
21         for(j = 0; j < tot && (temp = i*prime[j]) < maxn; j++) {
22             npm[temp] = true;
23             if(i%prime[j] == 0) break;
24         }
25     }
26     for(i = 2; i < maxn; i++) {
27         if(!npm[i]) pm[i].push_back(i);
28         else {
29             int t = sqrt(i),k = i;
30             for(j = 0; j < tot && prime[j] <= t; j++) {
31                 if(k%prime[j] == 0) {
32                     pm[i].push_back(prime[j]);
33                     while(k && k%prime[j] == 0) k /= prime[j];
34                 }
35             }
36             if(k > 1) pm[i].push_back(k);
37         }
38     }
39 }
40 void del(int u){
41     for(int i = 0; i < pm[u].size(); i++)
42         index[pm[u][i]] = -1;
43 }
44 bool calc(int x,int &op) {
45     int i,j;
46     bool flag = true;
47     for(i = 0; i < pm[x].size(); i++) {
48         if(index[pm[x][i]] > 0) {
49             flag = false;
50             op = index[pm[x][i]];
51             break;
52         }
53     }
54     if(flag) {
55         for(i = 0; i < pm[x].size(); i++)
56             index[pm[x][i]] = x;
57     }
58     return flag;
59 }
60 int main() {
61     int n,m,id,i;
62     char s[4];
63     getprime();
64     while(~scanf("%d %d",&n,&m)){
65         memset(vis,false,sizeof(vis));
66         memset(index,-1,sizeof(index));
67         for(i = 0; i < m; i++){
68             scanf("%s %d",s,&id);
69             if(id == 1){
70                 if(s[0] == ‘+‘){
71                     if(vis[id]) puts("Already on");
72                     else {vis[id] = true;puts("Success");}
73                 }else{
74                     if(vis[id]) {vis[id] = false;puts("Success");}
75                     else puts("Already off");
76                 }
77             }else{
78                 if(s[0] == ‘+‘){
79                    if(vis[id]) puts("Already on");
80                    else{
81                     int conflic;
82                     if(calc(id,conflic)){
83                         vis[id] = true;
84                         puts("Success");
85                     }else printf("Conflict with %d\n",conflic);
86                    }
87                 }else{
88                     if(vis[id]) {del(id);vis[id] = false;puts("Success");}
89                     else puts("Already off");
90
91                 }
92             }
93         }
94     }
95     return 0;
96 }

xtu summer individual 2 D - Colliders,布布扣,bubuko.com

时间: 2024-10-04 01:31:40

xtu summer individual 2 D - Colliders的相关文章

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A

xtu summer individual 5 D - Subsequence

Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 353064-bit integer IO format: %I64d      Java class name: Main There is a sequence of integers. Your task is to find the longest subsequence that sa

xtu summer individual 1 A - An interesting mobile game

An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

xtu summer individual 2 C - Hometask

Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 154A64-bit integer IO format: %I64d      Java class name: (Any) Sergey attends lessons of the N-ish language. Each lesson he receives a hometas

xtu summer individual 5 F - Post Office

Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 116064-bit integer IO format: %lld      Java class name: Main There is a straight highway with villages alongside the highway. The highway is repres

xtu summer individual 5 A - To Add or Not to Add

To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 231C64-bit integer IO format: %I64d      Java class name: (Any) A piece of paper contains an array of n integers a1, a2, ..., an. Y

xtu summer individual 5 E - Burning Bridges

Burning Bridges Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 258864-bit integer IO format: %lld      Java class name: Main Ferry Kingdom is a nice little country located on N islands that are connected by