其他分享
首页 > 其他分享> > Codeforces Round #630 (Div. 2) B—Composite Coloring

Codeforces Round #630 (Div. 2) B—Composite Coloring

作者:互联网

整理的算法模板:ACM算法模板总结(分类详细版)

 

目录

题目:B. Composite Coloring

题意:

思路:

代码:


题目:B. Composite Coloring

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

A positive integer is called composite if it can be represented as a product of two positive integers, both greater than 11. For example, the following numbers are composite: 66, 44, 120120, 2727. The following numbers aren't: 11, 22, 33, 1717, 9797.

Alice is given a sequence of nn composite numbers a1,a2,…,ana1,a2,…,an.

She wants to choose an integer m≤11m≤11 and color each element one of mm colors from 11 to mm so that:

Note that equal elements can be colored different colors — you just have to choose one of mm colors for each of the indices from 11 to nn.

Alice showed already that if all ai≤1000ai≤1000 then she can always solve the task by choosing some m≤11m≤11.

Help Alice to find the required coloring. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some mm from 11 to 1111.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then the descriptions of the test cases follow.

The first line of the test case contains a single integer nn (1≤n≤10001≤n≤1000) — the amount of numbers in a sequence aa.

The second line of the test case contains nn composite integers a1,a2,…,ana1,a2,…,an (4≤ai≤10004≤ai≤1000).

It is guaranteed that the sum of nn over all test cases doesn't exceed 104104.

Output

For each test case print 22 lines. The first line should contain a single integer mm (1≤m≤111≤m≤11) — the number of used colors. Consider colors to be numbered from 11 to mm. The second line should contain any coloring that satisfies the above conditions. Print nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤m1≤ci≤m), where cici is the color of the ii-th element. If there are multiple solutions then you can print any of them. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some mm from 11 to 1111.

Remember that each color from 11 to mm should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than 11).

 

这道题我觉是这场div2里面比c题就还有意思的题,过的人很多,但是我却没想到;

题意

给这1000个数染色,要求相同颜色的数的 gcd>1 然后颜色的种类数不能超过11,并且假如你用了m种颜色,那么1~m中的任意一种颜色都要至少被使用一次;

思路:

(一个合数的最小质因子一定小于这个合数的根号,所以对于极值1000的根号33以内的质因数有11个;)

首先如果两个数gcd>1,那么他们一定有一个共同的最小质因子;因为平方小于1000的数分解完只有11个质因数,31*31=961;(31是第11个素数)。所以枚举每个数,判断它的最小质因子是谁,然后把最小质因子相同的放在一起即可;

代码:

#include <bits/stdc++.h>
using namespace std;
int a[10005];
int pr[11]={2,3,5,7,11,13,17,19,23,29,31};
int color[10005];//标记每个因子被染成什么色; 
map<int,int> mp;//存放每个位置染什么颜色 
int main()
{
	int t;
	cin >>t;
	int cnt=0;
	while(t--)
	{
		int n;
		cin >>n;
		mp.clear();
		memset(color,0,sizeof color);
		for(int i=0;i<n;i++) cin >>a[i];
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<11;j++)
			{
				if(a[i]%pr[j]==0)
				{
					if(color[j]==0) color[j]=++cnt;//如果当前这个合数可以整除pr[j],那么就判断pr[j]是什么颜色
									//如果pr[j]没有被染色,那么就染之前的颜色+1 
					mp[i+1]=color[j];//记录当前位置的颜色; 
					break;
				}
			}
		}
		cout <<cnt<<endl;
		for(int i=1;i<=n;i++) cout <<mp[i]<<" ";
		cout <<endl;
	}
}

 

标签:630,11,Coloring,Composite,mm,int,colors,test,color
来源: https://blog.csdn.net/weixin_43872728/article/details/105244409