ZOJ1238 Guess the Number
/*
In this problems, we’ll talk about BIG numbers. Yes, I’m sorry, big numbers again…. Let N be a positive integer, we call S=NN the “big big power” of N. In this time, I will calculate the exact value of S for a positive integer N. Then, I tell you S, you guess N.
Note that I may make mistakes in calculating, but I promise that if I’m wrong, my result and the correct result will differ in exactly one single digit, and the number of digits is always correct(no missing or extra digits). That means, I will NOT get ‘terribly wrong result’ such as 3456 or 111.
Input
The first line in the input contains a positive integer T indicating the number of test cases (1<=T<=10). Each case consists of a single line containing the exact value of S. The line does not contain any character apart from digits (0,1,2...9), and will have at most 500,000 digits. Input integers do NOT contain leading zeros.
Output
For each test case, print on a single line the value of N if a unique N satisfying N^N=S can be found. Otherwise, print -1 in the corresponding line, showing that I made a mistake in calculating.
Sample Input
4
3
4
3225
387420489
Sample Output
-1
2
-1
9
Thought: for any number N greater than 3, the number of digits of N^N is different from each other. We can first check the number of digits of S to see if the number of digits is possible. Then check mod(S,N) is zero.
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm> // std::find
#include <vector> // std::vector
using namespace std; int NN[100005],length;
char number[500005]; int digits(char *str){
int ans = std::distance(NN,std::find(NN+3,NN+100002,length)); //find the index of element in an array
if(ans) return ans;
return -1; }
int verify(char *str,int res){
int n=0;
for(int i=0;i<length;i++){
n=n*10+number[i]-'0'; // mod of large number
n%=res;
}
if(n%res) return 0;
return 1;
} int main(){
int T;
memset(NN,0,sizeof(NN));
for(int i=3;i<=100000;i++){
NN[i]=int(i*log10(i))+1;
}
scanf("%d",&T);
while(T–){
scanf("%s",&number);
length = strlen(number);
if(length==1){
if(number[0]=='0'){
printf("0\n");
continue;
}else if(number[0]=='1'){
printf("1\n");
continue;
}else if(number[0]=='4'){
printf("2\n");
continue;
}else{
printf("-1\n");
continue;
}
}else{
int result = digits(number);
if(result>0) {
if(verify(number,result))
printf("%d\n", result);
else printf("-1\n");
}
else printf("-1\n");
} }
return 0;
}
ZOJ1238 Guess the Number的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- java.io中流的操作:字节流、字符流
java.io中流的操作:字节流.字符流(1)使用File类打开一个文件(2)通过字节流或字符流的子类指定输出的位置(3)进行读/写操作(4)关闭输入/输出 1.字节流:主要是byte类型数据,以by ...
- 编码为multipart/form-data自定义类型(包括文件)如何自动绑定到webapi的action的参数里
application/x-www-form-urlencoded与 multipart/form-data: Fom表单中如果没有type=file的控件,用默认的application/x-www ...
- RHEL 6.3安装(超级详细图解教程)[转载]
附:RHEL6.3下载地址 32位:http://rhel.ieesee.net/uingei/rhel-server-6.3-i386-dvd.iso 64位:http://rhel.iee ...
- Apache Ant运行时Unable to locate tools.jar解决方法
下载Apache Ant 一.解压ant安装包在D:\ant下 二.环境变量配置 ANT_HOME D:\ant\apache-ant-1.9.0 CLASSPATH ;%ANT_HOME%lib; ...
- ie浏览器兼容性快速处理小招
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android 禁用以及捕捉home键
最近要做个小项目,其中有需要禁止home键的需求,一开始以为不可以,感觉得root一下才行,后来查了一下,发现还是不少朋友都实现了这个功能,现在也引用一下,供大家参考一下: 1. 在activity中 ...
- c# access插入null值
c# 插入access数据库 提示错误: Parameter @DeviceLocation has no default value. 参数@DeviceLocation 的有没有默认值. Stri ...
- Coudera-Manager/CDH的安装和部署
由于之前部署的集群采用的是用apache hadoop的方式来实现,但是考虑到运维的成本问题,下面将apache hadoop转换成cloudera cdh.下面主要讲解一下cloudera cdh的 ...
- 重拾java系列一java基础(1)
前言,不知不觉,从接触java到工作至今已有两年的时间,突然感觉自己的基础知识还很薄弱,有些知识虽然知道,但是停留在表面上,没有深挖,或者实践过,感觉掌握的很肤浅,而且时间一长,就觉得忘记了,我觉得这 ...
- (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解
原文:http://blog.csdn.net/totogo2010/article/details/8048652 iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: ...