A Cubic number and A Cubic Number(HDU-6216)

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 pp. Check that if pp 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≤10^12).

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

题意:t 组数据,每组给出一个素数 p,问这个数是不是两个数的立方的差

思路:

首先,对于任意的两个数 x、y,他们的立方数的差 p=x^3-y^3=(x-y)(x^2+x*y+y^2)

由于输入的 p 是素数,那么 (x-y)=1(x^2+x*y+y^2)=p

由于 (x-y)=1,因此只需要打表记录相邻两个数的立方差,然后对于每个输入检查是否在数组中即可

100 0000 的立方差差不多在 13 位左右,而 p 的范围最大到 1E12,因此打 100w 的表足够

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define INF 0x3f3f3f3f
#define N 1000010
#define LL long long
const int MOD=10;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
using namespace std;
LL a[N];
int main(){
    int cnt=0;
    for(LL i=1;i<=1000000;i++)
        a[cnt++]=i*i+i*(i+1)+(i+1)*(i+1);

    int t;
    scanf("%d",&t);
    while(t--){
        LL p;
        scanf("%lld",&p);

        bool flag=false;
        for(int i=0;i<cnt;i++){
            if(a[i]==p){
                flag=true;
                break;
            }
        }

        if(flag)
            printf("YES\n");
        else
            printf("NO\n");

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值