码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-6216 A Cubic number and A Cubic Number[二分]

时间:2017-10-02 18:24:53      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:bool   script   out   multi   hdu   typedef   mes   rip   pac   

A Cubic number and A Cubic Number

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

题意:给一个素数,问这个素数是否是两个立方数的差。

思路:

对于方程$a^3-b^3=p$,p是个素数,因此把方程进行变形成$a^3 - b^3 = (a-b)*(a^2+ab+b^2)$。

这时候可以发现$b=a-1$,因此问题就变成了找到a,使得方程$a^2+a(a-1)+(a-1)^2 = p$成立。然后进行二分。

#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
bool judge(LL x, LL p) {
    return x*(x-1)+x*x+(x-1)*(x-1) >= p;
}
int main(int argc, char const *argv[])
{
    int T;
    scanf("%d", &T);
    while (T--) {
        LL p;
        scanf("%lld", &p);
        LL ub = 1e6 + 10, lb = 0;
        LL ans = 0;
        while (ub >= lb) {
            LL mid = (ub+lb)>>1;
            if (judge(mid, p)) {
                ans = mid;
                ub = mid - 1;
            }
            else lb = mid + 1;
        }
        if (ans*(ans-1)+ans*ans+(ans-1)*(ans-1) == p) puts("YES");
        else puts("NO");
    }
    return 0;
}

HDU-6216 A Cubic number and A Cubic Number[二分]

标签:bool   script   out   multi   hdu   typedef   mes   rip   pac   

原文地址:http://www.cnblogs.com/cniwoq/p/7620485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!