CodeForces 154B——Colliders——筛选素数,模拟标记

B. 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.

Sample test(s)
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

解题思路:

    众所周知,所有的合数都能化成若干个素因子相乘的形式,n不会超过10^5,所以我们可以把10^5以内的素数通过线性筛选,将它们筛选出来,然后用一个二维数组记录1~n的所有机器号的素数因子,因为10^5 < 2^20,所以n的素因子不会超过20个,所以我们很方便的就通过一个二维数组将每个数和它的素因子关联起来(注意,当自身也为素数时,应该把自己也放到属于自己的数组里面)。当要激活一个碰撞机的时候,我们只需要知道有没有和它共素因子的数,若没有则可以激活。那么如何检测有没有和它共素因子的数的机器已经激活了呢?

    我们用一个一维数组记录每一个素数和一个合数的激活对应关系:

    如果6激活了,那么就记录一个2(6),3(6)的对应关系。下次出现2,3为因子的合数时,就可以通过找与它关联的素因子找到2,3并且去判断有没有出现2,3与其他数的对应关系,如果出现了10,它找到了2(6),所以出现了冲突。

当一个机器被成功机会后,我们用一个数组记录它的激活状态,并且把与之关联的素因子都与它建立对应关系。

代码如下:

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 100008
bool is_prime[maxn]; //判断素数标记
bool visit[maxn];    //筛选访问标记
int prime[maxn];     //素数数组
bool active[maxn];   //激活标记
int tool[maxn][20];  //记录一个数的素因子数组
int count[maxn];     //将素数与对应的激活号关联

void select_prime () //筛选素数
{
    is_prime[0] = 0;
    is_prime[1] = 0;
    int cnt = 0;
    for ( int i = 2 ; i < maxn ; i ++ )
        if ( !visit[i] )
        {
            visit[i] = 1;
            if ( is_prime[i] )
            {
                prime[cnt++] = i;
                for ( int j = 2 * i ; j < maxn ; j += i )
                {
                    visit[j] = 1;
                    is_prime[j] = 0;
                }
            }
        }
}

void div_prime ( int n )  //拆分素因子
{
    int ths = 1; //其中0号下标存素因子个数
    for ( int i = 0 ; prime[i] <= n / 2 ; i ++ )
    {
        if ( n % prime[i] == 0 ) //每找到一个素数就存起来
            tool[n][ths++] = prime[i];
    }
    if ( is_prime[n] ) tool[n][ths++] = n;
    tool[n][0] = ths - 1;  //其中0号下标存素因子个数
}

void Active ( int num )  //激活操作
{
    if ( active[num] ) printf ( "Already on\n" );
    else
    {
        for ( int i = 1 ; i <= tool[num][0] ; i ++ ) //寻找冲突即自己的素因子出现在已经激活的
        {                       		     //合数或者素数中
            if ( count[tool[num][i]] != 0 ) 
            {
                printf ( "Conflict with %d\n" , count[tool[num][i]] );
                return ;
            }
        }
        active[num] = 1;
        for ( int i = 1 ; i <= tool[num][0] ; i ++ )
            count[tool[num][i]] = num;
        printf ( "Success\n" );
    }
}

void Deactive ( int num )  //关闭激活操作
{
    if ( !active[num] ) printf ( "Already off\n" );
    else
    {
        active[num] = 0;
        for ( int i = 1 ; i <= tool[num][0] ; i ++ )
            count[tool[num][i]] = 0;
        printf ( "Success\n" );
    }
}

int main()
{
    int n,m,num;
    char op;
    memset ( active , 0 , sizeof ( active ) );
    memset ( is_prime , 1 , sizeof ( is_prime )) ;  
    memset ( visit , 0 , sizeof ( visit ) );
    memset ( prime , 0 , sizeof ( prime ) );
    memset ( count , 0 , sizeof ( count ) );
    select_prime();
    scanf ( "%d %d" , &n , &m );
        for ( int i = 1 ; i <= n ; i ++ )
            div_prime ( i );
        /*
        for ( int i = 1 ; i <= n ; i ++ )
        {
            cout << i <<"的素因子有" << tool[i][0] << "个: ";
            for ( int j = 1 ; j <= tool[i][0] ; j ++ ) 
                cout << tool[i][j] <<  ' ';
            cout << endl;
        }
        */
        while ( m-- )
        {
            cin >> op >> num;
            if ( op == '+' ) Active ( num );
            else Deactive ( num );
        }
    return 0;
}


技巧总结:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值