Codeforces Round #563 (Div. 2)C

C. Ehab and a Special Coloring Problem

题目链接:http://codeforces.com/contest/1174/problem/C

题目

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.

题意

给你一个数,让你输出长度为n-1的数组,这个数组的起始下标从2开始,使每组任意下标互质的两个数所对应的值都互质。

思路

由于范围在10^5,故可以打素数表,遇到2的倍数打印1,遇到3的倍数打印2,遇到5的倍数打印3...遇到质数的倍数打印该质数在质数表中的位置即可。

//
// 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;
}

猜你喜欢

转载自www.cnblogs.com/Vampire6/p/10990359.html