P8302 [CoE R4 B/Stoi2041] 龙拳

· · 题解

题面:link

算法分析

设: h(x) 为 x 的最小质因数

\therefore \frac{x}{g(x)} = h(x)\ ,\ x = h(x) \times g(x) \therefore f(x) = (h(x)+1) \times g(x) = \frac{x}{h(x)} \times (h(x) + 1)

当 h(x) = 2 时:

h(x) + 1 = 3

当 h(x) \geqslant 3 时:

\therefore \forall \; x \geqslant 2 \times k + 1\ (k \in N^*) \; , \; 2 \mid f(x)

当 h(x) = 3 时:

不妨设:x = 2^a \times 3^b \times k

\begin{aligned} f(x) & = 2^{a-1} \times 3^{b+1} \times k \\ & \dots \dots \\ f^{(a)}(x) &= 2^{0} \times 3^{b+a} \times k \\ f^{(a+1)}(x) & = 2^{2} \times 3^{b+a-1} \times k \\ f^{(a+2)}(x) & = 2^{1} \times 3^{b+a} \times k \\ f^{(a+3)}(x)& = 2^{0} \times 3^{b+a+1} \times k\end{aligned}

综上:

  1. 3 \mid k

code

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
    int x = 0,f = 1ll;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-') f = -1ll;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1ll) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return f * x;
}
inline void write(int x)
{
    if (x < 0) x = -x, putchar('-');
    if (x <= 9) putchar(x+'0');
    if (x > 9) write(x/10),putchar((x % 10) + '0');
}
int n,k,tot,ans,tmp;
int prime[SIZE],minn[SIZE];
bool is_prime[SIZE];
inline void Euler()
{
    for (int i = 2; i < (int)3e7; ++i)
    {
        if (!is_prime[i]) prime[++tot]=i,minn[i]=i;
        for (int j = 1; j <= tot && i * prime[j] < (int)3e7; ++j)
        {
            is_prime[i*prime[j]] = true;
            minn[i*prime[j]] = prime[j];
            if (i % prime[j] == 0) break;
        }
    }
}
inline int solve()
{
    n = read(); k = read(); ans = -2; tmp = 0;
    if (k % 3 != 0) return -1;
    if (n % 3 == 0 && n % 2) return 0;
    if (n % 2 && n % 3)
    {
        n = n / minn[n] * (minn[n] + 1);
        tmp++;
    }
    while (n % 2 == 0)
    {
        ans ++;
        n /= 2;
    }
    return max(0,ans) + tmp;
}
signed main()
{
    Euler();
    for (int T = read(); T; --T) write(solve()),puts("");
    return 0;
}