题解:P13836 微观戏剧(Constructive ver.)

· · 题解

洛谷 P13836

解题思路:

首先,我们要先做出 P11275 这道题。根据前面题目的结论:z=0 时,令 x_0 = y_0 即可;当 z=1,没有任何的两个数最小公倍数为 1,此时无解。由于题目要求 x_0,y_0\le 10^{18},所以当 z 小于等于 10^{18},让 x_0=1,y_0 = z 即可;而如果 z> 10^{18},可以考虑 x_0+y_0=z,而这时还要判断奇数与偶数。通过画图发现,奇数情况时,x_0 = \left\lfloor\dfrac{z}{2}\right\rfloor,y_0 = \left\lfloor\dfrac{z}{2}\right\rfloor+1;偶数情况时,x_0 = \dfrac{z}{2}-1,y_0 = \dfrac{z}{2}+1。

AC 代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int q;
signed main() {
    cin >> q;

    while (q--) {
        int x;
        cin >> x;

        if (x == 0) {
            cout << "1 1" << endl;
        } else if (x == 1) {
            cout << "-1 -1" << endl;
        } else if (x < 1e18) {
            cout << 1 << " " << x << endl;
        } else {
            if (x % 2 == 1) {
                cout << x / 2 << " " << x / 2 + 1 << endl;
            } else {
                cout << x / 2 - 1 << " " << x / 2 + 1 << endl;
            }
        }
    }

    return 0;
}