I always think that the question solutions on codeforces are novel. This is no exception,
The maximum number of 10 ^ 5 is 10 ^ 5 operations. Each time a number is added or deleted, only the number of elements that have been added can be added, output reason (or, already added ). When deleting: the output is deleted successfully or has not been added.
This question does not seem simple. After analysis, I found that for a number as large as 10 ^ 5, there can only be a maximum of seven forms of power product of different prime numbers, that is to say, there is no common prime factor. In this way, we mark the prime factor of each number. When inserting, We can query whether the prime factor of the inserted number has appeared.
The time complexity of this idea is O (10 ^ 5*7), which can be solved.
However, one of them needs to output the first few elements, which also needs to be marked. OK. For the code, see the following:
# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; # define n Limit 10int prime [N]; bool isprime [n + 1]; int size; void getprime () {size = 0; memset (isprime, true, sizeof (isprime); int I; for (I = 2; I <= n/2; I ++) {If (isprime [I]) // I is a prime number for (Int J = I + I; j <= N; j + = I) isprime [J] = 0 ;}for (I = 2; I <= N; I ++) {If (isprime [I]) prime [size ++] = I ;}} bool active [100010]; int data [100010] [10]; int count [100010]; // count the jint main () {// cout <2*3*5*7*11*13*17; getprime (); for (INT I = 0; I <size; I ++) {for (Int J = prime [I]; j <100010; j + = prime [I]) data [J] [++ data [J] [0] = prime [I];} int n, m; scanf ("% d", & N, & M); memset (active, 0, sizeof (active); memset (count, 0, sizeof (count); char C [2]; int T; for (INT I = 0; I <m; I ++) {scanf ("% S % d", C, & T ); if (C [0] = '+') {// 1 success 2 already on 3 conflict if (acti Ve [T]) printf ("already on \ n"); else {bool B = 0; For (Int J = 1; j <= data [T] [0]; j ++) if (count [DATA [T] [J]) {printf ("conflict with % d \ n ", count [DATA [T] [J]); B = 1; break;} If (! B) {for (Int J = 1; j <= data [T] [0]; j ++) Count [DATA [T] [J] = T; printf ("Success \ n"); active [T] = 1 ;}} else // -- {If (! Active [T]) printf ("already off \ n"); else {for (Int J = 1; j <= data [T] [0]; j ++) count [DATA [T] [J] = 0; printf ("Success \ n"); active [T] = 0 ;}} return 0 ;}