题目地址

题目链接

题解

这题很有意思啊qwq。本来是写算出每个数的质约数的,然后写到一半发现,质约数互相不影响,有质约数的数肯定是合数。
所以合数染一种色,质数染一种色就好

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;

#define N 100010
#define ll long long 
#define inf 2147483647
int p[N], n, a[N], vis[N];
int ans[N];

int main() {
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
    scanf("%d", &n);
    int cnt = 0; ++n;
    for(int i = 2; i <= n; ++i) {
        if(!vis[i]) p[++cnt] = i;
        for(int j = 1; j <= cnt && i * p[j] <= n; ++j) {
            vis[i * p[j]] = 1;
            if(i % p[j] == 0) break;
        }
    }
    int mx = 1;
    for(int i = 2; i <= n; ++i) {
        if(!vis[i]) ans[i - 1] = 1;
        else ans[i - 1] = 2, mx = 2;
    }
    printf("%d\n", mx);
    for(int i = 1; i < n; ++i) printf("%d ", ans[i]);
    return 0;
}