A Cubic number and A Cubic Number
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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
题意:判断能不能找到a³-b³等于一个质数p。
思路:一开始想着打表的,但是提交代码的长度是有限制的,emmm...
有一个公式叫做立方差公式
·
因为p是一个质数,所以(a-b)肯定等于一。所以只要判断另一个因子是不是等于p就行了,就把原来的立方转化成平方可以正常算了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
long long num;
scanf("%I64d",&num);
int flag=0;
for(long long i=0;i<=1000000;i++)
{
long long sum=i*i+(i+1)*(i+1)+i*(i+1);
if(num==sum)
{
flag=1;
break;
}
}
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}