Codeforces 155D(分解质因子+预处理)

本文介绍了一个关于质因数预处理与灯状态管理的问题。通过预处理1到n的所有数的质因子,在每次操作时判断当前数的质因子是否已出现来决定灯的状态。实现了开灯、关灯及冲突检测等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

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
题目题意:总共n盏灯,有如下处理:

+ num 表示打开num灯 三种情况 

1:灯已经打开

2:存在某一个s灯,s和num存在公因子,不能打开

3:成功打开

- num表示熄灭num灯 2种情况

1:灯已经熄灭

2:成功熄灭

题目分析:不能发现最难的操作是+ num的第二种情况,判断是否存在一个数与num有公共因子。我们预处理去1到n的所有数的质因子,每次开灯的时候去判断一下这个num的质因子在前面是否出现过,出现就不行,没有就可以。

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;

const int maxn=1e5+100;
int prime[maxn],cnt;
bool vis[maxn];
int ok[maxn],factor[maxn];
vector<int> vec[maxn];
void get_prime()//素数打表
{
    memset (vis,true,sizeof (vis));
    vis[1]=false;
    for (int i=2;i<maxn;i++) {
        if (vis[i]) prime[cnt++]=i;
        for (int j=0;j<cnt&&i*prime[j]<maxn;j++) {
            vis[i*prime[j]]=false;
            if (i%prime[j]==0) break;
        }
    }
}
void get_factor(int n)//分解质因子
{
    if (n==1) return vec[n].push_back(1);
    int m=n;
    for (int i=0;i<cnt&&prime[i]*prime[i]<=n;i++) {
        if (n%prime[i]==0) {
            vec[m].push_back(prime[i]);
            while (n%prime[i]==0) {
                n=n/prime[i];
            }
        }
    }
    if (n!=1) vec[m].push_back(n);
}
int main()
{
    get_prime();
    int n,m;
    while (scanf("%d%d",&n,&m)!=EOF) {
        for (int i=1;i<=n;i++) {
            get_factor(i);
            ok[i]=factor[i]=0;
        }
        while (m--) {
            char str[5];int num;
            scanf("%s%d",str,&num);
            if (str[0]=='-') {
                if (ok[num]==0) {
                    puts("Already off");
                }
                else {
                    puts("Success");
                    ok[num]=0;//记得删除这个数的因子
                    for (int i=0;i<vec[num].size();i++) {
                        factor[vec[num][i]]=0;
                    }
                }
            }
            else if (str[0]=='+') {
                if (ok[num]==1) {
                    puts("Already on");
                }
                else {
                    bool flag=false;
                    for (int i=0;i<vec[num].size();i++) {
                        if (factor[vec[num][i]]!=0) {
                            printf("Conflict with %d\n",factor[vec[num][i]]);
                            flag=true;
                            break;
                        }
                    }
                    if (!flag) {
                        puts("Success");
                        ok[num]=1;
                        for (int i=0;i<vec[num].size();i++) {
                            factor[vec[num][i]]=num;
                        }
                    }
                }
            }
        }
    }
    return 0;
}





















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值