跳到正文

个人记录

abc451_d 题解

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

文章信息

收录
2026年3月29日
更新
2026年3月29日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

观察样例:当 nn 为 10998981099898 (1.09×1061.09 \times 10 ^ 6)时输出 819264512819264512,为 8.19×1088.19 \times 10 ^ 8,已经十分接近上限 10910 ^ 9。

我们大胆猜测好数的个数小于一个阈值 kk。暴力搜出 kk 个好数后排序找到第 nn 小的。为了避免搜出重复的数,可以适当放大阈值。

实测 k=1.5×107k = 1.5 \times 10 ^ 7 时可过。

需要注意 00 并不是合法的答案,所以答案是搜出来的第 n+1n + 1 小的数。

C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1500000 + 10;
int n;
ll p[MAXN], po[MAXN];
ll res, tot = 0;
ll ans[MAXN];
ll limit = 1500000;//k = 1.5 * 10 ^7
void dfs(){
	if(tot == limit){
      //排序 + 去重
      sort(ans + 1, ans + 1 + tot);
      unique(ans + 1, ans + 1 + tot);
      cout << ans[n + 1];
      exit(0);
	}
    ans[++tot] = res;
    for(int i = 0; i <= 30; i++){
        if(res * po[i] + p[i] < 1e9){
        	int las = res;
            res = res * po[i] + p[i];
            dfs();
            res = las;
        }
    }
}
ll get(ll x){
    ll cnt = 0;
    while(x){
        x /= 10;
        cnt++;
    }
    return cnt;
}
int main(){
    cin >> n;
    p[0] = 1, po[0] = 10;
    for(int i = 1; i <= 30; i++){
        p[i] = (1 << i), po[i] = pow(10, get(p[i]));
    }
    dfs();
    //为了防止搜不出来,最好在主函数也统计一遍答案。
    sort(ans + 1, ans + 1 + tot);
    unique(ans + 1, ans + 1 + tot);
    cout << ans[n + 1];
    return 0;
}
原文