Cod sursa(job #1661798)

Utilizator radu.millio15Radu Millio radu.millio15 Data 24 martie 2016 10:31:55
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    FILE *fin, *fout;
    fin=fopen("fact.in", "r");
    fout=fopen("fact.out", "w");
    int p,z,med,k, lastst, st, dr, i,n;
    long long numitor;
    fscanf(fin, "%d", &z);
    n=2000000000;
    st=1;
    dr=n;
    lastst=0;
    k=5;

    while(st<=dr)
    {
        med=dr-(dr-st)/2;
        p=0;
        numitor=k;
        while(numitor<=med)
        {
            p=p+med/numitor;
            numitor=numitor*k;
        }
        if(p>=z)
        {
            lastst=med;
            dr=med-1;
        }
        else st=med+1;
    }
    p=0;
    numitor=k;
    while(numitor<=lastst)
    {
        p=p+lastst/numitor;
        numitor=numitor*k;
    }
    if(p==z)
        fprintf(fout, "%d", lastst);
    else fprintf(fout, "-1");
    return 0;
}