Cod sursa(job #2883925)

Utilizator matwudemagogul matwu Data 1 aprilie 2022 23:25:34
Problema Factorial Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int dv(int x){
    int d = 5, p = 0;
    while(x % d == 0) x /= d, p++;
    if(p) 
        return p;
}

int64_t fact(int a){
    int i; int64_t cnt = 0;
    for(i = 5; i <= a; i+=5)
        cnt += dv(i);
    return cnt;
}
int n, st = 0, dr = 100000000, mij, gasit = 0;
int64_t k;
int main(){

    fin >> n; if(n == 0) gasit = 1;
    while(st <= dr && !gasit){
        mij = (st + dr) / 2; k = fact(mij);
        if(k == n) gasit = mij;
        else if(n < k) dr = mij - 1;
        else if(n > k) st = mij + 1;
    }
    if(gasit == 1 || gasit % 5 == 0) cout << gasit;
    else{ 
        for(int i = gasit; i >= 1; i--)
            if(i % 5 == 0){
                fout << i;
                break;
            }
    }
    if(gasit == 0) fout << -1;
}