B. Composite Coloring

在这里插入图片描述
题目:
B. Composite Coloring
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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
inputCopy
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
outputCopy
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.

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

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

**坑点:**记得多组测试用例,每次都要将数组清零和map清零,否则会对后面的测试用例产生未知的错误

代码:

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <iterator>
#include<math.h>
#define debug() puts("what the fuck")
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int a[2000];
int pr[11]={2,3,5,7,11,13,17,19,23,29,31};
int  b[12];
map<int,int>mp;
int main()
{
    int t,n,j,i;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int ans=0;
        mp.clear();
        memset(b,0,sizeof(b));
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            for( j=0;j<11;j++)
            {
                if(a[i]%pr[j]==0)
                {
                    if(b[j]==0)
                    {
                        ans++;
                        b[j]=ans;
                    }
                    mp[i]=b[j];
                    break;
                }
            }

        }
        cout<<ans<<endl;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            if(flag==0)
            { cout<<mp[i];
                flag=1;
            }
               else
                cout<<" "<<mp[i];
        }
        cout<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值