abc397D题解

· · 题解

第一次用KaTeX,写的不豪请谅解

题意🤓🤓🤓

给定一个正整数N,问有没有一对正整数(x,y),使x^3+y^3=N(1<=N<=1e18)

过程😶😶😶

我们可以先化简x^3-y^3:

x^3-y^3 =(x-y)(x^2+y^2+xy) \because (x-y)^2=x^2+y^2-2xy \therefore (x-y)(x^2+y^2+xy) =(x-y)((x-y)^2+3xy)

设A=x-y,B=xy

\therefore (x-y)((x-y)^2+3xy) =A(A^2+3B) =3AB+A^3=N \because 3AB+A^3=N \therefore B= \frac{N-A^3}{3A} x-y=A x+y = \sqrt{(x-y)^2+4xy} =\sqrt{A^2+4B} \therefore 2x =x-y+x+y = \sqrt{A^2+4B}+A x= \frac {\sqrt{A^2+4B}+A}{2} y= \frac{\sqrt{A^2+4B}-A}{2}

因为B可以用A表示出来,所以我们只枚举A就行了🙄💅💅💅

蛋事!!!🤯🤯🤯

A要枚举到哪呢?🤔🤔🤔

如果直接枚举到N那么1e18会让你直接TLE😧😧😧

现在,我们来证明:x-y \leqslant xy:

x-y \leqslant xy xy+y-x \geqslant0 $\therefore x-y \leqslant xy$成立 $\because 3AB+A^3=N \therefore 3A^2+A^3 \leqslant N

代码附上😋😋😋

#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N = 1e6 + 5;
//因为是三次方所以只枚举到1e6 
int n, x, y; 
signed main(){
    cin >> n;
    if(n == 1){
        cout << -1;
        return 0;
    }
    for(int i = 1; i * i * i + 3 * i * i <= n; i++){
        int k = sqrt(i * i + 4 * ((n - i * i * i) / (3 * i)));//表示x+y 
        x = (k + i) / 2;
        y = (k - i) / 2;
        if(x * x * x - y * y * y == n){
            cout << x << " " << y;
            return 0;
        }
    }
    cout << -1;
    return 0;
}