Cod sursa(job #1499721)

Utilizator klamathixMihai Calancea klamathix Data 11 octombrie 2015 00:13:11
Problema NumMst Scor 36
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <bits/stdc++.h>
using namespace std;

int main() {
    #ifdef INFOARENA
    ifstream cin("nummst.in");
    ofstream cout("nummst.out");
    #endif

    int n; cin >> n;
    
    int g = 0;

    for(int d = 2; d <= n; ++d)
        if(n % d == 0) {
            g = n / d;
            break;
        }
    
    n /= g;
    vector<int> primes;
    
    auto isPrime = [] (int a) {
        for(int i = 2; i * i <= a; ++i)
            if(a % i == 0)
                return false;
        return true;
    };
    
    primes.push_back(1);

    for(int i = 2; i <= n; ++i)
        if(isPrime(i))
            primes.push_back(i);
    
    vector<double> best(n + 1, 0);
    vector<int> from(n + 1, 0);
    
    for(auto prime : primes) {
        int current = prime;
        
        vector<double> newBest = best;
        vector<int> newFrom = from;
    
        while(current <= n) {

            vector<double> temp = best;
            vector<int> tempFrom = from;

            double logC = log(current);
            for(int i = n - current; i >= 0; --i) {
                if(current == n && i == 0)
                    continue;
                if(temp[i] + logC > temp[i + current]) {
                    temp[i + current] = temp[i] + logC;
                    tempFrom[i + current] = i;
                }
            }

            for(int i = 0; i <= n; ++i)
                if(temp[i] > newBest[i]) {
                    newBest[i] = temp[i];
                    newFrom[i] = tempFrom[i];
                };
            
            current *= prime;
            if(prime == 1)
                break;
        }

        best = newBest;
        from = newFrom;
    }
    
    for(int i = 1; i <= n; ++i)
        if(best[i - 1] >= best[i]) {
            best[i] = best[i - 1];
            from[i] = i - 1;
        }

    vector<int> ans;
    for(int now = n; now; now = from[now]) 
        ans.push_back((now - from[now]) * g);

    for(auto temp : ans)
        cout << temp << " ";
    cout << "\n";
}