abc234E 不小于X的数位构成等差数列的最小数字

50 阅读1分钟

题面:给定X,求不小于X的整数,满足各个数位正好构成等差数列。

范围:1 <= X <= 1E17

分析:直接枚举首项和公差,找出所有可行的解,取最优值即可。

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)

i64 x;
set<i64> ans;
void check(i64 d, i64 a) {
    if (d == 0 && a == 0)
        return;
    i64 u = a;
    while (a < x) {
        u += d;
        if (u < 0 || u > 9) {
            return;
        }
        a = a * 10 + u;
    }
    ans.insert(a);
}
void solve() {
    cin >> x;
    rep(i,-9,9) rep(j,0,9) {
        check(i, j);
    }
    cout << *ans.lower_bound(x) << "\n";
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}

标签:暴力