Cod sursa(job #425029)

Utilizator Cristy94Buleandra Cristian Cristy94 Data 25 martie 2010 13:57:07
Problema Descompuneri Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.81 kb
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
FILE *f=fopen("desc.in","r");
FILE *g=fopen("desc.out","w");
long long divs[3001],n,k,nrdivs,i,j,p,x,y,u,m;
int a[3001][3001];

int cmp(const void  *a,const void  *b)
    {
       long long x = *((long long*)a);
       long long y = *((long long*)b);
       if (x < y)
          return -1;
       if (x == y)
          return 0;
      return 1;
 }

void divide(){
	long long  d;
	for(d=2;d<=sqrt(n);d++)
		if(n%d==0){
			divs[0]++;
			divs[divs[0]]=d;
		}
nrdivs=divs[0];
for(d=1;d<=nrdivs;d++)
 if(n/divs[d]!=divs[d])
{ divs[0]++;
  divs[divs[0]]=n/divs[d];
}
divs[0]++;
divs[divs[0]]=n;
nrdivs=divs[0];
qsort(divs+1, nrdivs, sizeof(long long),cmp);	
}

long long caut(long long y){
if(y==1)
return 1;
	p=1;
	u=nrdivs;
	while(p<=u){
		m=(p+u)/2;
		if(divs[m]>y)
		 u=m-1;
		else if(divs[m]<y)
			  p=m+1;
		else return m;}
return 0;	
}

long long caut1(long long y){
if(y==1)
return 0;
	p=1;
	u=nrdivs;
	while(p<=u){
		m=(p+u)/2;
		if(divs[m]>y)
		 u=m-1;
		else if(divs[m]<y)
			  p=m+1;
		else return m;}
return 0;	
}
void recons(int lvl,int k){
	if(lvl>=1){
	long long i=1;
   while(a[lvl][i]<k)
	   { i++;
		 k-=a[lvl][i];
	   }
   recons(caut1(divs[lvl]/divs[i]),k);
   fprintf(g,"%lld ",divs[i]);
	}

}
int main(){
	//citire
fscanf(f,"%lld%lld",&n,&k);
    //divsizorii lui n
divide();
    //initializam prima linie cu 1
for(i=1;i<=nrdivs;i++)
	 a[1][i]=1;

for(i=2;i<=nrdivs;i++)
	for(j=1;j<=nrdivs;j++){
		a[i][j]=a[i][j-1]; //valoarea anterioara
		if(i>=j&&divs[i]%divs[j]==0) //daca se imparte
		{  y=divs[i]/divs[j]; //trebuie sa cautam pozitia divsizorului p
		   x=caut(y);
		   a[i][j]+=a[x][j];
		}
}

//reconstituire
fprintf(g,"%d\n",a[nrdivs][nrdivs]);	
recons(nrdivs,k);
return 0;
}