• luogu_2425【题解】小红帽的回文数 (基本算法)


    题目:https://www.luogu.org/problemnew/show/P2425

    大意:求 t 个数,a[1.....t]  满足其在 x 进制下是回文数,求x。

    题解:

    1.可以枚举x,求出。(不知道可不可以)。

    2.当 x 大于sqrt(a) 时,数字会只有两位数。

     所以设 i 为两位数的分别的数字。

     有 i * x + i == a 

     化简 x= a / i - 1

     所以 i 大于 sqrt(a),且当 i 是 a 的因数时,右边就是答案。

     当 i 小于 sqrt(a) 时,枚举 x 。

     代码如下。

    #include<bits/stdc++.h>
    using namespace std;
    int t;
    long long num[1010];
    inline bool pd(long long n,long long x){
        long long z=n,m=0;
        while(z>0){
            m++;
            num[m]=z%x;
            z=z/x;
        }
        for(int i=1;i<=m/2;i++)
            if(num[i]!=num[m-i+1]) 
                return 0;
        return 1;
    }
    int main()
    {
        scanf("%d",&t);
        while(t--){
            bool b=0;
            long long a;
            scanf("%lld",&a);
            long long i=1;
            do
            {
                i++;
                if(pd(a,i)){
                    printf("%lld
    ",i);b=1;break;
                }
            }while(i<=sqrt(a)-1);
            if(b==0)
                for(int i=a/sqrt(a)-1;i>=1;i--)
                    if(a%i==0){ 
                        printf("%lld
    ",a/i-1);
                        break; 
                    }
        }
        return 0;
    }

     不要忘了开 long long ,不然在特殊的计算中可能会爆炸。

  • 相关阅读:
    preprocessing
    hist
    RabbitMQ
    线程池
    springmvc功能以及源码实现分析
    西瓜书第二章--模型评估与选择
    西瓜书第一章--绪论
    基于python的递归简述
    python小白学习之旅5
    python小白学习之旅4
  • 原文地址:https://www.cnblogs.com/ChrisKKK/p/10923319.html
Copyright © 2020-2023  润新知