浙江中医药大学暑期训练测试赛八E

1570: Palindromic Numbers

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 110   Solved: 7
[ Submit][ Status][ Web Board]

Description

    • Johnny has figured out that there are some numbers which have an interesting property: they are the same when read from left to right, and from right to left. For example, 5115 and 929 are such numbers, while 31 and 125 are not. Johnny calls such numbers palindromic numbers.

      After a while, Johnny has realized that his definition of palindromic numbers is not really precise. Whether a number is palindromic or not depends on the base in which the number is written. For example, 21 is not palindromic in base 10 but it is palindromic in base 2 (because 21 = 101012).

      Johnny finds it interesting that any number becomes palindromic when it is written in an appropriate base.

      Given a number N, write a program to help Johnny compute the smallest base B such that N is palindromic when written in base B.

Input

  • The first line contains t, the number of test cases (about 1000). Then t test cases follow.

    Each test case consists of a number N written in one line (1 <= N <= 1010).

Output

For each given number N, print a line containing the smallest base B such that N is palindromic in base B.

Sample Input

3
1
4
21

Sample Output

2
3
2

超时代码:
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
const int MOD=1000000007;
int pan(int n,int k)
{
        int a[35]={0};
        int r=0;
        while(n)
        {
                a[r++]=n%k;
                n=n/k;
        }
        int i;
        for(i=0;i<=(r-1)/2;i++)
        {
                if(a[i]!=a[r-1-i])
                {
                        break;
                }
        }
        if(i>(r-1)/2)
                return 1;
        else
                return 0;
}
int main()
{
        int t;
        scanf("%d",&t);
        while(t--)
        {
                long long n;
                scanf("%lld",&n);
                if(n==1)
                {
                        printf("2\n");
                        continue;
                }
                if(n==2)
                {
                        printf("3\n");
                        continue;
                }
                for(int i=2;i<=n;i++)
                {
                        if(pan(n,i))
                        {
                                printf("%d\n",i);
                                break;
                        }
                }
        }
        return 0;
}



AC代码:



#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<cmath>
using namespace std;;
int a[100000];
int pan(long long n,int k)
{
        int r=0;
        while(n)
        {
                a[r++]=n%k;
                n=n/k;
        }
        int i;
        for(i=0;i<r/2;i++)
        if(a[i]!=a[r-1-i])
                return 0;
        return 1;
}
int main()
{
        int t;
        scanf("%d",&t);
        while(t--)
        {
                long long n;
                scanf("%lld",&n);
                if(n==1)
                {
                        printf("2\n");
                        continue;
                }
                int len=sqrt(n);
                if(n<10000)
                {
                        len=n;
                }
                for(int i=2;i<=len;i++)
                {
                        if(pan(n,i))
                        {
                                printf("%d\n",i);
                                goto out;
                        }
                }
                len=sqrt(n);
                for(int i=len;i>=1;i--)
                if(n%i==0)
                {
                        printf("%lld\n",n/i-1);
                        break;
                }
                out:;
        }
        return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值