codeforces B. Composite Coloring

在这里插入图片描述

题目

题意:

给你一个 a a a序列,让你给这个 a a a序列涂色,但是两种颜色相同的 a i a_i ai必须有 g c d ( a i , a j ) > 1 gcd(a_i,a_j)>1 gcd(ai,aj)>1且颜色最大不能超过 11 11 11

思路:

因为颜色 m m m最大不超过 11 11 11,所以我们可以从 11 11 11入手我们可以发现质数有 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 2,3,5,7,11,13,17,19,23,29,31 2,3,5,7,11,13,17,19,23,29,31刚好 11 11 11个,那么第 12 12 12个是 37 , 37 ∗ 37 > 1000 37,37*37>1000 373737>1000,所以不考虑,我们可以参考埃式筛法的思路,把所有的相同的从 2 2 2开始的 g c d ( a i , a i ) = 2 gcd(a_i,a_i)=2 gcd(ai,ai)=2的进行标记,比如 4 4 4可以写成 b o o k [ 4 ] = 2 book[4] = 2 book[4]=2,但是还有一个问题那就是有多少种颜色就不能有超过这个颜色的数字,所以最后要设置一个数组映射一下就行了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 1010;
int a[maxn], book[maxn];
int prime[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
void init() {
    for (int i = 0; i < 11; i++) {
        for (int j = prime[i]; j <= maxn; j += prime[i]) {
            book[j] = i + 1;
        }
    }
}
int gcd(int a, int b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
int main() {
    int t;
    read(t);
    init();
    while (t--) {
        int n, cnt = 0;
        int xx[20] = {0};
        read(n);
        for (int i = 0; i < n; i++) {
            read(a[i]);
            if (xx[book[a[i]]] == 0) xx[book[a[i]]] = ++cnt;
        }
        printf("%d\n", cnt);
        for (int i = 0; i < n; i++) {
            out(xx[book[a[i]]]);
            putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值