Cod sursa(job #1104827)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 11 februarie 2014 08:06:20
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include<fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int p,st,dr,mid,c,z,n;
int main(){
    fin>>n;
    st=1;
    dr=n*5;
    while(st<=dr){
        mid=(st+dr)/2;
        z=0;
        c=5;
        while(c<=mid){
            z+=mid/c;
            c=c*5;
        }
        if(z>=n){
            dr=mid-1;
        }
        else{
            st=mid+1;
        }
    }
    z=0;
    c=5;
    while(c<=st){
        z+=st/c;
        c=c*5;
    }
    if(z==n){
        fout<<st;
    }
    else{
        fout<<-1;
    }
    return 0;
}