C. Ehab and a Special Coloring Problem

You’re given an integer ?n. For every integer ?i from 22 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 11.

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

Output
Print ?−1n−1 integers, ?2a2, ?3a3, ……, ??an (1≤??≤?1≤ai≤n).

If there are multiple solutions, print any of them.

Examples
inputCopy
4
outputCopy
1 2 1
inputCopy
3
outputCopy
2 1
Note
In the first example, notice that 33 and 44 are coprime, so ?3≠?4a3≠a4. Also, notice that ?=[1,2,3]a=[1,2,3] satisfies the first condition, but it’s not a correct answer because its maximal value is 33.

题意:给一个正整数n,要求构造一个数组a[],数组的下标i从2到n。对于任意i, j (i != j),如果i和j互质的话,那么要求a[i]和a[j]不同。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+100;
int a[maxn];
bool prime[maxn];
void init()              //埃式筛素
{
    memset(prime,true,sizeof(prime));
    prime[0] = prime[1] = false;
    for(int i=2; i<maxn; i++)
    {
        if(prime[i])
        {
            for(int j=i*2; j<maxn; j+=i)
            {
                prime[j] = false;
            }
        }
    }
}
 
int main()
{
    init();
    int n;
    cin>>n;
    int cnt = 1;
    int i;
    for(i=2; i<=n; i++)
    {
        if(prime[i])                //因为每个素数跟其他素数都是互质的,所以每个素数都需要分配不同的元素。
            a[i] = cnt++;
        else if(i%2==0)               // 偶数的话分配1就可以了
            a[i] = 1;
        else
        {
            for(int j=2; j<i; j++)        //其余的情况找到第一个与它本身不互质
            {
                if(__gcd(i,j)!=1)
                {
                    a[i] = a[j];
                    break;
                }
            }
        }
    }
    for(i=2; i<=n; i++)
    {
        if(i==2)
            printf("%d",a[i]);
        else
            printf(" %d",a[i]);
    }
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值