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

给定X,求不小于X的整数,满足各个数位正好构成等差数列。
1<=X<=1E17

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

#include <bits/stdc++.h>
using namespace std;
#define int 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--)

int x;
set<int> ans;
void check(int d, int a) {
    if (d == 0 && a == 0)
        return;
    int 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";
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}
posted @ 2024-03-12 21:57  chenfy27  阅读(1)  评论(0编辑  收藏  举报