给定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;
}
标签:return,int,rep,long,abc234E,等差数列,数位
From: https://www.cnblogs.com/chenfy27/p/18069439