题目链接:https://www.acwing.com/problem/content/description/1295/
思路:质数填1 ,合数填2
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int primes[N], cnt; // primes[]存储所有素数
int col[N],idx;
void get_primes(int n)
{
for (int i = 2; i <= n; i ++ )
{
if (!col[i])
{
primes[cnt ++ ] = i;
col[i]=1;
}
for (int j = 0; primes[j] <= n / i; j ++ )
{
col[primes[j] * i] = 2;
if (i % primes[j] == 0) break;
}
}
}
int main()
{
int n;
scanf("%d",&n);
get_primes(n+1);
if(n>2) printf("%d\n",2);
else printf("%d\n",1);
for(int i=2;i<=n+1;i++)
{
if(i!=n+1) printf("%d ",col[i]);
else printf("%d\n",col[i]);
}
}