Codeforces Round #109 (Div. 1) B Number Theory
// If two numbers are not mutually dependent, they must have a prime number factor.
// Store all prime numbers of 2 to n using vec
// Whether to read the number of factors stored in vis
// You only need to maintain the vis array during processing.
# Include
# Include
# Include
# Include
Using namespace std;
Const int maxn = 100010;
Vector Vec [maxn];
Int isp [maxn];
Int vis [maxn];
Int num [maxn];
Void set ()
{
Memset (isp, 0, sizeof (isp ));
Isp [1] = 1;
For (int I = 2; I <maxn; I + = 2)
{
Isp [I] = 1;
Vec [I]. push_back (2 );
}
For (int I = 3; I <maxn; I ++)
{
If (isp [I]) continue;
For (int j = I; j <maxn; j + = I)
{
Isp [j] = 1;
Vec [j]. push_back (I );
}
}
}
Int main ()
{
// Freopen ("input.txt", "r", stdin );
Set ();
Int n, m;
Char ch; int I;
While (~ Scanf ("% d", & n, & m ))
{
Memset (num, 0, sizeof (num ));
Memset (vis, 0, sizeof (vis ));
While (m --)
{
Scanf ("% c % d", & ch, & ch, & I );
// Cout < If (ch = '-')
{
// Cout < If (! Num [I]) puts ("Already off ");
Else
{
Puts ("Success ");
Num [I] = 0;
For (int j = 0; j <vec [I]. size (); j ++)
Vis [vec [I] [j] = 0;
}
}
If (ch = '+ ')
{
If (num [I]) puts ("Already on ");
Else
{
Int flag = 0;
For (int j = 0; j <vec [I]. size (); j ++)
{
Int t = vec [I] [j];
If (vis [t])
{
Printf ("Conflict with % d \ n", vis [t]);
Flag = 1;
Break;
}
}
If (! Flag)
{
Puts ("Success ");
// Cout < Num [I] = 1;
For (int j = 0; j <vec [I]. size (); j ++)
Vis [vec [I] [j] = I;
}
}
}
}
}
Return 0;
}