(SPOJ) 1752 - Fatorial - Solução

import java.io.*;
import java.util.*;
import java.math.*;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.processa();

        System.exit(0);
    }
       
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        long[] resultadosFatoriais = new long[1000001];
       
        long multiplica = 1;
        for (int i = 1; i <= 1000000; i++) { 
            multiplica *= i; 
           
            while (multiplica%10 == 0) {
                multiplica /= 10;
            }
           
            multiplica %= 1000000;
            resultadosFatoriais[i] = multiplica%10;
        }

        int valor;
        int contador = 0;
        String line = "";
        while ((line = br.readLine()) != null) {
            valor = Integer.parseInt(line);
             
            contador++;
            bw.write("Instancia " + contador + "\n" + resultadosFatoriais[valor] +"\n\n");
        }

        bw.flush();       
        bw.close();
     
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução