Cod sursa(job #1461134)

Utilizator cuna_christianCuna-Mic Mihai-Cristian cuna_christian Data 14 iulie 2015 20:24:00
Problema Factorial Scor 95
Compilator c Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>

int64_t end_zero(int64_t p){

    int64_t zeroes = 0;

    while(p > 0){
        zeroes += p / 5;
        p /= 5;
    }

    return zeroes;
}

bool is_lower(int64_t n, int p){
    --n;
    if(end_zero(n) == p)
        return true;
    else
        return false;
}

int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    int p, ok = 0;
    int64_t a = 0 , b = 100000000000, c;
    scanf("%d", &p);

    while(a < b){
        c = (a+b) / 2;

        if(end_zero(c) == p){
            while(is_lower(c, p))
                --c;
            printf("%"  PRId64, c);
            ok = 1;
            break;
        }
        else {
            if(end_zero(c) < p)
                a = c + 1;
            else
                if (end_zero(c) > p)
                    b = c - 1;
        }
    }

    if(!ok)
        printf("-1");

    return 0;
}