codeforces 154B 素筛 机器激活
来源:互联网 发布:创建bat文件还原源码 编辑:程序博客网 时间:2024/06/05 11:03
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”.
题目大意:
有n个电子碰撞机,编号为1~n,初始状态所有的碰撞机都没有激活。然后需要你编写程序对下面的m次操作进行判断。’+’操作代表激活,’-’操作代表关闭激活,后面跟的数字就是操作的机器号。对机器i的操作规则如下所述:
1.当操作为’+’时: 1) 当i号机器已经激活时,则输出 Already on 2) 当i号机器没有激活时,则如果有一个已经激活的j号机器,并且i与j不互质的话即gcd(i,j)!=1时,那么提示 Conflict with j。 3) 否则机器正常激活,输出Success2.当操作为’-’时: 1) 当i号机器已经激活时,则关闭激活成功,输出Success 2) 当i号机器没有激活时,则不需要关闭,则输出Already off
技巧总结:
对于互质判断,我们应当首先想到他们是否有公共的质因子,因为每一个合数都能表示成某些素数的连乘,这也是筛选法找出素数的思想。
#include <bits/stdc++.h>using namespace std;vector<int> prime;int is_prime[101101];vector<int> pp[101110];int kaiguan[101110];int coun[101100];void init(){ for(int i=2;i<101010;i++) { if(!is_prime[i]) { prime.push_back(i); for(int j=i+i;j<101010;j+=i) { is_prime[j]=1; } } }}void del(int t){ int num=t; for(int i=0;prime[i]<=sqrt(num);i++) { if(num%prime[i]==0) { pp[t].push_back(prime[i]); while(num%prime[i]==0) { num/=prime[i]; } } } if(num>1) pp[t].push_back(num);}void Active(int n){ if(kaiguan[n]) printf ( "Already on\n" ); else { for(int i=0;i<pp[n].size();i++) { if(coun[pp[n][i]]!=0) { printf ( "Conflict with %d\n" , coun[pp[n][i]] ); return ; } } kaiguan[n]=1; for(int i=0;i<pp[n].size();i++) { coun[pp[n][i]]=n; } printf("Success\n"); }}void Deactive(int num){ if(kaiguan[num]==0) printf ( "Already off\n" ); else { kaiguan[num]=0; for(int i=0;i<pp[num].size();i++) { coun[pp[num][i]]=0; } printf("Success\n"); }}int main(){ init(); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { del(i); } char op; int num; while(m--) { cin>>op>>num; if(op=='+') Active(num); else Deactive(num); }}
- codeforces 154B 素筛 机器激活
- CodeForces 154B- Colliders
- Codeforces 154B Colliders
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- codeforces#154_div2_B (253B) Physics Practical
- CodeForces 154B——Colliders
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- Matlab 2015b 激活失效后,重新激活
- 机器学习: 激活函数的认识
- 机器学习--神经网络算法系列--激活函数
- 机器学习中的常用激活函数
- codeforces 134B
- codeforces#98 b
- codeforces 105 div2 B
- Activity生命周期
- 蓝桥杯省赛感想
- 用创建图形路径的方式去绘制图形(CGMutablePathRef)
- RTP概述
- 单例模式
- codeforces 154B 素筛 机器激活
- 建造者模式
- 在Putty/winSCP连接拒绝及Vim(插入模式)下,无法输入,没有反应。
- live555的安装 RTSP点播消息流程实例(客户端:VLC, RTSP服务器:LIVE555 Media Server)
- android 上传aar到私有maven服务器
- 两种方法位运算实现加法器
- Python range函数
- 剑指offer-面试题19-二叉树的镜像
- android文件存储