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:

  • for each color from 11 to mm 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 11, i.e. gcd(ai,aj)>1gcd(ai,aj)>1 for each pair i,ji,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 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;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章