2017年ACM青岛站网络赛--A Cubic number and A Cubic Number

来源:互联网 发布:淘宝上怎么评价不了了 编辑:程序博客网 时间:2024/05/19 14:38

A Cubic number and A Cubic Number

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


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

思路://计算两个立方数的差,由公式a^3-b^3=(a-b)*(a^2+a*b+b^2);直接计算三次方会超过long long的范围;由于输入的是素数,故a-b只能是1;也就是说我们只需要记录相邻两个数的立方差,不需要考虑其它的两个数隔开的立方差情况,因此我们开一个数组记录相邻两个数的立方差即可,用公式计算,不会炸掉; 然后对每个输入的数检查是否存在数组中即可,经过模拟,差不多在一百万左右其差值在13位数据,题目中p的范围也就12位数,故循环到一百万已经够用。AC代码如下:


#include<stdio.h>#include<math.h>long long p;long long a[1000010];int f(){int k=0;for(long long i=2;i<=1000000;i++){long long t=(i-1)*(i-1)+i*(i-1)+i*i;a[k++]=t;}return k;}int main(){int t;scanf("%d",&t);int k=f();while(t--){scanf("%lld",&p);bool flag=false;for(int i=0;i<k;i++){if(a[i]==p){flag=true;break;}}if(flag) printf("YES\n");else printf("NO\n");}return 0;}

原创粉丝点击