xtu summer individual 2 D - Colliders
Colliders
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
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的更多相关文章
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- 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 ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- xtu summer individual 1 D - Round Numbers
D - Round Numbers Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u D ...
- xtu summer individual 5 F - Post Office
Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...
随机推荐
- 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数
给定一个整数数组,除了某个元素外其余元素均出现两次.请找出这个只出现一次的元素.备注:你的算法应该是一个线性时间复杂度. 你可以不用额外空间来实现它吗? 详见:https://leetcode.com ...
- python_10(模块与包)
第1章 模块 1.1 模块的种类 1.2 定义 1.3 作用 1.4 导入及使用 1.4.1 import 1.4.2 测试一: 1.4.3 测试二: 1.4.4 测试三: 1.4.5 小结 1.4. ...
- input标签属性
很多时候,我们都用到了很多标签实现输入功能,所以在这里梳理一下. 1.建立一个文本框 <input type="text" name="userName" ...
- AJPFX简述Java中this关键字的使用
Java中this关键字的使用主要有两处: 1.构造方法 this指的是调用构造方法进行初始化的对象. //有参构造public Human(String name, int age) { this( ...
- 初试springWebMVC
最近在尝试配置SpringMVC,发现各种坑. 首先遇到了这个问题. 'component-scan' and its parser class [org.springframework.contex ...
- oracle适配器连接不上解决方案
Oracle适配器连接不上解决方案 作者:Vashon oracle 的Developer连接不上报错:listener does not currently know of SID given in ...
- oss图片上传失败
在生产上跑的正常代码,新搭了个测试环境,发现oss上传失败! 开始分析oss是否有以各种类似于白名单的功能,不认识测试域名导致的...结果不是! 改变访问类型 因为oss节点Endpoint是在杭州, ...
- SAP成都研究院姚瑶:软件质量保证工作的变迁
大家好,我是来自SAP成都研究院Revenue Cloud 团队的质量工程师 , yoyo.很高兴可以和大家分享我个人的工作体会.每个团队都有QE(Quality Engineer), 相信大家对QE ...
- 禁用DRM
10G: alter system set "_gc_policy_time"=0 scope=spfile sid='*'; alter system set "_gc ...
- 在proe模型文件里面存储用户数据
存储外部数据 author:visualsan 2014.2 上海 1.简介 利用外部数据存储外部接口,可以在模型文件里面尺寸用户自定义数据.在模型保存时数据自动存储,在模型载入时数据自动载入.外部数 ...