Cod sursa(job #809503)

Utilizator hrazvanHarsan Razvan hrazvan Data 8 noiembrie 2012 16:42:41
Problema GFact Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <stdio.h>
#include<math.h>

int nr(int n,int b)
{
    long long a=0;
    while(n>0)
    {
        a+=n/b;
        n/=b;
    }
    return a;
}

int caut(int n,int b)
{
    int pas=1,a=0,v=0;
    long long i,ii;
    while(b>0)
    {
        a=i=ii=0;
        pas=1;
        while(a<=b)
        {
            a=nr(pas,n);
            if(a<=b)
            {
                i=pas;
                ii=a;
            }
            pas*=n;
        }
        b-=ii;
        v+=i;
    }
    return v;
}

int main()
{
    FILE *in,*out;
    int a,i,c;
    long long max=0,umax=0,b,ad=0;
    in=fopen("gfact.in","r");
    out=fopen("gfact.out","w");
    fscanf(in,"%d %I64d",&a,&b);
    while(a%2==0&&a!=0)
    {
        a/=2;
        ad+=1;
    }
    if(ad>1)
    {
        max=caut(2,ad*b);
        if(max>umax)
        {
            umax=max;
        }
    }
    c=sqrt(a);
    for(i=3;i<=c&&a>1;i+=2)
    {
        ad=0;
        while(a%i==0)
        {
            a/=i;
            ad+=1;
        }
        if(ad>1)
        {
            max=caut(i,b*ad);
            if(max>umax)
                umax=max;

        }
    }
    if(a>1)
    {
        max=caut(a,b);
        if(max>umax)
            umax=max;
    }
    fprintf(out,"%I64d",umax);
    return 0;
}