洛谷 CF776B Sherlock and his girlfriend

CF776B Sherlock and his girlfriend

乍一看貌似有点难。

仔细想想会发现这道题目有点, 首先,所有的素数都可以直接染成1,因为他们之间不存在质因子的关系。
然后,所有的合数可以直接染成2,,,
为什么呢?
因为一旦某个数字被染成了2,就意味着这个数是非素数(也就是合数),那么他就不可能再是其他某个数的质因子了,这也就意味着不可能用到第三种颜色。


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
//Mystery_Sky
//
#define M 1000100
int prime[M], check[M], tot;
int color[M];
int n, ans;

inline void get_prime()
{
	check[0] = check[1] = 1;
	for(int i = 2; i <= 100001; i++) {
		if(!check[i]) prime[++tot] = i;
		for(int j = 1; j <= tot; j++) {
			if(i * prime[j] > 100001) break;
			check[i * prime[j]] = 1;
			if(i % prime[j] == 0) break; 
		}
	} 
}

int main() {
	get_prime();
	scanf("%d", &n);
	n >= 3 ? printf("2\n") : printf("1\n");
	for(int i = 1; i <= n; i++) {
		if(check[i+1]) printf("2 ");
		else printf("1 ");
	}
	return 0;
}
posted @ 2019-07-11 20:29  Mystery_Sky  阅读(151)  评论(0编辑  收藏  举报