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
| Problem | Lang | Verdict | Time | Memory |
|---|---|---|---|---|
| B - Composite Coloring | GNU C++11 | Accepted | 31 ms | 0 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;
}
本文详细解析了Codeforces Round #630(Div.2)中的B题“Composite Coloring”,通过数学证明,展示了如何使用不超过11种颜色对合数进行有效染色,适用于算法竞赛爱好者和初学者。

6600

被折叠的 条评论
为什么被折叠?



