Cod sursa(job #1726131)

Utilizator Tudor_CandeaCandea Tudor Tudor_Candea Data 7 iulie 2016 13:12:12
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin ("fact.in");
ofstream fout ("fact.out");

long long p;

int main()
{
    int n=6, n1=0, v=25, v1=1, x, x1, x2, v2, ct=-1, ct1=0;
    fin>>p;

    while(p>n)
    {
        n1=n;
        n=5*n+1;
        v1=v;
        v=v*5;
        ct1++;
    }
    x2=n-1;
    v2=v;

    if(p==0)
    {
        fout<<1;
        return 0;
    }

    if(p==1)
    {
        fout<<5;
        return 0;
    }

    if(p==2)
    {
        fout<<10;
        return 0;
    }

    if(p==n)
        fout<<v;
    else
    {
        while(n!=p and n1!=p)
        {
            x2=x2/5;
            v2=v2/5;
            x=x2;
            x1=v2;
            n=n-1;

            if(x==1)
            {
                if(p>=n-ct1)
                    break;
            }

            while(n!=(n1+x))
            {
                n1=n1+x;
                v1=v1+x1;
                n=n-x;
                v=v-x1;
                if(n1>p)
                {
                    n1=n1-x;
                    v1=v1-x1;
                }
                if(n<p)
                {
                    n=n+x;
                    v=v+x1;
                }
                if(n==p and x==1)
                {
                    ct=v;
                    break;
                }
                if(n1==p and x==1)
                {
                    ct=v1;
                    break;
                }
            }
            ct1--;
        }
        fout<<ct;
    }
    return 0;
}