Cod sursa(job #1591887)

Utilizator razvandRazvan Dumitru razvand Data 6 februarie 2016 20:06:54
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream in("fact.in");
ofstream out("fact.out");

long long pw5[15];

int main() {

    long a;
    in >> a;

    if(a == 0) {
        out << 1;
        return 0;
    }

    long long nr = 0;
    long long sec = 0;
    long fact5 = 0;

    // 400000015

    pw5[1] = 5;
    for(int i = 2; i <= 14; i++)
        pw5[i] = pw5[i-1]*5;

    //cout << pw5[14];

    //nr = 25;


    /*for(int i = 5; i < 25; i += 5) {
        fact5++;
        if(fact5 == a) {
            out << nr;
            break;
        } else if(fact5 > a) {
            out << "-1";
            break;
        }
    }*/


    while(true) {

        nr += 5;
        sec = nr;

        //int lw = lower_bound(pw5, pw5+15, nr+5) - pw5 - 1;
        int lw = 2;

        /*while(sec % pw5[lw] == 0) {
            lw++;
        }*/

        fact5 += lw-1;

        //cout << nr << " " << lower_bound(pw5, pw5+15, nr+5) - pw5 - 1 << " " << pw5[lower_bound(pw5, pw5+15, nr+5) - pw5 - 1] << "    " << fact5 << endl;

        /*while(sec%5 == 0 && sec != 0) {
            fact5++;
            sec /= 5;
        }

        cout << nr << " " << lower_bound(pw5, pw5+15, nr+5) - pw5 - 1 << " " << pw5[lower_bound(pw5, pw5+15, nr+5) - pw5 - 1] << "    " << fact5 << endl;*/

        if(fact5 == a) {
            out << nr;
            break;
        } else if(fact5 > a) {
            out << "-1";
            break;
        }

    }

    return 0;
}