Cod sursa(job #1794358)

Utilizator TonisonIlle Antoniu Nicolae Tonison Data 1 noiembrie 2016 11:14:05
Problema Factorial Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

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

int x=0;
vector<long long> cc(20);
void cinci()
{
    cc[0]=5;
    for(int i=1; i<20; ++i)
    {
        cc[i]=cc[i-1]*5;
    }
}

int ff(int n){
    int rez = 0;
    while(n)
    {
        rez+=n/5;
        n/=5;
    }
    return rez;
};

int fact(long long n, long long put)
{
    int rez=0;
    while(n%put==0)
    {
        rez++;
        n/=put;
    }
    return rez;
}

int main()
{
    long long n,nn;
    f>>n;
    nn=n;
    if(n==0)
    {
        g<<1;
        return 0;
    }
    cinci();
    for(int i=0; i<15; ++i)
    {
        n-=fact(n,cc[i]);
    }
    if(ff(n*5)==nn)
    {
        g<<n*5;
    }
    else
        g<<-1;
    return 0;
}
// 5 10 15 20 25 30 35 40 45 50
// 1  2  3  4  6  7  8  9 10 12