codeforces 154B 素筛 机器激活

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) 否则机器正常激活,输出Success

2.当操作为’-’时:

    1) 当i号机器已经激活时,则关闭激活成功,输出Success

    2) 当i号机器没有激活时,则不需要关闭,则输出Already off

技巧总结:
对于互质判断,我们应当首先想到他们是否有公共的质因子,因为每一个合数都能表示成某些素数的连乘,这也是筛选法找出素数的思想。

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
map<int,int> mp; int vis[100010];

void add(int x)
{
	vector<int> v; v.clear();
	int xx = x;
	for(int i = 2; i * i <= xx; i++)
		if(x%i == 0)
		{
			v.push_back(i);
			while(xx % i == 0) xx/=i;
		}
	if(xx!=1) v.push_back(xx);
	f(i,0,v.size()) 
		if(vis[v[i]] != 0) {
			printf("Conflict with %d\n",vis[v[i]]);
			return;
		}
	f(i,0,v.size())
		vis[v[i]] = x;
	printf("Success\n");
	mp[x] = 1;
	return ;
}

void del(int x)
{
	vector<int> v; v.clear();
	int xx = x;
	for(int i = 2; i * i <= xx; i++)
		if(x%i == 0)
		{
			v.push_back(i);
			while(xx % i == 0) xx/=i;
		}
	if(xx!=1) v.push_back(xx);
	f(i,0,v.size())
		vis[v[i]] = 0;
	printf("Success\n");
	mp[x] = 0;
	return ;
}

int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    ff(i,1,m)
    {
    	char ch;int x;
    	cin >> ch >> x;
    	if(ch == '+') {
    		if(mp[x] != 0) printf("Already on\n");
    		else add(x);
    	}
    	else{
    		if(mp[x] == 0) printf("Already off\n");
    		else del(x);
    	}
    	
    }
    
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值