zoukankan      html  css  js  c++  java
  • B. Sherlock and his girlfriend

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry.

    He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, ... n + 1.

    Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don't have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.

    Help Sherlock complete this trivial task.

    Input

    The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.

    Output

    The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints.

    The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price.

    If there are multiple ways to color the pieces using k colors, you can output any of them.

    Examples
    input
    Copy
    3
    output
    Copy
    2
    1 1 2
    input
    Copy
    4
    output
    Copy
    2
    2 1 1 2
    Note

    In the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.

    In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct.

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    #include <vector>
    #include <set>
    #include <queue>
    #include <stack>
    #include <cmath>
    using namespace std;
    #define mem(s,t) memset(s,t,sizeof(s))
    #define pq priority_queue
    #define pb push_back
    #define fi first
    #define se second
    #define ac return 0;
    #define ll long long
    #define cin2(a,n,m)     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
    #define rep_(n,m)  for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
    #define rep(n) for(int i=1;i<=n;i++)
    #define test(xxx) cout<<"  Test  " <<" "<<xxx<<endl;
    #define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
    #define lc now<<1
    #define rc now<<1|1
    #define ls now<<1,l,mid
    #define rs now<<1|1,mid+1,r
    #define half no[now].l+((no[now].r-no[now].l)>>1)
    #define ll long long
    #define inf 0x3f3f3f3f
    #define mod 1000000007
    const int mxn = 1e5+10;
    int n,m,k,ans,col,t,flag,x,y,vis[mxn],pim[mxn];
    ll a[mxn],sum[mxn],cnt;
    vector<int>G[mxn],v;
    string str , ch ,s1 ,s2;
    //pair <int,int> pa[mxn];
    bool cmp(pair<int,int>x,pair<int,int>y){    return x.first>y.first;}
    void init()
    {
        k = 0;
        memset(vis,0,sizeof(vis));
        for(int i=2;i<=n+1;i++)
        {
            if(!vis[i]) {pim[k++] = i;}
            for(int j=0;j<k && i*pim[j]<=n+1;j++)
            {
                vis[ i*pim[j] ] = 1 ;
                if(i%pim[j]==0) break;
            }
        }
    }
    int main()
    {
        TLE;
        while(cin>>n)
        {
            init();
            if(n==1)
                cout<<1<<endl<<1<<endl;
            else if(n==2)
                cout<<1<<endl<<"1 1"<<endl;
            else
            {
                cout<<2<<endl;
                k = 0;cnt = 2;
                for(int i=2;i<=n+1;i++)
                {
                    if(pim[k]==i)
                    {
                        cout<<1<<" ";
                        k++;
                    }
                    else
                        cout<<cnt<<" ";
                }
                cout<<endl;
     
            }
        }
        return 0;
    }
    所遇皆星河
  • 相关阅读:
    纯JS制作选项卡--JavaScript实例集锦(初学)
    超级简单实用的CSS3动画,增添网页效果
    3种方法实现图片瀑布流的效果(纯JS,Jquery,CSS)
    CSS动画小结
    遗忘比死亡更可怕
    matplotlib笔记2
    matplotlib笔记1
    pandas数据结构之Panel笔记
    pandas数据结构之Series笔记
    ndarray笔记续
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11884693.html
Copyright © 2011-2022 走看看