跳到正文

题解

abc397D题解

wmoia约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年3月17日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v1当前

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

题意🤓🤓🤓

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

过程😶😶😶

我们可以先化简x3−y3x^3-y^3:

x3−y3x^3-y^3

=(x−y)(x2+y2+xy)=(x-y)(x^2+y^2+xy)

∵(x−y)2=x2+y2−2xy\because (x-y)^2=x^2+y^2-2xy

∴(x−y)(x2+y2+xy)\therefore (x-y)(x^2+y^2+xy)

=(x−y)((x−y)2+3xy)=(x-y)((x-y)^2+3xy)

设A=x-y,B=xy

∴(x−y)((x−y)2+3xy)\therefore (x-y)((x-y)^2+3xy)

=A(A2+3B)=A(A^2+3B)

=3AB+A3=N=3AB+A^3=N

∵3AB+A3=N\because 3AB+A^3=N

∴B=N−A33A\therefore B= \frac{N-A^3}{3A}

x−y=Ax-y=A

x+yx+y

=(x−y)2+4xy= \sqrt{(x-y)^2+4xy}

=A2+4B=\sqrt{A^2+4B}

∴2x\therefore 2x

=x−y+x+y=x-y+x+y

=A2+4B+A= \sqrt{A^2+4B}+A

x=A2+4B+A2x= \frac {\sqrt{A^2+4B}+A}{2}

y=A2+4B−A2y= \frac{\sqrt{A^2+4B}-A}{2}

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

蛋事!!!🤯🤯🤯

A要枚举到哪呢?🤔🤔🤔

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

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

x−y⩽xyx-y \leqslant xy

xy+y−x⩾0xy+y-x \geqslant0

(x+1)(y−1)+1⩾0(x+1)(y-1)+1 \geqslant 0恒成立

∴x−y⩽xy\therefore x-y \leqslant xy成立

∵3AB+A3=N\because 3AB+A^3=N

∴3A2+A3⩽N\therefore 3A^2+A^3 \leqslant N

代码附上😋😋😋

C++
#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;
}
原文