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.
The only line contains the integer n (2≤n≤105).
Print n−1 integers, a2, a3, …, an (1≤ai≤n).
If there are multiple solutions, print any of them.
4
1 2 1
3
2 1
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.
素数就一定是其他数的因子,那么就将素数依次设置从1开始依次向上加设置,如果不是素数就赋值为本身最小的因数的值。
用map存储素数的值,再找到不是素数的最小因数的map值给这个数,就完了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
#define maxn 2147483648+30
using namespace std;
ll cmp(ll n){
ll flag=1,i;
if(n==2){
return 1;
}
for(i=2;i<=sqrt(n);i++){
if(n%i==0){
flag=i;break;
}
}
return flag;
}
int main(){
std::ios::sync_with_stdio(false);
ll x,y,i,j;
while(cin>>x){
map<ll,ll>mm;mm.clear();
for(i=2,j=1;i<=x;i++){
y=cmp(i);
if(y==1){
mm[i]=j;
cout<<j<<" ";
j++;
}//用map存储素数的值
else{
cout<<mm[y]<<" ";
}//找到不是素数的最小因数的map值给这个数
}
cout<<endl;
}
}