【数学】HDU 6216 - A Cubic number and A Cubic Number (2017ICPC青岛网络赛)
A Cubic number and A Cubic Number
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≤10^12).
Output
For each test case, output 'YES' if given p is a difference of two cubic numbers, or 'NO' if not.
Examples
Input
10
2
3
5
7
11
13
17
19
23
29
Output
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
Solution
题意:给出一个素数p(2 <= p <= 1e12),问你p是否是某两个立方数之差。
思路:设两个立方数分别为i*i*i和j*j*j(i = j + d, d > 0),令p = i^3 - j^3 = i^3 - (i-d)^3 = 3di^2 - 3d^2i + d^3 = d*(3i^2 - 3di + d^2)。因为p为素数,所以d一定为1,那么只需把相邻的立方数之差存下来,每次二分查找其中有没有p就能知道YES or NO。
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 int T; 6 ll p,d[600666]; 7 8 int main(){ 9 for(ll i = 2;i <= 600000;++i) 10 d[i-1] = (i*i*i-(i-1)*(i-1)*(i-1)); 11 12 ios::sync_with_stdio(false); 13 cin >> T; 14 while(T--){ 15 cin >> p; 16 if(binary_search(d+1,d+600000,p)) 17 cout << "YES" << endl; 18 else 19 cout << "NO" << endl; 20 } 21 22 return 0; 23 }