跳到正文

题解

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

Wangruofan约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年8月10日
更新
2026年8月10日
篇幅
约 11 分钟
题目
P17181

版本

  1. v1当前

P17181 [ICPC 2017 Hong Kong R] Fermat’s Optimization Problem

题意

定义误差函数 F(x,y,z,n)=∣xn+yn−zn∣F(x,y,z,n)=\left|x^n+y^n-z^n\right|,给定 nn 和 zz ,找出两个正整数 xx 和 yy,满足 x<y<zx<y<z,且使得误差值 F(x,y,z,n)F(x,y,z,n) 最小化。

思路

费马大定理

对于整数 n>2n>2,不存在正整数 x,y,zx,y,z 使得 xn+yn=znx^n+y^n=z^n。

不过其实在本题中没有影响。

看到楼上几篇题解都是用二分实现的,观察到当 xx 增大时,最优的 yy 一定单调不增,可以使用双指针。时间复杂度 O(Tz)O(Tz)。

笔者不会 Python,就套了C++又臭又长的高精度板子。

代码

C++
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct BigInt {
    vector<int> num;
    bool neg;
    BigInt() { neg = false; }
    BigInt(long long x) {
        neg = (x < 0);
        if (x == 0) num.push_back(0);
        else {
            if (x < 0) x = -x;
            while (x > 0) {
                num.push_back(x % 10);
                x /= 10;
            }
        }
    }
    BigInt(const string &s) {
        neg = (s[0] == '-');
        for (int i = s.size() - 1; i >= neg; i--) {
            num.push_back(s[i] - '0');
        }
        trim();
    }
    void trim() {
        while (num.size() > 1 && num.back() == 0) num.pop_back();
        if (num.size() == 1 && num[0] == 0) neg = false;
    }
    bool absGreater(const BigInt &b) const {
        if (num.size() != b.num.size())
            return num.size() > b.num.size();
        for (int i = num.size() - 1; i >= 0; i--)
            if (num[i] != b.num[i])
                return num[i] > b.num[i];
        return false;
    }
    bool operator>(const BigInt &b) const {
        if (neg != b.neg) return !neg;
        if (!neg) return absGreater(b);
        else return b.absGreater(*this);
    }
    bool operator<(const BigInt &b) const { return b > *this; }
    bool operator==(const BigInt &b) const {
        return neg == b.neg && num == b.num;
    }
    bool operator!=(const BigInt &b) const { return !(*this == b); }
    static vector<int> absAdd(const vector<int> &a, const vector<int> &b) {
        vector<int> res;
        int carry = 0;
        for (int i = 0; i < a.size() || i < b.size() || carry; i++) {
            if (i < a.size()) carry += a[i];
            if (i < b.size()) carry += b[i];
            res.push_back(carry % 10);
            carry /= 10;
        }
        return res;
    }
    static vector<int> absSub(const vector<int> &a, const vector<int> &b) {
        vector<int> res;
        int borrow = 0;
        for (int i = 0; i < a.size(); i++) {
            int cur = a[i] - borrow;
            if (i < b.size()) cur -= b[i];
            if (cur < 0) {
                cur += 10;
                borrow = 1;
            } else borrow = 0;
            res.push_back(cur);
        }
        while (res.size() > 1 && res.back() == 0) res.pop_back();
        return res;
    }
    BigInt operator+(const BigInt &b) const {
        BigInt ans;
        if (neg == b.neg) {
            ans.num = absAdd(num, b.num);
            ans.neg = neg;
        } else {
            if (absGreater(b)) {
                ans.num = absSub(num, b.num);
                ans.neg = neg;
            } else {
                ans.num = absSub(b.num, num);
                ans.neg = b.neg;
            }
        }
        ans.trim();
        return ans;
    }
    BigInt operator-(const BigInt &b) const {
        BigInt tmp = b;
        tmp.neg = !tmp.neg;
        return *this + tmp;
    }
    static vector<int> absMul(const vector<int> &a, const vector<int> &b) {
        vector<int> res(a.size() + b.size(), 0);
        for (int i = 0; i < a.size(); i++) {
            int carry = 0;
            for (int j = 0; j < b.size() || carry; j++) {
                long long val = res[i + j] + 1LL * a[i] * (j < b.size() ? b[j] : 0) + carry;
                res[i + j] = val % 10;
                carry = val / 10;
            }
        }
        while (res.size() > 1 && res.back() == 0) res.pop_back();
        return res;
    }
    BigInt operator*(const BigInt &b) const {
        BigInt ans;
        ans.num = absMul(num, b.num);
        ans.neg = (neg != b.neg);
        ans.trim();
        return ans;
    }
};
ostream &operator<<(ostream &os, const BigInt &a) {
    if (a.neg) os << '-';
    for (auto it = a.num.rbegin(); it != a.num.rend(); ++it)
        os << *it;
    return os;
}
istream &operator>>(istream &is, BigInt &a) {
    string s;
    is >> s;
    a = BigInt(s);
    return is;
}
BigInt qpow(BigInt a,int b){
    BigInt ans=1;
    while(b){
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
int T,n;
BigInt zn,mn;
int x,y,X,Y,z;
BigInt p[100009];
BigInt check(int x,int y){
    return p[x]+p[y];
}
int main(){
    cin>>T;
    while(T--){
        cin>>n;
        cin>>z;
        for(int i=1;i<z;++i){
            p[i]=qpow(BigInt(i),n);
        }
        zn=qpow(BigInt(z),n);
        x=1,y=z-1;
        mn=zn;
        X=1,Y=2;
        for(;y>x;++x){
            while(y>x&&check(x,y)>zn) --y;
            BigInt s1=check(x,y);
            BigInt d1;
            if(s1>zn) d1=s1-zn;
            else d1=zn-s1;
            if(d1<mn){
                X=x,Y=y;
                mn=d1;
            }
            int ny=y+1;
            if(ny>x&&ny<z){
                BigInt s2=check(x,ny);
                BigInt d2;
                if(s2>zn) d2=s2-zn;
                else d2=zn-s2;
                if(d2<mn){
                    X=x,Y=ny;
                    mn=d2;
                }
            }
        }
        cout<<X<<' '<<Y<<' '<<mn<<'\n';
    }
    return 0;
}
原文