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;
    }
    
  • 相关阅读:
    TCP_UCP通信原理及案例
    JavaScript高级笔记DOM与BOM
    JavaScript基本语法,基本对象,正则表达式
    JDBC连接池&JDBCTemplate&Sping JDBC
    JDBC,JDBCUtils,JDBC控制事务
    自动化工具ansible(0——准备部署工作)
    监控软件篇——prometheus+exporter组件+grafana
    命令工具篇
    sed 命令备忘
    ES6数据分组
  • 原文地址:https://www.cnblogs.com/naive/p/3568813.html
Copyright © 2011-2022 走看看