浙江中医药大学暑期训练测试赛八E
2017-08-01 09:12
141 查看
1570: Palindromic Numbers
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 110 Solved: 7
[Submit][Status][Web
Board]
Description
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
31
4
21
Sample Output
23
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);
d411
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;
}
相关文章推荐
- 浙江中医药大学暑期训练测试赛十 英雄无敌3(2) 数学公式
- 浙江中医药大学暑期训练测试赛八H
- 浙江中医药大学暑期训练测试赛八I
- 浙江中医药大学暑期训练测试赛八G
- 浙江中医药大学暑期训练测试赛十
- 浙江中医药大学暑期训练测试赛八C
- 浙江中医药大学暑期训练测试赛八A
- 浙江中医药大学暑期训练测试赛八B
- 浙江中医药大学暑期训练测试赛八F
- 暑期测试训练3
- 利用caffe训练好的模型测试自己的手写字体图片
- Windows Caffe 学习笔记(四)搭建自己的网络,训练和测试MNIST手写字体库
- CSU-ACM2017暑期训练14-最短路 D - 输入量很大的模板题
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- 使用caffe训练并且测试一个自己的模型
- 用训练好的caffe模型来测试样本
- CSU-ACM2017暑期训练16-树状数组 C - 按钮控制彩灯实验 CSU - 1770
- NTU-Coursera机器学习:机器学习的可行性 & 训练与测试
- 微软caffe windows cpu用自己数据 训练和测试AlexNet
- Caffe学习系列(12):训练和测试自己的图片