hud6216 A Cubic number and A Cubic Number O(1)时间复杂度

来源:互联网 发布:上海圣剑网络 吴杨观 编辑:程序博客网 时间:2024/05/16 09:30

A Cubic number and A Cubic Number

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 963    Accepted Submission(s): 468



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 (1T100) which is the total number of test cases.
For each test case, a line contains a prime number p (2p1012).
 

Output
For each test case, output 'YES' if given p is a difference of two cubic numbers, or 'NO' if not.
 

Sample Input
102357111317192329
 

Sample Output
NONONOYESNONONOYESNONO
 
p = a^3 - b^3;
 p = (a-b)(a^2 + ab + b^2)
又因为p为素数,我们可以跟定a-b = 1;    所以p = 3b(b+1) + 1;
如果(p-1)%3!=0,也就不存在b(b+1),直接输出no。
p = (p-1)/3;
b = (int)sqrt(p);
这是p应该等于b(b+1).
AC代码。
#includeusing namespace std;#define ll long longint main(){int tc;ll n;cin>>tc;while(tc--){cin>>n;n-=1;if(n%3){cout<<"NO"<

原创粉丝点击