HDU-6216 A Cubic number and A Cubic Number 找规律+猜

HDU-6216 A Cubic number and A Cubic Number 找规律+猜

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≤1e12).

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

题意:

输入T个样例,每个样例是一个素数,判断是否为两个立方数的差。

思路:

做题时,先把从1到10的立方数列出来,再把两两之间的差列在上面,再把这些差两两相减的出来的差列在上面。大概是这样:

      12    18    24     30    36      42      48     54
    7    19    37    61     91     127    169     217    271
  1    8    27    64    125    216    343    512     729    1000

然后我发现第一行的差是6的倍数,然后发现这样连续的数的立方数的差是一个优美的数列。
我猜这样得出的每个连续的差值为素数,然后先猜输入的素数减一是否为三的倍数。
然后,因为有些三的倍数不在这个范围中,我们又需要特判下,在代码中看吧。

AC代码:

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

typedef long long ll;
int main() {
    int n;
    scanf("%d", &n);
    while(n--) {
        ll tmp;
        scanf("%lld", &tmp);
        if((tmp-1)%3==0)    {
            tmp--;
            tmp/=3;
            ll tt = sqrt(tmp);
            if(tmp == tt*(tt+1))
                puts("YES");
            else
                puts("NO");
        } else
            puts("NO");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值