ICPC 青岛赛区 网络赛 1011 :A Cubic number and A Cubic Number<数学>

来源:互联网 发布:救世主大教堂知乎 编辑:程序博客网 时间:2024/06/05 22:42

A Cubic number and A Cubic Number

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



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)=(a-b)*(a^2-ab+b^2);

然后,题目说的是给你一个素数,素数的基本性质就是,他只有两个因子,因此我们可以得出 a-b=1 ,a^2-ab-b^2=p;

那么我们只需要证明后一个方程式成立就好了,并且得到的解要是个整数。

后一个方程化简后得到:3*b^2+3b+1=p;

运用中学学的解一元二次方程的公式,得到 ((-3+_sqrt(12*p-3))/6)

因此,只需要判断 sqrt开出来的值是不是整数,并且减去3之后能否被6整除就好了!


#include <cstdio>#include <algorithm>#include <iostream>#include <list>#include <queue>#include <cstring>#include <set>#include <map>#include<cmath>using namespace std;int main(){    int t;    scanf("%d",&t);    while(t--){        long long p;        scanf("%lld",&p);        long long pp=(long long )sqrt(12*p-3);        if(pp*pp!=12*p-3)        {            //cout<<pp<<endl;            printf("NO\n");        }        else        {            if((pp-3)%6!=0)                printf("NO\n");            else                printf("YES\n");        }    }return 0;}




阅读全文
0 0
原创粉丝点击