#include<bits/stdc++.h> using namespace std; const int maxn=200005; int n; int a[maxn]; int main() { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); vector<int> res = { a[1] }; for(int i = 1; i <= n; i++){ for(int j = 0; j < 31; j++){ int lx = a[i] - (1 << j); int rx = a[i] + (1 << j); bool isl = binary_search(a + 1, a + n + 1, lx); bool isr = binary_search(a + 1, a + n + 1, rx); if(isl && isr && int(res.size()) < 3){ res = { lx, a[i], rx }; } if(isl && int(res.size()) < 2){ res = { lx, a[i] }; } if(isr && int(res.size()) < 2){ res = { a[i], rx }; } } } cout << int(res.size()) << endl; for(auto it : res) cout << it << " "; cout << endl; return 0; }
题解:能整除25,说明最后两位是00,50,25,75中的一种。
注意前导0的情况,比如50267,所以要先把移动过程中出现的前导0处理掉。还有一种情况502,就是当末位为5,移掉5之后,会出现前导0,并且前导是无法处理掉的,这时可以特判一下。
#include<bits/stdc++.h> #define ll long long #define mem(arry, in) memset(arry, in, sizeof(arry)) #define For(index, st, end) for(int index = st; index < end; index++) using namespace std; const int maxn = 200005; const int INF = 1e9 + 7; ll n, cnt; int c[20]; void build(ll x){ cnt = 0; while(x){ c[cnt++] = x % 10; x /= 10; } } int solve_1(){ int l = -1, r = -1; for(int i = 0; i < cnt; i++) if(c[i] == 0) { l = i; break; } for(int i = 0; i < cnt; i++) if(i != l && (c[i] == 0 || c[i] == 5)) { r = i; break; } if(l == -1 || r == -1) return INF; return (l > r ? l + r : l + r - 1); } int getzero(int index){ int t = 0; for(int i = index - 1; i >= 0; i--){ if(c[i] == 0) t++; else break; } return t; }
int solve_2(){
int l = -1, r = -1;
for(int i = 0; i < cnt; i++) if(c[i] == 5) { l = i; break; }
if(l == cnt - 1 && c[l - 1] == 0){
int x = getzero(l);
if(x == cnt - 2) return INF;
l += x;
}
for(int i = 0; i < cnt; i++) if(i != l && (c[i] == 2 || c[i] == 7)) { r = i; break; }
if(l == -1 || r == -1 || (r == cnt - 1 && c[r - 1] == 0)) return INF;
return (l > r ? l + r : l + r - 1);
}
int main() { cin >> n; build(n); if(c[0] == 0 && c[1] == 0) { cout << 0 << endl; return 0; } int res = min(solve_1(), solve_2()); cout << (res == INF ? -1 : res) << endl; return 0; }