Cod sursa(job #1465872)

Utilizator KusikaPasa Corneliu Kusika Data 28 iulie 2015 10:25:05
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
unsigned long int p;

int Caut(unsigned long long int a, unsigned long long int b)
{
    unsigned long long int mid, k=1, pw = 0, pw1 = 0, pw2 = 0;
    //Find the middle point
    mid = (a + b) / 2;
    //Find the number of 5s
    while (k * 5 <= mid + 5){
            k = k * 5;
            pw = pw + mid / k;
            pw1 = pw1 + (mid - 5) / k;
            pw2 = pw2 + (mid + 5) / k;
        }
    //Return the result if it is what we are searching
    if (pw == p){
        return mid-mid%5;
    }
    else
    {

        if ((pw > p && pw1 < p) || (pw < p && pw2 > p)) return -1;
        //Continue searching
        if (pw < p)
            return Caut(mid+1,b);
        else return Caut(a,mid-1);
    }
}

int main()
{
    ifstream f1("fact.in");
    ofstream f2("fact.out");
    f1 >> p;
    if (p == 0) f2 << 1;
    else f2 << Caut(0,pow(10,8));
    return 0;
}