Cod sursa(job #1023191)

Utilizator PsychoAlexAlexandru Buicescu PsychoAlex Data 6 noiembrie 2013 16:01:27
Problema Factorial Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <iostream>
#include <fstream>

std::ifstream fin("fact.in");
std::ofstream fout("fact.out");

int p;

void citire()
{
    fin>>p;
}

void check()
{
    int nrOf5powers = p / 5;
    long long lastVal = 5;
    long long nrOf0 = 0;//nrOf5powers * (nrOf5powers + 1) / 2;
    long long nr = p * 5;

    long long lastNr = 0;

    while(lastVal <= nr && lastVal > 0)// && nrOf0 != p)
    {
//        std::cout<<nrOf0<<'\n';
        nrOf0 += (nr / lastVal);
        if(nrOf0 > p)
        {
            nrOf0 = 0;//-= (nr / lastVal);
            lastNr = nrOf0 - 1;
            nr -= 5;
            lastVal = 5;// /= 5;
//            std::cout<<"here1: "<<nrOf0<<' '<<nr<<' '<<lastVal<<'\n';

//            while(lastVal < nr)
//            {
//                nrOf0 += (nr / lastVal);
//                lastVal *= 5;
//            }
//            std::cout<<"here2: "<<nrOf0<<' '<<nr<<' '<<lastVal<<'\n';
            continue;
        }
//        std::cout<<"here3: "<<nrOf0<<' '<<nr<<'\n';
        lastNr = nrOf0;
        lastVal *= 5;
    }

//    while(lastVal < p)
//    {
//        nrOf0 += (p / lastVal);
//        lastVal *= 5;
//    }
    if(nrOf0 != p)
    {
        fout<<-1<<'\n';
        return;
    }
    fout<<nr<<'\n';
}

int main()
{
    citire();
    check();
    return 0;
}