Cod sursa(job #1704485)

Utilizator xtreme77Patrick Sava xtreme77 Data 18 mai 2016 21:01:39
Problema Factorial Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 1.33 kb
//package factorial;

import java.util.*;
import java.io.*;
 
/***
 * 
 * @author Patrick
 *
 */
  
public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new BufferedInputStream(new FileInputStream("fact.in")));
  
        long P = scanner.nextLong() ;
        PrintStream output = new PrintStream(new FileOutputStream("fact.out"));
        if ( P == 0 ) {
        	output.print( 1 ); 
        	return ;
        }
        long st = 1 ; 
        long dr = 1000000014 ;
        long solutie = -1 ;
        while ( st <= dr ) {
        	long mij = ( st + dr ) / 2 ; 
        	if ( nrzero ( mij ) >= P ) {
        		solutie = mij ; 
        		dr = mij - 1 ; 
        	}
        	else {
        		st = mij + 1 ; 
        	}
        }
        if ( solutie == -1 ) {
        	output.print ( solutie ) ;
        }
        else if ( nrzero ( solutie ) != P ) {
        	output.print ( -1 ) ;
        }
        else {
        	while ( solutie % 5 != 0 ) {
        		solutie = solutie - 1 ;
        	}
        	output.print ( solutie ) ;
        }
    }
    public static long nrzero ( long a )
    {
    	long p = 5 ; 
    	long s = 0 ;
    	while ( a / p != 0 ) {
    		s = s + a / p ; 
    		p *= 5 ; 
    	}
    	return s ;
    }
}