Cod sursa(job #273170)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 8 martie 2009 11:48:00
Problema GFact Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.8 kb
# include <stdio.h>
# include <math.h>

# define MAXP 15000


long int ciur[MAXP+1],ciurlen;
long int p,q;
long int fact[MAXP+1],factlen,rep[MAXP+1];
long long int solp[MAXP+1],sol;

long long int max(long long int a, long long int b) {if (a>b) return a; return b;}

void citire()
{
     FILE *f=fopen("gfact.in","r");
     fscanf(f,"%ld%ld",&p,&q);
     fclose(f);
}

long int prim(long int a)
{
     long int i;
     for (i=1;i<=ciurlen&&ciur[i]*ciur[i]<=a;i++)
         if (a%ciur[i]==0) return 0;
     return 1;
}

void make_ciur()
{
     long int i,upper_bound=(long int)sqrt(p)+1;
     ciur[1]=2;
     ciurlen=1;
     //printf("%ld\n",upper_bound);
     for (i=3;i<=upper_bound;i+=2)
         {
         if (prim(i)) ciur[++ciurlen]=i;
         }
}

void factorizeaza_p()
{
     long int i;
     for (i=1;p!=1&&i<=ciurlen;i++)
         {
         if (p%ciur[i]==0)
            {
            fact[++factlen]=ciur[i];
            rep[factlen]=0;
            while (p%ciur[i]==0) 
                  {
                  p/=ciur[i];
                  rep[factlen]++;
                  }
            }
         if (p!=1&&ciur[i]*ciur[i]>p)
            {
            fact[++factlen]=p;
            rep[factlen]=1;
            break;
            }
         }
}
           
void inmulteste_rep_q()
{
     long int i;
     for (i=1;i<=factlen;i++) rep[i]*=q;
}

long int puterea(long long int x, long int p)
{
     long int sol=0;
     while (x) {sol+=x/p;x/=p;}
     return sol;
}

long long int searchbin(long long int li, long long int lf,long int target, long int base)
{
     printf("%ld:%ld\n",target,base);
     if (li>lf) return 0;
     long long int m=(li+lf)/2;
     long int pm=puterea(m,base),pm1=puterea(m-1,base);
     if (pm>=target&&pm1<target) return m;
     if (pm<target) return searchbin(m+1,lf,target,base);
     return searchbin (li,m-1,target,base);
}

void calculeaza()
{
     long int i;long long int aux;
     for (i=1;i<=factlen;i++) //pt fiecare factor prim
         {
         aux=(long long int)fact[i];
         printf(" aux: %lld\n",aux);
         aux*=rep[i];
         if (rep[i]<=fact[i]) solp[i]=aux;
         else solp[i]=searchbin(1,aux,rep[i],fact[i]);
         printf("%lld\n",solp[i]);
         }
     //getchar();
}

void scrie_fisier(long long int sol)
{
     FILE *g=fopen("gfact.out","w");
     fprintf(g,"%lld\n",sol);
     fclose(g);
}

void get_solution()
{
     sol=solp[1];
     long int i;
     for (i=2;i<=factlen;i++)
         sol=max(sol,solp[i]);
}

int main()
{
    citire();
    make_ciur();
    factorizeaza_p();
    inmulteste_rep_q();
    //printf("%ld %ld\n",factlen,fact[factlen]);
    calculeaza();
    get_solution();
    scrie_fisier(sol);
    //getchar();
    return 0;
}