题解:P17181 [ICPC 2017 Hong Kong R] Fermat's Optimization Problem

· · 题解

题意:给定 n,z,设 x,y 为正整数,且 1 \le x < y < z,求误差函数 F(x, y, z, n) = |x^n + y^n - z^n| 的最小值。

数据范围:2 < n < 10,1 < z < 10^5。

看到误差函数的定义,想到分 x^n + y^n \le z^n 和 x^n + y^n > z^n 这两种情况讨论。

数据范围 1 < z < 10^5,想到枚举 x,对于每一个 x 求出使得 x^n + y^n \le z^n 成立的最大的 y,这样使得不等式不成立的最小 y 值即为前面求出的 y 加一。分别计算误差值即可覆盖所有情况。

注意到 x^n 在 x 为正整数时单调递增,所以当 x 增加时,“使得 x^n + y^n \le z^n 成立的最大的 y”一定单调不增。所以可以在枚举 x 时使用双指针计算 y。注意题目规定 x \neq y。

时间复杂度:O(z),因为枚举 x 是 O(z),计算 y 也是 O(z),不考虑高精度耗时。

代码中的 m 为 z - 1,即为 x,y 的取值上限,并且用了两次循环分别计算 y 与 y_1。

赛时代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
struct BigInt { // 高精度板子
    static const int MAXN = 55;
    int a[MAXN];
    int len; 

    BigInt() {
        len = 0;
        memset(a, 0, sizeof(a));
    }

    BigInt& operator=(const BigInt& x) {
        len = x.len;
        for (int i = 0; i <= len; ++i)
            a[i] = x.a[i];
        return *this;
    }

    BigInt& operator=(string s){
        len = (int)s.size();
        for (int i = 0; i < len; ++i)
            a[len - i] = s[i] - '0';
        return *this;
    }

    BigInt& operator=(int x) {
        string s = to_string(x);
        return *this = s;
    }

    BigInt operator+(const BigInt& b) const {
        int n = max(len, b.len);
        BigInt c;
        c.len = n + 1;
        for (int i = 1; i <= n; ++i) {
            c.a[i] += a[i] + b.a[i];
            c.a[i + 1] += c.a[i] / 10;
            c.a[i] %= 10;
        }
        while (c.len > 1 && c.a[c.len] == 0)
            c.len--;
        return c;
    }

    BigInt operator-(const BigInt& b) const {
        int n = max(len, b.len);
        BigInt c;
        c.len = n;
        for (int i = 1; i <= n; ++i) {
            c.a[i] += a[i] - b.a[i];
            if (c.a[i] < 0) {
                c.a[i + 1]--;
                c.a[i] += 10;
            }
        }
        while (c.len > 1 && c.a[c.len] == 0)
            c.len--;
        return c;
    }

    BigInt operator*(const BigInt& b) const {
        BigInt c;
        c.len = len + b.len;
        for (int i = 1; i <= len; ++i) {
            int carry = 0;
            for (int j = 1; j <= b.len; ++j) {
                int temp = c.a[i + j - 1] + a[i] * b.a[j] + carry;
                c.a[i + j - 1] = temp % 10;
                carry = temp / 10;
            }
            c.a[i + b.len] = carry;
        }
        while (c.len > 1 && c.a[c.len] == 0)
            c.len--;
        return c;
    }

    BigInt operator/(int b) const {
        BigInt c;
        int remainder = 0;
        for (int i = len; i >= 1; --i) {
            int cur = remainder * 10 + a[i];
            c.a[i] = cur / b;
            remainder = cur % b;
        }
        c.len = len;
        while (c.len > 1 && c.a[c.len] == 0)
            c.len--;
        return c;
    }

    int operator%(int mod) const {
        int ans = 0;
        int pow10 = 1;
        for (int i = 1; i <= len; ++i) {
            ans = (ans + a[i] * pow10) % mod;
            pow10 = pow10 * 10 % mod;
        }
        return ans;
    }

    BigInt pow_(int exp) const {
        if (exp == 0) {
            BigInt one;
            one = 1;
            return one;
        }
        if (exp & 1) {
            return pow_(exp - 1) * (*this);
        } else {
            BigInt half = pow_(exp / 2);
            return half * half;
        }
    }

    bool operator<(const BigInt& b) const {
        if (len != b.len) return len < b.len;
        for (int i = len; i >= 1; --i) {
            if (a[i] != b.a[i])
                return a[i] < b.a[i];
        }
        return false;
    }

    bool operator>(const BigInt& b) const {
        return b < *this;
    }

    bool operator!=(const BigInt& b) const {
        return b < *this || *this < b;
    }

    bool operator==(const BigInt& b) const {
        return !(*this != b);
    }

    bool operator<=(const BigInt& b) const {
        return !(b < *this);
    }

    bool operator>=(const BigInt& b) const {
        return !(*this < b);
    }

    void scan() {
        BigInt x;
        char ch = getchar();
        while (ch < '0' || ch > '9')
            ch = getchar();
        while (ch >= '0' && ch <= '9') {
            BigInt digit, ten;
            digit = (ch - '0');
            ten = 10;
            x = x * ten + digit;
            ch = getchar();
        }
        *this = x;
    }

    void print() const {
        if (len == 0) {
            cout << 0;
        } else {
            for (int i = len; i >= 1; --i)
                cout << a[i];
        }
    }

    string tostr() const {
        if (len == 0) return "0";
        string s;
        s.reserve(len);
        for (int i = len; i >= 1; --i)
            s.push_back(char(a[i] + '0'));
        return s;
    }
};
int n, z, m;
BigInt pm, pw[N];
void solve(){
    cin >> n >> z;
    m = z - 1;
    pm = m;
    pm = pm.pow_(n);
    for (int i = 1;i <= m;i++){
        pw[i] = i;
        pw[i] = pw[i].pow_(n);
    }
    BigInt mn;
    mn = "100000000000000000000000000000000000000000000000000";
    // 10^50,作为无穷大 
    int ansx = 0, ansy = 0;
    for (int x = 1, y = m;x <= m;x++){
        while (y > 1 && pw[x] + pw[y] > pm) y--;
        if (x != y && pw[x] + pw[y] <= pm && mn > pm - pw[x] - pw[y]){
            mn = pm - pw[x] - pw[y];
            ansx = x; ansy = y;
        }
    }
    for (int x = 1, y = m;x <= m;x++){
        while (y > 0 && pw[x] + pw[y] >= pm) y--;
        y++; // 把 y 更新为上文中的 y1 
        if (x != y && y <= m && pw[x] + pw[y] >= pm && mn > pw[x] + pw[y] - pm){
            mn = pw[x] + pw[y] - pm;
            ansx = x; ansy = y;
        }
        y--; // 恢复 
    }
    if (ansx > ansy) swap(ansx, ansy); // 题目说 x < y 
    cout << ansx << ' ' << ansy << ' ';
    mn.print(); cout << endl;
}
int main(){
    int T; cin >> T;
    while (T--) solve();
}