Cod sursa(job #970520)

Utilizator danlexDan Alexandru danlex Data 7 iulie 2013 01:56:01
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
#include <fstream>
using namespace std;

int z, n, estN;
bool DEBUG = false;

void print(){
	cout << "z: " << z <<  " n: " << n << " estN: " << estN << " d: " << n - estN << endl;
}

void read(){
    ifstream fi("fact.in");
    fi >> z;
    fi.close();
}

void write(){
    ofstream fo("fact.out");
    fo << n;
    fo.close();
}

int zeroN(int n){
	int z = 0;
	int i5 = 0;
	for(int i = 5; i <= n; i+=5){
		z ++;
		i5 = i/5;		
		while (i5 % 5 == 0 && i5 > 0){
			z ++;
			i5 /= 5;
		}
	}
	return z;
}

int findN(int estStart, int estEnd){
	if (estEnd == 0){
		return 1;
	}
	if (estEnd - estStart < 2){
		return -1;
	}
	int estMidd = estStart + (estEnd - estStart) / 2;
	int estZ = zeroN(estMidd);
	if(DEBUG) cout << estStart << " " << estEnd << " " << estN << endl;
	if (estZ < z){
		return findN(estMidd, estEnd);
	} else if (estZ > z){
		return findN(estStart, estMidd);
	} else {
		estMidd -= estMidd % 5;
		return estMidd;
	}
}

void compute(){
	int estStart = 4 * z - 11;
	int estEnd = 4 * z + 11;
	n = findN(estStart, estEnd);
	estN = 4 * z + 1;
	if(DEBUG) print();
}

int main(void){
    read();
    compute();
	write();
    return 0;
}