原文链接:传送门
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.
Examples
input
4
output
1 2 1
input
3
output
2 1
Note
In the first example, notice that 3 and 4 are coprime, so a3≠a4. Also, notice that a=[1,2,3] satisfies the first condition, but it’s not a correct answer because its maximal value is 3.
思路:素数筛
代码:
#include"iostream"
#include"algorithm"
#include"cstring"
using namespace std;
long long a[2000006],n;
int main(){
cin>>n;
long long flag = 1;
memset(a,0,sizeof(a));
for(int i=2;i<=n;i++){
if(a[i]==0){
a[i]=flag;
for(int j=i*2;j<=n;j+=i)
if(a[j]==0)
a[j]=flag;
flag++;
}
cout<<a[i];
if(i<n)
cout<<" ";
}
return 0;
}