题解 ARC153A【AABCDDEFE】

本题解并非正经解法(正经解法是简单数学),但在遇到更复杂的类似题目时可能会用到,放在这里供参考。

九位数共 9×108 个,可以在较短时间内枚举一遍,因此考虑分段打表。按题意容易写出暴力打表程序(核心代码):

#define rep(x,y,z) for(int x=(y);x<=(z);x++)
bool check(int x) {
	int a = x / 100000000;
	int b = x / 10000000 % 10;
	int c = x / 1000000 % 10;
	int d = x / 100000 % 10;
	int e = x / 10000 % 10;
	int f = x / 1000 % 10;
	int g = x / 100 % 10;
	int h = x / 10 % 10;
	int i = x % 10;
	return a == b && e == f && g == i;
}
int main() {
	int n; vector<int> p;
	rep(i, 100000000, 999999999) if(check(i)) p.push_back(i);
	printf("const int a[]={0");
	rep(i, 0, (int)p.size()-1) /* if(i % 1000 == 999) */ printf(",%d", p[i]);
	printf("};");
	return 0;
}

上面这份代码会生成所有符合要求的九位数,并生成相关的 C++ 代码。但是数太多了代码太长,怎么办呢?我们每 1000 个数打印一个(即注释部分),然后代码就能放下了。

容易根据 n 算出所求在哪两个数中间,把这两个数中间的所有数暴力一遍即可。

可以得到最终代码:

// Problem: A - AABCDDEFE
// Contest: AtCoder - AtCoder Regular Contest 153
// URL: https://atcoder.jp/contests/arc153/tasks/arc153_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//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;
const int a[]={100000000,110099999,110199999,...,999999999};

int n;
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;}
bool check(int); // 同打表程序

int main() {
	scanf("%d", &n);
	int i, j;
	for(i = a[n/1000] + 1, j = n / 1000 * 1000; j < n; i++) if(check(i)) ++j;
	printf("%d\n", i-1);
	return 0;
}
posted @   rui_er  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示