跳到正文

题解

P16277题解

sdmin_dp约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年4月12日
收录
2026年5月4日
更新
2026年10月18日
篇幅
约 2 分钟
题目
P16277

版本

  1. v1当前

这题应该是个红题…

题目简介

一个回文数 aa 加二的 bb 次方,相加可以得到 nn ,nn 是题目给出的

思路

预处理出 22 的幂次,到 1181^{18} 范围之内的有 6060 个左右。

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

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

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

ACcode

C++
#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;
}
原文