Codeforces Round #563 (Div. 2)C

C. Ehab and a Special Coloring Problem

Topic links: http://codeforces.com/contest/1174/problem/C

topic

You're given an integer n. For every integer i from 2 to n, assign a positive integer ai such that the following conditions hold:

For any pair of integers (i,j), if i and j are coprime, ai≠aj

The maximal value of all ai should be minimized (that is, as small as possible).

A pair of integers is called coprime if their greatest common divisor is 1.

input

The only line contains the integer n (2≤n≤105).

output

Print n−1
integers, a2, a3, …, an (1≤ai≤n).

If there are multiple solutions, print any of them.

The meaning of problems

Give you a number, you let the output array of length n-1, but starting from the index of the array 2 is started, arbitrary coprime index value corresponding to each set of two numbers that are prime.

Thinking

Since the range of 10 ^ 5, it is possible to hit the prime number table, a print encountered a multiple of 2, 3 encounter multiple print 2, 3 encounter multiple 5 ... print quality prints encounter multiple of the number of the prime quality table in location.

 

//
// Created by hjy on 19-6-4.
//

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int n;
bool prime(int m)//简单判断素数
{

    for(int i=2;i<=sqrt(m);i++)
    {
        if(m%i==0)
            return false;
    }
    return true;
}
int result[maxn]={0};
void cun()///存入表中
{
    int op=0;
    for(int i=2;i<=maxn;i++)
    {

       if(prime(i))
       {
           op++;
           //cout<<"i="<<i<<endl;
           for(int j=i;j<=maxn;j+=i)
           {
               //cout<<"j="<<j<<endl;
               result[j]=op;
               //cout<<"result[j]="<<result[j]<<endl;

           }
       }
    }
}
int main()
{

    int n;
    while(cin>>n) {
        cun();
      for(int i=2;i<=n;i++)
          cout<<result[i]<<' ';
      cout<<endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/10990359.html
Recommended