Solution to a problem UVa10791

Title effect a plurality of sets of data, each set of data is given a positive integer \ (n-\) , requesting a set of numbers \ (A_1 \ cdots a_m \) , satisfies \ (LCM_ {k = 1} ^ ma_k = n \) and \ (\ sum_ {k = 1 } ^ ma_k \) minimum.

Analysis We study two numbers as an example. Assumes \ (LCM (a, b) = n \) then if \ (the GCD (A, B) \ NEQ. 1 \) , there are \ (LCM (\ frac {a } {gcd (a, b)}, b) n-= \) , and \ (A + B> \ {A} {FRAC GCD (A, B)} + B \) . Therefore, if and only if \ (gcd (a, b) = 1 \) the optimal time. For the case where a plurality of numbers, but also if and only if \ (\ prod_ {k = 1 } ^ ma_k = n \) Optimal. For positive integer \ (A_1 \ GEQ 2, A_2 \ GEQ 2, \ cdots, a_m \ GEQ 2 \) , there is always \ (\ sum_ {k = 1 } ^ ma_k \ leq \ prod_ {k = 1} ^ ma_k \) . Therefore, if may be \ (n-\) prime factor decomposition \ (n-= \ prod_ {K =. 1} ^ MP_K ^ {q_k} \) (where \ (\ forall i \ in \ mathbb {N +}, p_i \) is a prime number), if and only if \ (\ forall i \ in \ mathbb {N +}, a_i = p_i ^ {q_i} \) when the optimal solution.

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll t, tot, n, ans;
map<ll, ll> m;

ll QuickPow(ll a, ll b)
{
    ll res = 1;
    while(b) {
        if(b & 1) res *= a;
        a = a * a, b >>= 1;
    }
    return res;
}

int main()
{
    while(~scanf("%lld", &n) && n) {
        m.clear(), ans = 0, tot = 0;
        
        for(ll i = 2; i * i <= n && n > 1; ++i)
            while(n % i == 0) ++m[i], n /= i;
        if(n > 1) m[n] = 1;
        
        map<ll, ll>::iterator it = m.begin();
        while(it != m.end()) {
            ans += QuickPow(it->first, it->second);
            ++it, ++tot;
        }
        
        if(tot < 2) ans += 2 - tot;
        printf("Case %lld: %lld\n", ++t, ans);
    }
}

Guess you like

Origin www.cnblogs.com/whx1003/p/11964885.html