CF776B Sherlock and his girlfriend

Posted henry-1202

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF776B Sherlock and his girlfriend相关的知识,希望对你有一定的参考价值。

题目地址

题目链接

题解

这题很有意思啊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
", mx);
    for(int i = 1; i < n; ++i) printf("%d ", ans[i]);
    return 0;
}

以上是关于CF776B Sherlock and his girlfriend的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 776B Sherlock and his girlfriend

一本通1623Sherlock and His Girlfriend

Codeforces_776B: Sherlock and his girlfriend(素数筛)

CF1179B Tolik and His Uncle

CF-1362B. Johnny and His Hobbies

CF-1362B. Johnny and His Hobbies