2017青岛赛区亚洲区域赛网络赛 1011题题解

忘是亡心i 2022-06-08 06:14 361阅读 0赞

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≤1012).

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

思路:若能找到一个整数n满足 p=3n²+3n+1 则为YES(好不容易找到的规律)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <map>
  9. #include <string>
  10. #include <stack>
  11. #define LL long long
  12. #define INF 0x7fffffff
  13. #define MAX 200010
  14. #define PI 3.1415926535897932
  15. #define E 2.718281828459045
  16. using namespace std;
  17. int t;
  18. LL p;
  19. int main()
  20. {
  21. scanf("%d",&t);
  22. while(t--){
  23. scanf("%lld",&p);
  24. bool flag=false;
  25. p=p-1;
  26. if(p%3==0){
  27. p=p/3;
  28. LL n=(LL)sqrt(1.0*p);
  29. if(n*n+n==p) flag=true;
  30. }
  31. if(flag)printf("YES\n");
  32. else printf("NO\n");
  33. }
  34. return 0;
  35. }

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读