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