Cod sursa(job #481175)

Utilizator ariel_roAriel Chelsau ariel_ro Data 30 august 2010 19:52:17
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <iostream>
#include <fstream>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;

int factZero(int n)
{
    if (n >= 0 && n < 5) return 0;

    int k = 0;
    int c = 1;
    while (c < n)
    {
        c *= 5;
        k++;
    }

    int ans = 0;
    for (int i = 1; i <= ((c == n) ? k : k - 1); i++)
        ans += floor((double)n / (double)pow(5, i));

    return ans;
}

int getN(int p, int pr, int ul)
{
    if (p == 0) return 1;

    if (pr == ul) return -1;

    int mij = (pr + ul) / 2;
    if (factZero(mij) < p)
        return getN(p, mij + 1, ul);
    else
        if (factZero(mij) > p)
            return getN(p, pr, mij);
        else
            return mij;
}

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

    int p;
    f>>p;
    int candidate = getN(p, 0, (LONG_MAX / 2));

    if (candidate != 1)
    {
        while (factZero(--candidate) == p); // maxim 5
        cout<<(candidate + 1)<<endl;
        g<<(candidate + 1)<<endl;
    }
    else
    {
        cout<<candidate<<endl;
        g<<candidate<<endl;
    }

    return 0;
}