Cod sursa(job #73369)

Utilizator peanutzAndrei Homorodean peanutz Data 18 iulie 2007 03:22:12
Problema Descompuneri Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <stdio.h>
#include <math.h>

#define NMAX 1000

long long n;
int k;
long long d[NMAX], h;
int a[NMAX][NMAX];
int last[NMAX][NMAX];

int aux;

void findDiv()
{
	int i, until = n, h = -1;
	d[++h] = 1;
	if(!(n%2))
		d[++h] = 2;

	for(i = 3; i <= until; ++i)
		if(!(n%i))
			d[++h] = i;
	aux = h;
}

int binarySearch(long long x)
{
	int st = 0, dr = h, m;
	dr = aux;
	while(st <= dr)
	{
		m = (st+dr) >> 1;
		if(d[m] > x)
			dr = m-1;
		else if(d[m] < x)
			st = m+1;
		else
			return m;
	}
	return -1;
}

void buildNr()
{
	int i, j, res, k = 0;

	k = 0;
	for(j = 1; j <= h; ++j)
		++a[0][j];//, last[0][j] = 0;

	for(i = 1; i <= h; ++i)
	{
		//k = 1;
		for(j = h; j; --j)
		{
			a[i][j] = a[i][j+1];

			if((d[i]%d[j]))
				continue;


			res = d[i]/d[j];
			for(k = last[i-1][j]; d[k] != res; ++k);
			last[i][j] = k;

			if(!(d[i]%d[k]))
			{
				a[i][j] += a[k][j];

			}
		}
	}
}

int main()
{
	freopen("desc.in", "r", stdin);
	freopen("desc.out", "w", stdout);

	scanf("%lld%lld", &n, &k);

	findDiv();

	h = aux;

	buildNr();

	printf("%d\n", a[h][1]);

    return 0;

	int i, aux;
	int crt = h;
	while(n != 1 && n != 0)
	{
		for(i = 1; i <= h && n != 1; ++i)
		{
			if((n % d[i]))
				continue;
			aux = last[ crt ][ i ];

			if(a[ aux ][i] >= k)
			{
				printf("%d ", d[i]);

				crt = aux;

				n /= d[i];
				--i;
			}
			else
				k -= a[ aux ][i];
		}
	}


	return 0;
}