Cod sursa(job #586719)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 2 mai 2011 20:21:29
Problema NumMst Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <cstdio>
#include <cmath>
#define nmax 3500
#define lmax 500

int n, d, h, f, a[nmax], rs; 
short  prev[lmax][nmax], best[nmax], nr[lmax][nmax];
double v[lmax][nmax], g, sol;
char p[nmax];

int main()
{
    freopen("nummst.in","r",stdin);
    freopen("nummst.out","w",stdout);
    scanf("%d",&n);
    int i, s, x, j;
    for (i=2; i*i<=n; i++)
    {
        if (!(n%i))
        {
            d=i;
            break;
        }
    }
    s=d;
    d=n/d;
    for (x=2; x<=s; x++)
        for (i=x+x; i<=s; i+=x) p[i]=1;
    for (i=2; i<=s; i++)
        if (!p[i]) a[++f]=i;
    if (s==2)
	{
        printf("%d %d\n",d,d);
		return 0;
    }
	for (i=1; i<=s; i++) best[i]=-1;
	for (i=1; i<=f; i++)
	{
		for (j=1; j<=s; j++) v[i][j]=-1;
		for (x=a[i]; x<=s; x*=a[i])
			for (j=s; j>=x; j--)
				if (best[j-x]!=-1)
				{
					g=v[best[j-x]][j-x]+log((double) x);
					if (g>v[i][j])
					{
						v[i][j]=g;
						nr[i][j]=x;
						prev[i][j]=best[j-x];
					}
				}
		for (j=1; j<=s; j++)
			if ((best[j]==-1 && v[i][j]!=-1) || v[i][j]>v[best[j]][j])
				best[j]=i;
	}
	for (i=2; i<=s; i++)
		if (v[best[i]][i]>sol)
		{
			rs=i;
			sol=v[best[i]][i];
		}
	i=rs;
	j=best[rs];
	while (i)
	{
		printf("%d ",nr[j][i]*d);
		x=i;
		i-=nr[j][i];
		j=prev[j][x];
	}
	for (i=1; i<=s-rs; i++) printf("%d ",d);
    return 0;
}