Two numbers if it's not coprime, then they must have a prime factor.
Use VEC to deposit all prime-number factors from 2 to n
Whether the number of the factor is read in by the Vis
Only need to maintain the VIS array on the line when processing
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace Std;
const int MAXN = 100010;
Vector<int> 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%d", &n, &m))
{
memset (num, 0, sizeof (num));
memset (Vis, 0, sizeof (VIS));
while (m--)
{
scanf ("%c%c%d", &ch, &ch, &i);
cout<<ch<<endl;
if (ch = = '-')
{
cout<<i<<endl;
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<<i<<endl;
Num[i] = 1;
for (int j = 0;j < Vec[i].size (); j + +)
VIS[VEC[I][J]] = i;
}
}
}
}
}
return 0;
}
Codeforces Round #109 (Div. 1) B number theory