HDU—6216 A Cubic number and A Cubic Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jnxxhzz/article/details/84333864

A Cubic number and A Cubic Number
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2572 Accepted Submission(s): 1050

Problem Description
A cubic number is the result of using a whole number in a multiplication three times. For example, 3×3×3=27 so 27 is a cubic number. The first few cubic numbers are 1,8,27,64 and 125. Given an prime number p. Check that if p is a difference of two cubic numbers.

Input
The first of input contains an integer T (1≤T≤100) which is the total number of test cases.
For each test case, a line contains a prime number p (2≤p≤1012).

Output
For each test case, output ‘YES’ if given p is a difference of two cubic numbers, or ‘NO’ if not.

Sample Input
10
2
3
5
7
11
13
17
19
23
29

Sample Output
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO

Source
2017 ACM/ICPC Asia Regional Qingdao Online

Recommend
liuyiding | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443

【分析】
题意:给出一个素数p,问p是否是两个立方数的差。
那么设存在整数a,b使得 a 3 b 3 = p a^3 - b^3 = p
化简得 ( a b ) ( a 2 + a b + b 2 ) = p (a-b)*(a^2 + ab + b^2) = p
因为p是素数,所以p应该只有两个因子1和p本身,那么显然 a 2 + a b + b 2 = p a^2 + ab + b^2 = p , a b = 1 a-b = 1
所以可以证明a,b相差1,即 a = b + 1 a = b + 1 ,代入得到 3 b 2 + 3 b + 1 = p 3b^2 + 3b + 1 = p
然后就是解方程的事情了,只要能解出一个整数解就表示存在两个立方数之差等于p
【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main (){
	int pp;scanf("%d",&pp);
	while (pp--){
		long long n;scanf("%lld",&n);
		long long delta = 9 + 4 * 3 * (n - 1);
		long long sq = sqrt(delta);
		if (sq * sq == delta){
			long long x1 = -sq - 3;
			if (x1 % 6 == 0){
				puts("YES");
				goto out;
			}
			long long x2 = sq - 3;
			if (x2 % 6 == 0){
				puts("YES");
				goto out;
			}
		}
		puts("NO");
		out:;
	}
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/jnxxhzz/article/details/84333864