Cod sursa(job #585741)

Utilizator DraStiKDragos Oprica DraStiK Data 30 aprilie 2011 11:34:58
Problema NumMst Scor 20
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Clasele 10-12 Marime 1.44 kb
#include <algorithm>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cmath>
using namespace std;

#define MAX 10000005
#define DIM 4005


double bst_0[DIM],bst_1[DIM],lg2[DIM];
int ant_0[DIM],ant_1[DIM];
bitset <MAX> viz;
int oldN,N,M,P;

void read ()
{
    int i,j;

    scanf ("%d",&N);

    oldN=N;
    if (!(N&1))
        N=2;

    for (i=3; i<=N; i+=2)
        if (!viz[i])
        {
            if (!(N%i))
            {
                N=i;
                return ;
            }

            for (j=3*i; j<=N; j+=(i<<1))
                viz[j]=1;
        }
}

void solve ()
{
    int i,j,val;

    for (i=1; i<=N; ++i)
        lg2[i]=log (i);

    bst_0[1]=0; ant_0[1]=1;
    bst_1[1]=0; ant_1[1]=1;
    for (i=2; i<=N; ++i)
    {
        bst_1[i]=bst_1[i-1]; ant_1[i]=1;
        for (j=1; j<i; ++j)
            if (bst_0[i-j]+lg2[j]>bst_1[i])
            {
                bst_1[i]=bst_0[i-j]+lg2[j];
                ant_1[i]=j;
            }
        bst_0[i]=bst_1[i]; ant_0[i]=ant_1[i];
        if (lg2[i]>bst_0[i])
        {
            bst_0[i]=lg2[i];
            ant_0[i]=i;
        }
    }

    printf ("%d ",ant_1[N]*(oldN/N));
    for (val=N-ant_1[N]; val; val-=ant_0[val])
        printf ("%d ",ant_0[val]*(oldN/N));
}

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

    read ();
    solve ();

    return 0;
}