[AcWing 1293] 夏洛克和他的女朋友

image
image

构造


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1e6 + 10;

int n;
vector<int> primes;
bool st[N];

void get_primes(int x)
{
    for (int i = 2; i <= x; i ++) {
        if (!st[i])
            primes.push_back(i);
        for (auto p : primes) {
            if (p * i > x)
                break;
            st[p * i] = true;
            if (i % p == 0)
                break;
        }
    }
}

void solve()
{
    get_primes(1e5 + 10);
    cin >> n;
    if (n <= 2)
        cout << "1" << endl;
    else
        cout << "2" << endl;
    for (int i = 2; i <= n + 1; i ++)
        if (!st[i])
            cout << "1 ";
        else
            cout << "2 ";
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}

  1. 二分图
    依据是质数还是合数分类,当是质数时,染为 \(1\),当是合数时,染为 \(2\)
posted @ 2022-08-03 23:05  wKingYu  阅读(19)  评论(0编辑  收藏  举报