P16277题解

· · 题解

这题应该是个红题...

题目简介

一个回文数 a 加二的 b 次方,相加可以得到 n ,n 是题目给出的

思路

预处理出 2 的幂次,到 1^{18} 范围之内的有 60 个左右。

然后通过多测,每次输入一个 n ,然后拿 n 去减预处理出的 2 的幂次,判断减出的数是不是回文,最多 18 位,可以当成 O(1)。

循环 50 次,也差不多是 O(1)

最后,整个程序的时间复杂度为 O(T)

ACcode

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
#define el '\n'
const ll N=1e2+5;
ll n;
ll a[N];
bool ishuiwen(ll x){
    ll k=x;
    ll y=0;
    do{
        y=y*10+x%10;
        x/=10;
    }while(x);
    return (k==y);
}
void solve(){
    cin>>n;
    for(ll i=0;i<=log2(n)+5;i++){
        if(a[i]>n) break;
        if(ishuiwen(n-a[i])){
            cout<<"Yes\n";
            return;
        }
    }
    cout<<"No\n";
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("xxx.in","r",stdin);
    //freopen("xxx.out","w",stdout);
    a[0]=1;
    for(ll i=1;i<=62;i++){
        a[i]=a[i-1]*2;
    }
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}