Cod sursa(job #1647086)

Utilizator TataruTataru Mihai Tataru Data 10 martie 2016 18:57:25
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <iostream>
#include <cstdio>

const char inFile[] = "fact.in";
const char outFile[] = "fact.out";
const unsigned long long inf = 1<<61;

using namespace std;

unsigned long long p, n;

int main()
{
    unsigned long long a, b, mid, x, y;
    freopen(inFile, "r", stdin);
    freopen(outFile, "w", stdout);
    b = inf;
    scanf("%d", &p);
    if(p == 0) printf("%d", 1);
    if(p == 1) printf("%d", 5);
    while(a <= b){
        mid = (a + b) / 2;
        x = 5;
        y = 0;
        while(x <= 5 * mid) {
            y += mid / x;
            x = 5 * x;
        }
        if(p == y) {
            while(mid % 5 != 0)
                    mid--;
            break;
        }
        else{
            if(p > y)
                a = mid + 1;
            else
                b = mid - 1;
        }

    }
    if(a <= b)
        printf("%d", mid);
    else
        printf("%d", -1);
}