hdu 6216 A Cubic number and A Cubic Number

来源:互联网 发布:maven 打包java环境 编辑:程序博客网 时间:2024/05/30 23:00

A Cubic number and A Cubic Number

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


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,问是否有两个数的立方差等于p,如果有输出YES,没有输出NO;
题目解析:
由题目可以得到,由于a^3-b^3=(a-b)(a^2+b^2+ab) 所以(a-b)a-b只有等于1,a^3-b^3的差才会是素数,所以可以得到
(i+1)^3-i^3;
代码:
#include <cstdio>#include <iostream>using namespace std;int main(){    //freopen("in.txt","r",stdin);    int T;    cin>>T;    while(T--)    {       long long n;        cin>>n;        for(long long i=1;i<100000000;i++){           if((i+1)*(i+1)*(i+1)-i*i*i>n){                cout<<"NO"<<endl;                break;           }           if((i+1)*(i+1)*(i+1)-i*i*i==n){                cout<<"YES"<<endl;                break;           }        }    }    return 0;}


原创粉丝点击