[codeforces 1332B] Composite Coloring 不超过11种颜色的证明

本文详细解析了Codeforces Round #630(Div.2)中的B题“Composite Coloring”,通过数学证明,展示了如何使用不超过11种颜色对合数进行有效染色,适用于算法竞赛爱好者和初学者。

Codeforces Round #630 (Div. 2)   比赛人数12012

[codeforces 1332B]  Composite Coloring    不超过11种颜色的证明

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1332/problem/B

ProblemLangVerdictTimeMemory
B - Composite Coloring GNU C++11Accepted31 ms0 KB

弄明白了不超过11种颜色,该题也就自然会编了。

32*32=1024
多举几个数据,可以发现,1000以内的合数,一定可以被
2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
上述某个数整除

将上述数据进行因式分解
2,3,4=2*2,5,6=2*3,7,8=2*2*2,9=3*3,10=2*5,11,12=2*2*3,13,14=2*7,15=3*5,16=2*2*2*2,
17,18=2*3*3,19,20=2*2*5,21=3*7,22=2*11,23,24=2*2*2*3,25=5*5,26=2*13,27=3*3*3,
28=2*2*7,29,30=2*3*5,31

发现,包含因子
2,3,5,7,11,13,19,23,29,31
只有10个数据,10种颜色已够用,更不用说11种颜色了。

样例对应的输入输出数据如下

Input:
3
3
6 10 15
2
4 9
23
437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961
Output:
2
1 1 2
2
1 2
10
8 2 3 1 2 7 1 2 2 3 7 3 4 4 5 5 6 6 7 7 8 9 10

AC代码如下

#include <cstdio>
#include <algorithm>
#define maxn 1010
using namespace std;
struct node{
	int seq,v,color,d;//seq记录序列,v记录值,color记录颜色,d记录整除因子
}a[maxn];
int cmp1(node a,node b){
	return a.d<b.d;
}
int cmp2(node a,node b){
	return a.seq<b.seq;
}
int main(){
	int t,n,i,tot,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=1;i<=n;i++)scanf("%d",&a[i].v),a[i].seq=i;
		for(i=1;i<=n;i++)
			for(j=2;j<=31;j++)
				if(a[i].v%j==0){
					a[i].d=j;//寻找a[i].v的第一个整除因子j
					break;
				}
		sort(a+1,a+1+n,cmp1);//按第一个整除因子,自小到大排序
		tot=0;
		for(i=1;i<=n;i++)//染色
			if(a[i].d==a[i-1].d)a[i].color=tot;
			else tot++,a[i].color=tot;
		sort(a+1,a+1+n,cmp2);//序列,自小到大排序
		printf("%d\n",tot);
		for(i=1;i<n;i++)printf("%d ",a[i].color);
		printf("%d\n",a[n].color);
	}
	return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值