一本通1623Sherlock and His Girlfriend

1623:Sherlock and His Girlfriend

时间限制: 1000 ms         内存限制: 524288 KB

【题目描述】

原题来自:Codeforces Round #400 B.

Sherlock 有了一个新女友(这太不像他了!)。情人节到了,他想送给女友一些珠宝当做礼物。

他买了 n 件珠宝。第 i 件的价值是 i+1。那就是说,珠宝的价值分别为 2,3,4,,n+1

Watson 挑战 Sherlock,让他给这些珠宝染色,使得一件珠宝的价格是另一件的质因子时,两件珠宝的颜色不同。并且,Watson 要求他最小化颜色的使用数。

请帮助 Sherlock 完成这个简单的任务。

【输入】

只有一行一个整数 n,表示珠宝件数。

【输出】

第一行一个整数 k,表示最少的染色数;

第二行 n 个整数,表示第 1 到第 n 件珠宝被染成的颜色。若有多种答案,输出任意一种。

【输入样例】

3

【输出样例】

2
1 1 2

【提示】

样例输入 2

4

样例输出 2

2
2 1 1 2

样例说明

因为 2 是 4 的一个质因子,因此第一件珠宝与第三件珠宝的颜色必须不同。

数据范围与提示:

对于全部数据,1≤n≤105 。

 

sol:第一眼看上去很难的样子(然后发现是质因数),于是可知最多分成两类,一类质数,一类非质数

Ps:n+1=2和n+1=3要特判下,因为 2,3两个数可以放一起,于是只有一类

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=100005;
int n;
bool Bo[N];
int Prime[N];
inline void Get_Prime()
{
    int i,j;
    for(i=2;i<=n;i++)
    {
        if(!Bo[i]) Prime[++*Prime]=i;
        for(j=1;j<=*Prime&&Prime[j]*i<=n;j++)
        {
            Bo[Prime[j]*i]=1;
            if(i%Prime[j]==0) break;
        }
    }
    return;
}
int main()
{
    int i;
    n=read()+1;
    if(n==2) return 0*printf("1\n1\n");
    if(n==3) return 0*printf("1\n1 1\n");
    Get_Prime();
    puts("2");
    for(i=2;i<=n;i++)
    {
        W((Bo[i])?(2):(1));
    }
    return 0;
}
View Code

 

posted @ 2019-02-24 12:21  yccdu  阅读(365)  评论(1编辑  收藏  举报