微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。