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");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章