Past

· · 题解

官方题解。

容易发现,当 n \le 10^{18} 时,b \le 59。故枚举 b,判断 n-2^b 是否是回文数即可。复杂度 O(\log^2 n)。

:::success[实现]{open}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
bool check(ll x){
    ll rev = 0, t = x;
    while (x){
        rev = rev * 10 + x % 10;
        x /= 10;
    }
    return rev == t;
}
void solve(){
    cin >> n;
    for (int i = 0; ; i++){
        if (n - (1ll << i) < 0){
            cout << "No" << "\n";
            break;
        }
        if (check(n - (1ll << i))){
            cout << "Yes" << "\n";
            break;
        }
    }
    return;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--){
        solve();
    }
    return 0;
}

:::