Composite Coloring CodeForces - 1332B(数论+思维)

A positive integer is called composite if it can be represented as a product of two positive integers, both greater than 1. For example, the following numbers are composite: 6, 4, 120, 27. The following numbers aren’t: 1, 2, 3, 17, 97.

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

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

for each color from 1 to m there is at least one element of this color;
each element is colored and colored exactly one color;
the greatest common divisor of any two elements that are colored the same color is greater than 1, i.e. gcd(ai,aj)>1 for each pair i,j if these elements are colored the same color.
Note that equal elements can be colored different colors — you just have to choose one of m colors for each of the indices from 1 to n.

Alice showed already that if all ai≤1000 then she can always solve the task by choosing some m≤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 m from 1 to 11.

Input
The first line contains a single integer t (1≤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 n (1≤n≤1000) — the amount of numbers in a sequence a.

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

It is guaranteed that the sum of n over all test cases doesn’t exceed 104.

Output
For each test case print 2 lines. The first line should contain a single integer m (1≤m≤11) — the number of used colors. Consider colors to be numbered from 1 to m. The second line should contain any coloring that satisfies the above conditions. Print n integers c1,c2,…,cn (1≤ci≤m), where ci is the color of the i-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 m from 1 to 11.

Remember that each color from 1 to m 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 1).

Example
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
1
1 1 1
2
2 1
11
4 7 8 10 7 3 10 7 7 8 3 1 1 5 5 9 2 2 3 3 4 11 6
Note
In the first test case, gcd(6,10)=2, gcd(6,15)=3 and gcd(10,15)=5. Therefore, it’s valid to color all elements the same color. Note that there are other colorings which satisfy Alice’s requirement in this test case.

In the second test case there is only one element of each color, so the coloring definitely satisfies Alice’s requirement.
思路:一开始就很好奇为什么非得是11呢?因为1000内的质因数个数就是11个。我们对于输入的每一个数字,都求一下它的最小的质因子,如果最小的质因子一样的话,就染成一样的颜色。这样做①最终颜色的个数不会超过11。②对于gcd大于1的数字来说,颜色一样。③对于gcd等于1的数字来说,颜色一定不一样。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e3+100;
int vis[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		map<int,int> mp;mp.clear();
		int ans=0,x;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			for(int j=2;j<=x;j++)
			{
				if(x%j==0)
				{
					if(mp[j]) vis[i]=mp[j];
					else vis[i]=mp[j]=++ans;
					break;
				}
			}
		}
		cout<<ans<<endl;
		for(int i=1;i<=n;i++) cout<<vis[i]<<" ";
		cout<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值