Codeforces Round #563 (Div. 2) 1174C. Ehab and a Special Coloring Problem

C. Ehab and a Special Coloring Problem

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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

inputCopy

4

outputCopy

1 2 1 

inputCopy

3

outputCopy

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.

思路:

素数就一定是其他数的因子,那么就将素数依次设置从1开始依次向上加设置,如果不是素数就赋值为本身最小的因数的值。

操作:

用map存储素数的值,再找到不是素数的最小因数的map值给这个数,就完了。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#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;
	 }
}

发布了148 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/90761485