HDU 6216 A Cubic number and A Cubic Number(思维)

来源:互联网 发布:python hist alpha 编辑:程序博客网 时间:2024/06/09 18:25

题目地址
题意:给你一个素数num,求有没有两个立方数的差是这个素数。
思路:只有两个相邻的立方数的差是素数,所以我们枚举出每一个差,记录在数组里面,然后通过二分查找,看看能不能查到这个数就好了。

#include <cstdio>  #include <cstring>  #include <algorithm>  #include <iostream>#include <set>#include <cmath>#define LL long longusing namespace std;LL cub[1825794];int main() {    cin.sync_with_stdio(false);    LL t, n;    for (LL i = 1; i <= 1825793; i++){        LL p = i + 1;        cub[i] = p*p*p - i*i*i;    }    cub[0] = 0;    cin >> t;    while (t--){        cin >> n;        bool flag = false;        LL px = lower_bound(cub, cub + 1825794, n) - cub;        if (cub[px] == n)flag = true;        if (flag)cout << "YES" << endl;        else cout << "NO" << endl;    }    return 0;}
原创粉丝点击