Problem Description
A cubic number is the result of using a whole number in a multiplication three times. For example, so is a cubic number. The first few cubic numbers are and . Given an prime number . Check that if is a difference of two cubic numbers.
Input
The first of input contains an integer which is the total number of test cases.
For each test case, a line contains a prime number .
Output
For each test case, output 'YES' if given 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
输入输出测试
题意:给出一个质数,判断该质数是否为两立方数之差
分析:
一开始想暴力打表的,发现超内存了。
p=x^3-y^3=(x-y)*(x^2+x*y+y^2)为一个质数,则x-y=1
知道这一步就ok了,打表或者推公式
则p=3*y(y+1)+1
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int N=100010;
int main(){
int t;
ll n;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if((n-1)%3==0)
{
n=(n-1)/3;
ll j = sqrt(n);
if(j*(j+1) == n)
printf("YES\n");
else
printf("NO\n");
}
else
printf("NO\n");
}
return 0;
}