Source

http://acm.hdu.edu.cn/showproblem.php?pid=6216

2017 ACM/ICPC Asia Regional Qingdao Online

Description

A cubic number is the result of using a whole number in a multiplication three times. For example, p is a difference of two cubic numbers.

Input

The first of input contains an integer ).

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 }

 

相关文章:

  • 2021-05-19
  • 2022-12-23
  • 2021-09-18
  • 2021-08-16
  • 2022-12-23
  • 2021-10-10
  • 2021-12-24
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-08-30
  • 2022-02-14
相关资源
相似解决方案