zoukankan      html  css  js  c++  java
  • 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
    ");
    				continue;
    			}else if(number[0]=='1'){
    				printf("1
    ");
    				continue;
    			}else if(number[0]=='4'){
    				printf("2
    ");
    				continue;
    			}else{
    				printf("-1
    ");
    				continue;
    			}
    		}else{
    			int result = digits(number);
    			if(result>0) {
    				if(verify(number,result))
    					printf("%d
    ", result);
    				else printf("-1
    ");
    			}
    			else printf("-1
    ");
    		}
    
    
    	}
    	return 0;
    }
    
  • 相关阅读:
    IE, FireFox, Opera 浏览器支持CSS实现Alpha半透明的方法
    5个CSS3技术实现设计增强
    SQL Server 2005 中的分区表和索引
    推荐12款可用于前端开发的免费文本编辑器
    960 Grid System
    初识Byte
    在线制作网站
    sqlserver操作符篇 优化
    ASP.NET 异常处理
    Photoshop 隐藏的快捷键
  • 原文地址:https://www.cnblogs.com/naive/p/3568813.html
Copyright © 2011-2022 走看看