夏洛克和他的女朋友(

# 题意

n个珠宝,第 i 个珠宝的值是i+1,也就是珠宝的价值是2,3,4,......,n+1,

给这些珠宝染色,使得珠宝的价格是另一个珠宝的质因子的时候,两只珠宝的颜色不同

要求颜色数目最少,输出颜色数目和染色方案

n∈[1,1e5]

# 题解

二分图,所有质数放在一边,合数在另一边,所以最多只需要2种颜色

当二分图中没有边的时候只需要1种颜色,有边的时候需要2种

即存在合数的时候有边,无合数的时候无边

即当n>=3的时候必定存在合数。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 int p[N],cnt;
 5 bool st[N];
 6 void get_primes(int n){
 7     for(int i=2;i<=n;i++){
 8         if(!st[i]) p[cnt++]=i;
 9         for(int j=0;p[j]<=n/i;j++){
10             st[p[j]*i]=true;
11             if(i%p[j]==0) break;
12         }
13     }
14 }
15 int main(){
16     int n;
17     cin>>n;
18     get_primes(n+1);
19     if(n>2) puts("2");
20     else puts("1");
21 
22     for(int i=2;i<=n+1;i++){
23         if(st[i])
24             cout<<'2'<<' ';
25         else cout<<'1'<<' ';
26     }
27 }

猜你喜欢

转载自www.cnblogs.com/hhyx/p/12602690.html