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

原文链接:传送门
    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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值