Cod sursa(job #940278)

Utilizator classiusCobuz Andrei classius Data 15 aprilie 2013 22:40:53
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.58 kb
#include <fstream>

using namespace std;

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

unsigned pow(unsigned b);

int main()
{

unsigned p,i,s=0,ok=0;
unsigned v[100],cnt[100]={};

f>>p;

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

v[1]=1;
i=1;

while(p>=v[i])
{
    i++;
    v[i]=v[i-1]*5+1;
}
i--;

while(i&&p)
{
while(p>=v[i])
{ s+=pow(i); p-=v[i];cnt[i]++;}
i--;
}

for(i=1;i<=20;i++) if(!(cnt[i]%5)&&cnt[i]) ok=1;

if(ok) g<<-1;
else g<<s;


return 0;
}

unsigned pow(unsigned b)
{
unsigned s=1;

while(b)
{
s*=5;
b--;
}

return s;
}