Cod sursa(job #1878687)

Utilizator IlluminatehPinzariu Denis Stefan Illuminateh Data 14 februarie 2017 13:06:16
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>

using namespace std;

/*

P:
    0! = 0
    5! = 1
    10! = 2
    25! = 6

25  = 2 - 1 + 5 = 6
45  = 2 - 1 + 9 = 10
5   = 1 - 1 + 1 = 1
10  = 1 - 1 + 2 = 2
20

25 = 2 - 1 + 5 = 6

125 = 3 - 1 + 25


putere 5 - 1 + nr / 5 = P

*/



int silp(int nr)
{
    int p = 0;

    while(nr % 5 != 0)
        nr--;

    int putere = 5;

    while(putere <= nr)
    {
        p += nr/putere;
        putere *= 5;
    }

    return p;


}


int main()
{

    ifstream in ("fact.in");
    ofstream out ("fact.out");


    int  P;
    in>>P;


    int pas = 1<<28;
    int r = 0;

    while(pas != 0)
    {

        if( silp(r + pas) <= P)
            r+=pas;

        pas/=2;

    }


    if(P == 0)
        out<<1;
    else if ( silp(r)  == P)
        out<<r-4;
    else
        out<<-1;



    return 0;
}