题解:AT_arc200_b [ARC200B] LCM

· · 题解

注意力不够惊人该怎么做这道题?

下文用 A,B,C 代替原文的 A_1,A_2,A_3。

考虑构造 p\times 10^w 的形式,其中 p 是素数。

若 C<\max(A,B) 或 C>A+B 则无解。

首先判掉 A+B=C 的情况,直接构造两个尽可能大的 A 位素数和 B 位素数即可。

否则令 w=A+B-C-1,找到两个尽可能小的 A-w 位素数和 B-w 位素数,再乘上 10^w 即可。

注意找到的数如果相同的话还需要继续往后面找。

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef __int128 lll;
int T, a, b, c;
const int base[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
int qpow(int a, int b, const int p) {
    int c = 1;
    while (b) { if (b & 1) c = (lll)c * a % p; a = (lll)a * a % p, b >>= 1; }
    return c;
}
bool millerRabin(const int n) {
    if (n < 3 || !(n & 1)) return n == 2;
    int u = n - 1, t = 0, v;
    while (!(u & 1)) u >>= 1, t++;
    for (int a : base) {
        v = qpow(a, u, n);
        if (v == 1 || v == n - 1 || !v) continue;
        for (int i = 1; i <= t; i++) {
            v = (lll)v * v % n;
            if (v == n - 1 && i != t) { v = 1; break; }
            if (v == 1) return 0;
        }
        if (v ^ 1) return 0;
    }
    return 1;
}
int pw10(int x) { int y = 1; for (int i = 1; i <= x; i++) y *= 10; return y; }
int findp(int x) { while (!millerRabin(x)) x++; return x; }
int findpp(int x) { while (x && !millerRabin(x)) x--; return x; }
int cnt10(int x) { int y = 0; while (x) x /= 10, y++; return y; }
signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> T;
    while (T--) {
        cin >> a >> b >> c;
        if (c == a + b) {
            int px = findpp(pw10(a) - 1), py = findpp(pw10(b) - 1);
            if (!px || !py) { cout << "No\n"; continue; }
            if (px == py) py = findpp(py - 1);
            if (!py) { cout << "No\n"; continue; }
            if (cnt10(px) != a || cnt10(py) != b || cnt10(px * py) != c) cout << "No\n";
            else cout << "Yes\n" << px << ' ' << py << '\n';
            continue;
        }
        if (max(a, b) > c || a + b - 1 < c) { cout << "No\n"; continue; }
        int w = a + b - c - 1, d = a - w, e = b - w;
        int px = findp(pw10(d - 1)), py = findp(pw10(e - 1));
        if (px == py) py = findp(py + 1);
        int pw = pw10(w), x = px * pw, y = py * pw;
        cout << "Yes\n" << x << ' ' << y << '\n';
    }
}