跳到正文

题解

题解 P9384【[THUPC 2023 决赛] 着色】

rui_er约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年6月3日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 2 分钟
获赞
8
题目
P9384

版本

  1. v1当前

有趣的构造题!

不存在三元环或五元环颜色相同看起来很诈骗,不妨加强一下问题,使得不存在奇环颜色相同。也就是说,每种颜色构成一个二分图。

又发现颜色数 10=⌈log⁡21000⌉10=\lceil\log_21000\rceil,其中 10001000 是 nn 的上限。因此,我们可以自然地想到使用二进制位为 00 或为 11 作为划分左部、右部的依据。

具体地,我们使得连接 u,vu,v 的边的颜色为 ctz⁡(u⊕v)\operatorname{ctz}(u\oplus v),其中 ctz⁡(x)=log⁡2(lowbit⁡(x))\operatorname{ctz}(x)=\log_2(\operatorname{lowbit}(x)),是 xx 二进制后导零个数。容易发现,这种构造是符合要求的,每种颜色的边必定连接某一个二进制位不同的两个点,因此每种颜色的边都构成二分图。

于是不存在奇环颜色相同,自然就不存在三元环或五元环颜色相同了。

C++
//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
    uniform_int_distribution<int> dist(L, R);
    return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

int main() {
    int n;
    scanf("%d", &n);
    rep(i, 1, n-1) {
        rep(j, i+1, n) printf("%d", (int)__builtin_ctz(i ^ j));
        puts("");
    }
    return 0;
}
原文
存档评论1 条抓取于