2017青岛赛区亚洲区域赛网络赛 1011题题解

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


思路:若能找到一个整数n满足 p=3n²+3n+1 则为YES(好不容易找到的规律)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
#define INF 0x7fffffff
#define MAX 200010
#define PI 3.1415926535897932
#define E 2.718281828459045
using namespace std;
int t;
LL p;
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&p);
        bool flag=false;
        p=p-1;
        if(p%3==0){
            p=p/3;
            LL n=(LL)sqrt(1.0*p);
            if(n*n+n==p) flag=true;
        }
        if(flag)printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


你可能感兴趣的:(2017青岛赛区亚洲区域赛网络赛 1011题题解)