Cod sursa(job #862928)

Utilizator sfarma_pietreOctavian Ganea sfarma_pietre Data 23 ianuarie 2013 01:06:44
Problema Factorial Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <map>
#include <stdlib.h>
#include <sstream>
#include <stdio.h>
using namespace std;
#define PI 3.1415926535897932384626433832795

#define MAX 1001

int main() {
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);

    long long n, p, sum, aux;
    scanf("%I64d", &p);
    if (p == 0) {
        cout << "1";
        return 0;
    }
    
    int step = 5;
    
    long long found = 0;
    for (n = 4*p / 5;;) {
        sum = 0;
        aux = n / 5;
        while(aux) {
            sum += aux;
            aux /= 5;
        }
        if (sum == p) {
            found = n;
            n--;
            continue;
        }
        
        if (sum < p && found > 0) {
            printf("%I64d", found);
            return 0;
        }
        if (sum < p) {
            n += 1 << step;
            step ++; 
        }
        if (sum > p) {
            step -= 2; 
            n -= 1 << step;
        }
        
    }

    return 0;
}