Cod sursa(job #1100692)

Utilizator negreadumitruNegrea Dumitru negreadumitru Data 7 februarie 2014 12:27:22
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include<fstream>
using namespace std;
int p, st, dr, mid, c, z;
ifstream fin("fact.in");
ofstream fout("fact.out");
int main(){
    fin>> p;
    st = 1;
    dr = p * 5;
    while(st <= dr){
        mid = (st + dr) / 2;
        z = 0;
        c = 5;
        while(c <= mid){
            z+= mid / c;
            c = c * 5;
        }
        if(z >= p){
            dr = mid - 1;
        }
        else{
            st = mid + 1;
        }
    }
    z = 0;
    c = 5;
    while(c <= st){
        z+= st / c;
        c = c * 5;
    }
    if(z == p){
        fout<< st;
    }
    else{
        fout<< -1;
    }
    return 0;
}