Codeforces 1332B - Composite Coloring (数学)
Description
题目大意是给定不超过1000个和数(均小于1000)。用m(m<=11)颜色染色。其中染上相同颜色的数必定是具有大于1的公约数。
思路
从小到大用素数把含有该素数的数染成相同颜色。由于小于1000,因此前11个素数必定可以覆盖完所有的数。因为第11个素数和第12个素数相乘就已经大于1000了。
点击展开代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3+10;
#define inf 0x3f3f3f3f
int arr[N];
int c[N];
bool vis[N];
vector<int> prime;
int main() {
ios::sync_with_stdio(false);
for(int i = 2; i <= 1000; i++) {
if(vis[i]) continue;
prime.push_back(i);
for(int j = 2 * i; j <= 1000; j+=i) {
vis[j] = 1;
}
}
int t;
cin >> t;
while(t--) {
memset(c, 0, sizeof c);
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
int cnt = 1;
for(auto p : prime) {
bool ok = false;
for(int i = 0; i < n; i++) {
if(c[i]) continue;
if(arr[i] % p == 0) {
c[i] = cnt;
ok = true;
}
}
if(ok) cnt++;
}
cout << cnt - 1 << endl;
for(int i = 0; i < n; i++) cout << c[i] << " ";
cout << endl;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端