跳到正文

算法·理论

PBOI-Round4-2026春季赛 & 2026愚人节赛 题解

pbcoding123约 22 分钟
原文
文章信息与版本

文章信息

发布
2026年4月14日
收录
2026年5月18日
更新
2026年11月11日
篇幅
约 22 分钟

版本

  1. v1当前

比赛传送门

INVITE CODE

这个p2f7没错,只是中间插入了不可见字符,直接复制洛谷是不认的。这告诉我们,不要被打上 lazy-tag

User Color Problem

这题其实就是仿照官方比赛第一题,直接输出purple即可。

ABC

这题简单推柿子:

8+(n(n+1)256−n256)−n2+20482561145n\large\frac{8+(\frac{n(n+1)}{256}-\frac{n}{256})-\frac{n^2+2048}{256}}{1145n}

=8+n2256−n2+20482561145n\large=\frac{8+\frac{n^2}{256}-\frac{n^2+2048}{256}}{1145n}

=2048256+n2256−n2+20482561145n\large=\frac{\frac{2048}{256}+\frac{n^2}{256}-\frac{n^2+2048}{256}}{1145n}

=n2+2048256−n2+20482561145n\large=\frac{\frac{n^2+2048}{256}-\frac{n^2+2048}{256}}{1145n}

=01145n\large=\frac{0}{1145n}

=0\large=0

由于 00 乘任何数都为 00 ,所以无脑输出 00 即可。

xyz

{x=k1z+ay=k2x+bz=k3y+c\begin{cases} x=k_1z+a\\ y=k_2x+b\\ z=k_3y+c \end{cases}

只需要让 k1,k2,k3=0k_1,k_2,k_3=0 ,方程变为

{x=ay=bz=c\begin{cases} x=a\\ y=b\\ z=c \end{cases}

所以把输入输出即可。

Flag

既然是CTF,先复制源码:

代码
# T751169 [PBOI-Round4] [Fun] Flag

## 题目背景

[](tr)
有一些优点,只有仔细才能看到……\
有一些缺点,只有别人才能看到……\
有一些旗子,只有放大[](3列testpoint)才能看到……\
有一些快乐,只有用心才能看到……\
有一些线索,只有观察才能看到……
[](y-)

## 题目描述

[](2-)
这是一个 CTF(Capture The Flag) 题目。\
找到这个Flag。
[](su)

## 输入格式

无

## 输出格式

[](bm)一个整数,表示找到的Flag。[](it)

把零散的隐藏信息拼接起来:try-2-submit,也就是try to submit,即尝试提交一下。

随便交一发,可以得到 6969 个测试点,其中的UKE似乎拼成了什么图案。

回到源码,在

代码
有一些旗子,只有放大[](3列testpoint)才能看到……

提示我们放大到 33 列测试点,放大之后会看到竖着的由UKE拼成的 20482048 ,提交即可。

Keyboard

这题要求选手有反编译能力。

反编译后找到生成序列的gen()函数,发现生成的种子其实是在下列种子中随机选一个。
{1145141919, 1234567890, 888888888, 33554432, 20260401}
于是我们只需要用程序随机选一个种子,依次按下生成的序列中的每个字母即可,只需要运气一下下就可以了。

keybd的代码

这是源码,可以自行研究一下。
除了base64和SetClipboard是AI,其余部分都是手敲的awa。

C++
#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdio>
#include<vector>
using namespace std;

bool pk(int vk){return (GetAsyncKeyState(vk)&0x8000);}
void gotoxy(int x,int y){COORD pos={(short)x,(short)y};HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOut,pos);}
void hidecur(){CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}
void putnstr(int n, string s){for(int _=1;_<=n;_++)printf("%s", s.c_str());}
void noedit(){HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);DWORD mode;GetConsoleMode(hStdin,&mode);mode&=~ENABLE_QUICK_EDIT_MODE;mode&=~ENABLE_INSERT_MODE;mode&=~ENABLE_MOUSE_INPUT;SetConsoleMode(hStdin,mode);}
void color(int x){SetConsoleTextAttribute(GetStdHandle(4294967285u),x);}
COORD getFontSize() {
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize = sizeof(cfi);
    GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
    return cfi.dwFontSize;
}
POINT getmouse() {
    POINT screenPos;
    GetCursorPos(&screenPos);
    ScreenToClient(GetConsoleWindow(), &screenPos);
    COORD fontSize=getFontSize();
    int col=fontSize.X>0?screenPos.x/fontSize.X:0;
    int row=fontSize.Y>0?screenPos.y/fontSize.Y:0;
    return {col, row};
}

const int view=5, len=100;
int cur=1, tick;
char p[128];
unsigned int seeds[6]={0, 1145141919, 1234567890, 888888888, 33554432, 20260401};

int getseed(){
	srand(time(0));
	return seeds[rand()%5+1];
}

void gen(){
	int sd=getseed();
	srand(sd);
	for(int i=1;i<=len;i++){
		p[i] = rand()%26+'A';
	}
}

namespace bs64{
	static const string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	string b64enc(const string &in) {
	    string out;
	    int val = 0, valb = -6;
	    for (unsigned char c : in) {
	        val = (val << 8) + c;
	        valb += 8;
	        while (valb >= 0) {
	            out.push_back(b64[(val >> valb) & 0x3F]);
	            valb -= 6;
	        }
	    }
	    if (valb > -6) out.push_back(b64[((val << 8) >> (valb + 8)) & 0x3F]);
	    while (out.size() % 4) out.push_back('=');
	    return out;
	}

	string b64dec(const string &in) {
	    string out;
	    vector<int> T(256, -1);
	    for (int i = 0; i < 64; i++) T[b64[i]] = i;

	    int val = 0, valb = -8;
	    for (unsigned char c : in) {
	        if (T[c] == -1) break;
	        val = (val << 6) + T[c];
	        valb += 6;
	        if (valb >= 0) {
	            out.push_back(char((val >> valb) & 0xFF));
	            valb -= 8;
	        }
	    }
	    return out;
	}
};

string encode(string s){
	srand(time(0));
	const string rub=",./;'[]\\|=+-*<>?:\"{}!@#$%^&*()";
	int buf[1024];
	for(int i=0;i<s.size();i++){
		buf[i] = (int)s[i];
	}
	string res;
	for(int i=0;i<s.size();i++){
		int fub[64];
		int r=buf[i], idx=0;
		while(r){
			fub[++idx] = (r&1);
			r >>= 1;
		}
		for(int j=idx;j>=1;j--){
			res.push_back(fub[j]+rand()%12*2+'a');
		}
		res.push_back(rub[rand()%30]);
	}
	return bs64::b64enc(res);
}
string decode(string s){
	string raw=bs64::b64dec(s);
	string res;
	for(int i=0;i<raw.size();i++){
		int c=0;
		while(islower(raw[i])){
			c <<= 1;
			c = (c|((raw[i]-'a')%2));
			i++;
		}
		res.push_back((char)c);
	}
	return res;
}

void SetClipboard(string x){
    if(OpenClipboard(nullptr)){
        EmptyClipboard();
        HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, (x.length()+1)*sizeof(char));
        if (hGlobal) {
            char* pGlobal = static_cast<char*>(GlobalLock(hGlobal));
            if (pGlobal) {
                strcpy(pGlobal, x.c_str());
                GlobalUnlock(hGlobal);
                SetClipboardData(CF_TEXT, hGlobal);
            }
        }
        CloseClipboard();
    }
}

string genflag(){
	int t=time(0);
	int speed=t-tick;
	string sp=to_string(speed);
	string st=to_string(t);
	string salt=decode("cmdlY2JkbCpybndlYWt2XnJ2Z2ZkaXAqcGRhc2RleCtycHN1ZHcnYmZpd21ve2Joc2Nkdy9qZG12aGM/ZmRnb29tKnBsZ2JvcyhuYndhb2EncHRrb2l2J3B1dHBpdiU=");
	string flag="flag{"+encode(salt+sp+"-"+st)+"}";
	SetClipboard(flag);
	return flag;
}

int main(){
	hidecur();
	noedit();
	gen();
	while(1){
		gotoxy(0, 0);
		printf("+-----------------+\n");
		printf("|Press Me To Start|\n");
		printf("+-----------------+\n");
		POINT p=getmouse();
		if(pk(VK_LBUTTON)&&p.x<=18&&p.y<=2){
			break;
		}
		Sleep(20);
	}
	tick = time(0);
	while(1){
		gotoxy(0, 0);
		putnstr(15, " ");
		printf("[Release Your Speed!]");
		printf("\n   +");
		putnstr(view, "---------");
		printf("+\n   |");
		int first=(cur-1)/view*view+1;
		if(cur==101){
			putnstr(20, " ");
			printf("Done!");
			putnstr(20, " ");
		}
		else{
			for(int i=0;i<view;i++){
				putnstr(4, " ");
				if(first+i==cur) color(/*BACKGROUND_INTENSITY|*/BACKGROUND_RED|0x7);
				printf("%c", p[first+i]);
				if(first+i==cur) color(0x7);
				putnstr(4, " ");
			}
		}
		printf("|\n   +");
		putnstr(view, "---------");
		printf("+\n [");
		putnstr((cur-1)/2, "=");
		if((cur-1)%2) printf("-");
		putnstr(50-(cur-1)/2-(cur-1)%2, " ");
		printf("]\n  %ds", time(0)-tick);
		if(cur==101) break;
		if(pk(p[cur])) cur++;
		Sleep(1);
	}
	printf("\n\nYou Capture The Flag : %s\n\nIt is already copied to the clipboard. Press Ctrl+V to paste in the problem.\n\n", genflag().c_str());
	system("pause");
	return 0;
}

Guess

签到题,把数字当成字符串读入处理更方便。

生成树

其实挺愚人的,完全图的第 nn−2n^{n-2} 小生成树其实就是最大生成树,只需要把最大的 n−1n-1 条边求和即可。

Toposort

其实就是拓排模板,但是对于自环,在判定之前就把指向自己的这条边删了,所以只要忽略自环跑拓排即可。还有一点,输出的是YE5 N0而不是YES NO。

Hide the flag

其实题目描述在提示 Flag 一题的解法,也就是观察测试点形状。

其实非常水。

其实数据范围是为了 I/O 速度而减小的,原来打算 1000×1000=1061000\times1000=10^6 的。

其实跑一个 BFS 再遍历每个点即可。

std

Code
User Color Problem
C++
#include<iostream>
using namespace std;
int main(){
    cout << "purple";
    return 0;
}
ABC

#include<iostream> using namespace std; int main(){ cout << 0; return 0; }

xyz
C++
#include<iostream>
using namespace std;
long long a, b, c;
int main(){
    cin >> a >> b >> c;
    cout << a << " " << b << " " << c;
    return 0;
}
Flag
C++
#include<iostream>
using namespace std;
int main(){
    cout << 2048;
    return 0;
}
Keyboard
C++
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<windows.h>
using namespace std;

unsigned int seeds[6]={0, 1145141919, 1234567890, 888888888, 33554432, 20260401};
char p[256];

int main(){
	srand(time(0));
	int sd=seeds[rand()%5+1];
	srand(sd);
	for(int i=1;i<=100;i++) p[i] = rand()%26+'A';
	for(int i=1;i<=5;i++) cout << p[i] << " ";
	Sleep(5000);
	mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // 点开始按钮
	Sleep(100);
	mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
	Sleep(1000);
	for(int i=1;i<=100;i++){
		keybd_event(p[i], 0, 0, 0); // 按按键
		Sleep(60);
		keybd_event(p[i], 0, 2, 0);
		Sleep(60);
	}
	return 0;
}
Guess
C++
#include<iostream>
using namespace std;
int n;
string key, guess;

int main(){
	cin >> n >> key;
	for(int i=1;i<=n;i++){
		cin >> guess;
		int a=0, b=0;
		for(int i=0;i<key.size();i++){
			if(key[i]==guess[i]){
				a++;
			}
			if(key.find(guess[i])!=-1){
				b++;
			}
		}
		printf("%dA + %dB\n", a, b-a);
	}
	return 0;
}
生成树
C++
#include<iostream>
using namespace std;

long long n;

int main(){
	cin >> n;
	cout << n*(n-1)*1000000+n*(n-1)/2;
	return 0;
}
Toposort
C++
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

const int maxn=114514;
int n, m;
vector<int> G[maxn];
int in[maxn];

void toposort(){
	queue<int> q;
	for(int i=1;i<=n;i++){
		if(in[i]==0){
			q.emplace(i);
		}
	}
	int cnt=0;
	while(q.size()){
		int u=q.front();
		q.pop();
		cnt++;
		for(int v:G[u]){
			if(--in[v]==0){
				q.emplace(v);
			}
		}
	}
	if(cnt==n){
		printf("YE5");
	}
	else{
		printf("N0");
	}
}

int main(){
	closeios();
	cin >> n >> m;
	for(int i=1;i<=m;i++){
		int u, v;
		cin >> u >> v;
		if(u!=v){
			G[u].emplace_back(v);
			in[v]++;
		}
	}
	toposort();
	return 0;
}
Hide the flag
C++
#include<iostream>
#include<queue>
#define sqrt(x) __builtin_sqrt(x)
#define memcpy(dest, src, n) __builtin_memcpy(dest, src, n)
#define memset(st, val, n) __builtin_memset(st, val, n)
#define memmove(dest, src, n) __builtin_memmove(dest, src, n)
#define closeios() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
template<typename t> t sqr(t x){return (x*x);}
template<typename t> t abs(t x){return (x>0?x:-x);}
template<typename t> t inrange(t n, t l, t r){return (l<=n&&n<=r);}
int       log2(int x){return (31-__builtin_clz(x));}
long long log2(long long x){return (63-__builtin_clzll(x));}
int       getback0(int x){return (x==0?0:__builtin_ctz(x));}
long long getback0(long long x){return (x==0?0:__builtin_ctzll(x));}
#define int2ll 0
#if int2ll
#define intEx signed
#define int long long
#else
#define intEx int
#define ll long long
#endif
using namespace std;

const int maxn=114;
int n, m;
char map[maxn][maxn];
struct node{int x, y;node(int _x, int _y){x=_x;y=_y;}};
queue<node> q;
int dir[4][2]={{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int vis[maxn][maxn];

intEx main(){
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	// code
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >> map[i][j];
			if(map[i][j]=='S'){
				q.emplace(i, j);
			}
		}
	}
	while(q.size()){
		auto [x, y]=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int dx=x+dir[i][0], dy=y+dir[i][1];
			if(inrange(dx, 1, n)&&inrange(dy, 1, m)&&map[dx][dy]!='#'&&!vis[dx][dy]){
				vis[dx][dy] = 1;
				q.emplace(dx, dy);
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(!vis[i][j]&&map[i][j]!='#'){
				cout << i << " " << j << "\n";
			}
		}
	}
	return 0;
}

题解仅供参考。

原文