Processing math: 100%

Sunday, 17 April 2022

A Fun Problem - Math

Problem Statement

JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an integer n, you can perform the following operations zero or more times:

mul x: multiplies n by x (where x is an arbitrary positive integer).

sqrt: replaces n with sqrt(n) (to apply this operation, sqrt(n) must be an integer). You can perform these operations as many times as you like. What is the minimum value of 𝑛, that can be achieved and what is the minimum number of operations, to achieve that minimum value?

Apparently, no one in the class knows the answer to this problem, maybe you can help them?

Input The only line of the input contains a single integer n (1<=𝑛<=106) — the initial number.

Output Print two integers: the minimum integer 𝑛 that can be achieved using the described operations and the minimum number of operations required.

Solution

We can prime-factorize n and get pa11pa22...pakk. At the end, we won't be able to remove the prime factors so the answer must be in the form of p1p2...pk. If ai is a power of 2, then we can apply sqrt operation and make an:=an2. Otherwise, we can use the first operation to make all ai equal to 2X. Therefore, the number of operations is either X or X+1.

vector<long long> factorize(int x) {
    vector<long long> res;
    for (int y = 2; y * y <= x; y++) {
        if (x % y) continue;
        while(x % y == 0) {
            res.push_back(y);
            x /= y;
        }
    }
    if (x > 1) res.push_back(x);
    return res;
}

void solve() {
    int n; cin >> n;
    vector<long long> f = factorize(n);
    map<long long , long long > m;
    for (auto x : f) m[x]++;
    long long  mx = 0;
    for (auto x : m) mx = max(mx, x.second);
    long long  b = 0;
    while ((1 << b) < mx) b += 1;
    long long  ops = b, all_same = 1;
    for (auto x : m) {
        if (x.second != (1 << b)) {
            all_same = 0;
            break;
        }
    }
    ops += all_same == 0;
    long long p = 1;
    for (auto x : m) p *= x.first;
    cout << p << " " << ops << endl;
}

A Fun Problem - Math

# Problem Statement JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today t...