Cod sursa(job #1329953)

Utilizator codrut_grosuGrosu Codrut-Cristian codrut_grosu Data 30 ianuarie 2015 02:12:53
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
# include <iostream>
# include <fstream>


int main () {

	std :: ifstream f ("fact.in");
	std :: ofstream g ("fact.out");

	long P;
	long nr = 0;
	long i;
	long  a;
	long nr1 ;

	f >> P;
	f.close ();

	if ( P < 0 ) {
		g << -1 << '\n' ;
		g.close ();
		return 0;
	}
	if ( P == 0 ) {
		g << 1;
	} else {
		a = 5;

		while (true) {
            i = a;
			nr += 5; 
            if ( i % 5 == 0 ) {
                while ( i % 5 == 0 ) {
                    i /=5;
                    nr ++;
                }
            }
 
            if ( nr == P ) {
                g << a*5;
                break;
            } else if ( nr > P ) {
            	nr1 = 0;
				i = a;
				while ( i % 5 == 0 ) {
					nr1 ++ ;
					i /= 5;
				}
				if ( nr - nr1 - 1 == P ) {
					g << ( a - 1) * 5;
					break;
				} else {
					nr -= nr1;
					a -= 1;
					nr --;
					while ( nr > P && a % 5 != 0 ) {
						a --;
						nr -- ;
					}
					if ( nr == P ) {
						g << a*5;
						break;
					} else {
						g << -1;
						break;
					}
				}
            }
            a += 5 ;
        }
    }
 
    g << '\n';
 	g.close ();

    return 0;
}