Cod sursa(job #1893754)

Utilizator kywyPApescu tiGEriu kywy Data 25 februarie 2017 23:26:01
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include<cstdio>
using namespace std;
int p;
FILE* in = fopen("fact.in", "r");
FILE* out = fopen("fact.out", "w");
int mc(int a)
{
    int m = a / 5;
    int x = 5;
    int s = m;
    while(m)
    {
        x *= 5;
        m = a / x;
        s += m;
    }
    return s;
}
int cautbin(int a)
{
    int start = 0, step = 1;
    for(; step <= p * 5; step <<= 1)
    {

    }
    for(; step; step >>= 1)
    {
        int index = start + step;
        if(p < mc(index)) continue;
        if(mc(index) <= a) start = index;
    }
    if(mc(start) != a) return -1;
    else return start;
}
int main()
{
    fscanf(in, "%d", &p);
    int v = cautbin(p);
    if(v != -1) if(v % 5 != 0) v -= (v % 5);
    if(p == 0) v = 1;
    fprintf(out, "%d", v);
}