Codeforces 155D Colliders【思维+模拟】

75 篇文章 0 订阅

D. Colliders
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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


题目大意:


有【1~n】这些数,现在有m个操作。

+x表示向机器中放入一个数x

-x表示在机器中拿出一个数x

这个机器整个过程都需要保证,在机器中任取两个数的gcd都是1、对于每一个操作,输出一个结果


思路:


我们分类讨论即可:

①当对于+x操作,如果机器中如果存在x这个数了,直接输出。否则判断能否放进去,判断放进去的条件我们O(sqrt(x))去判定就行。

如果可以放进去,我们放进去x的因子就行了。

对于整体时间复杂度为O(nlogn);

②当对于-x操作,如果机器中不存在这个数,直接输出。否则将其因子全部踢出 。


过程用vis【i】判定i这个数是否在机器中。

过程用s【i】=x表示i这个因子是属于数字x的。


那么如果我们加入一个新的数,其因子temp,s【temp】>0的话,就说明机器中有和其冲突的数字存在。

细节稍微有些多,谨慎一点就行了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
map<int,int>s;
int vis[150000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        s.clear();
        memset(vis,0,sizeof(vis));
        while(m--)
        {
            char op[15];scanf("%s",op);
            int x;scanf("%d",&x);
            if(op[0]=='+')
            {
                if(vis[x]==1)
                {
                    printf("Already on\n");
                }
                else
                {
                    int temp=0;
                    for(int i=1;i<=sqrt(x);i++)
                    {
                        if(x%i==0)
                        {
                            if(s[i]>0)temp=s[i];
                            if(s[x/i]>0)temp=s[x/i];
                        }
                    }
                    if(temp>0)
                    {
                        printf("Conflict with %d\n",temp);
                    }
                    else
                    {

                        vis[x]=1;
                        printf("Success\n");
                        for(int i=2;i<=sqrt(x);i++)
                        {
                            if(x%i==0)
                            {
                                s[i]=x;
                                s[x/i]=x;
                            }
                        }
                        if(x!=1)s[x]=x;
                    }
                }
            }
            else
            {
                if(vis[x]==0)
                {
                    printf("Already off\n");
                }
                else
                {
                    vis[x]=0;
                    printf("Success\n");
                    for(int i=2;i<=sqrt(x);i++)
                    {
                        if(x%i==0)s[i]=0,s[x/i]=0;
                    }
                    s[x]=0;
                }
            }
        }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值