HDu 6216 && 2017 ACM/ICPC Asia Regional Qingdao Online 1011

来源:互联网 发布:游戏开发编程语言 编辑:程序博客网 时间:2024/06/05 16: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): 89 Accepted Submission(s): 52

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

Source
2017 ACM/ICPC Asia Regional Qingdao Online

给你一个素数,判断它是不是能有两个立方数相减得到
立方差公式

a3b3=(ab)(a2+b2+ab)

由于给定的p是素数,所以a-b只能为1,也就是两个相邻的立方数,所以我们只要把范围内的立方数与前一个立方数相减后的结果打表就可以,然后再二分一下,(好像直接for循环也能过)看是否找到p

代码入下:

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;const int MAX  = (int)1e6+5;//这里要保证立方数相减后的最大数比1e12大ll num[MAX+10],p,a[MAX+10];bool solve(){    ll l = 0,r = MAX;    while(r - l > 1){        ll mid = (l+r)/2;        if(a[mid] >= p)            r = mid;        else            l = mid;    }    if(a[r] == p)//因为相等时更新的r        return true;    return false;}int main(void){    for(ll i=1;i<=MAX;i++){        num[i] = i*i*i;    }    for(int i=1;i<=MAX;i++){//求与前一个立方数相差的结果,存进a        a[i] = num[i] - num[i-1];    }    int t;    scanf("%d",&t);    while(t--){        scanf("%lld",&p);        if(solve()){            printf("YES\n");        }        else{            printf("NO\n");        }    }    return 0;}
阅读全文
0 0
原创粉丝点击