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
Success
Conflict with 6
Success
Already off
Success
Success
Success
Success
Conflict with 10
Already 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

 
解题:质因数分解。任何两个互质的数,两个数的所有的素因子必定没有交集。利用这一个性质,就能判断了。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
int prime[maxn],index[maxn],tot = ;
bool npm[maxn] = {true,true};
bool vis[maxn];
vector<int>pm[maxn];
void getprime() {
int i,j,temp;
for(i = ; i < maxn; i++) {
if(!npm[i]) prime[tot++] = i;
for(j = ; j < tot && (temp = i*prime[j]) < maxn; j++) {
npm[temp] = true;
if(i%prime[j] == ) break;
}
}
for(i = ; i < maxn; i++) {
if(!npm[i]) pm[i].push_back(i);
else {
int t = sqrt(i),k = i;
for(j = ; j < tot && prime[j] <= t; j++) {
if(k%prime[j] == ) {
pm[i].push_back(prime[j]);
while(k && k%prime[j] == ) k /= prime[j];
}
}
if(k > ) pm[i].push_back(k);
}
}
}
void del(int u){
for(int i = ; i < pm[u].size(); i++)
index[pm[u][i]] = -;
}
bool calc(int x,int &op) {
int i,j;
bool flag = true;
for(i = ; i < pm[x].size(); i++) {
if(index[pm[x][i]] > ) {
flag = false;
op = index[pm[x][i]];
break;
}
}
if(flag) {
for(i = ; i < pm[x].size(); i++)
index[pm[x][i]] = x;
}
return flag;
}
int main() {
int n,m,id,i;
char s[];
getprime();
while(~scanf("%d %d",&n,&m)){
memset(vis,false,sizeof(vis));
memset(index,-,sizeof(index));
for(i = ; i < m; i++){
scanf("%s %d",s,&id);
if(id == ){
if(s[] == '+'){
if(vis[id]) puts("Already on");
else {vis[id] = true;puts("Success");}
}else{
if(vis[id]) {vis[id] = false;puts("Success");}
else puts("Already off");
}
}else{
if(s[] == '+'){
if(vis[id]) puts("Already on");
else{
int conflic;
if(calc(id,conflic)){
vis[id] = true;
puts("Success");
}else printf("Conflict with %d\n",conflic);
}
}else{
if(vis[id]) {del(id);vis[id] = false;puts("Success");}
else puts("Already off"); }
}
}
}
return ;
}

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

  1. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  2. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  3. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  4. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  5. 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 H ...

  6. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

  7. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  8. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

  9. xtu summer individual 5 F - Post Office

    Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...

随机推荐

  1. fgetcsv()函数

    fgetcsv()函数.fgetcsv()函数可以读取指定文件的当前行,使用CSV格式解析出字段,并返回一个包含这些字段的数组.语法格式如下:array fgetcsv(resource $handl ...

  2. Python实现决策树C4.5算法

    为什么要改进成C4.5算法 原理 C4.5算法是在ID3算法上的一种改进,它与ID3算法最大的区别就是特征选择上有所不同,一个是基于信息增益比,一个是基于信息增益. 之所以这样做是因为信息增益倾向于选 ...

  3. AJPFX关于StringBuffer,StringBuilder类总结(二)

    StringBuffer,StringBuilder类 总结2需要注意的知识点:1):// String -- >StringBuffer        String s = "hel ...

  4. AJPFX总结面向对象中成员变量和成员方法的定义

    //面向对象中成员变量和成员方法的定义格式:=========================================          成员变量定义在类中方法外,可以被该类中所有方法使用. ...

  5. AJPFX:求两个城市之间的距离

    键盘录入多个城市: 城市1,城市2,城市3  以 ### 结束输出然后再键盘录入各个城市之间的距离:  格式如下:0,12,4512,0,2245,22,0### 然后按照输入的两个城市,求得两个城市 ...

  6. DOCTYPE详解

    什么是DTD? SGML引入了文档类型的概念,并由此引入了文档类型定义(Document Type Definition: DTD).文档类型定义 (DTD) 实际上就是一套关于标记符的语法规则,它包 ...

  7. Java程序操作数据库SQLserver详解

    数据库基本操作:增删改查(CRUD) crud介绍(增.删.改.查操作) CRUD是指在做计算处理时的增加(Create).查询(Retrieve)(重新得到数据).更新(Update)和删除(Del ...

  8. Linux常用终端快捷键

    UNIX程序员对键盘以及快捷键的设置都遵循一个标准:"手移动最少的距离,作更多的操作." 所有的类UNIX的终端上都有一些快捷键Ctrl+n = 下,Ctrl+b = 左,Ctrl ...

  9. 通过 getResources 找不到jar包中的资源和目录的解决方法

    http://my.oschina.net/sub/blog/184074 今天碰到一个怪问题: 原本跑的好好的代码,打成 jar 包就不能运行了. 问题出在,代码中有一段自动扫描 classpath ...

  10. C-基础:memcpy、memset、memmove、memcmp、memchr

    一,原型 void * memcpy ( void * destination, const void * source, size_t num ); 功能:将以source作为起始地址的数据复制nu ...