Cod sursa(job #1979254)

Utilizator trifangrobertRobert Trifan trifangrobert Data 10 mai 2017 08:36:01
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int Number_of_0(int x)
{
	int k = 0, a = 5;
	while (x / a)
	{
		k += x / a;
		a *= 5;
	}
	return k;
}

int Binary_Search(int p)
{
	int st = 1;
	long long dr = 400000020, mij = (st + dr) / 2;
	long long Min,x,ok=0;
	while (st <= dr)
	{
		x = Number_of_0(mij);
		if (p == x)
		{
		    ok=1;
			mij-=mij%5;
			break;
		}
		if (p > x)
		{
			st = mij + 1;
			mij = (st + dr) / 2;
		}
		if (p < x)
		{
			dr = mij - 1;
			mij = (st + dr) / 2;
		}
	}
	if(ok)
        return mij;
    return -1;
}

int main()
{
	f >> p;
	if(p==0)
        g << 1 << "\n";
    else
        g << Binary_Search(p) << "\n";
	return 0;
}