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");
}
}