博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-6216 A Cubic number and A Cubic Number [二分]
阅读量:7106 次
发布时间:2019-06-28

本文共 1464 字,大约阅读时间需要 4 分钟。

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;}

转载于:https://www.cnblogs.com/cniwoq/p/7620485.html

你可能感兴趣的文章
html查看器android
查看>>
从零打造B/S 自动化运维平台 (一、自动化运维平台的应用及业务流程)
查看>>
shell中使用FTP
查看>>
linux运维实用的42个常用命令总结
查看>>
MySQL分库分表python实现分库(7th)
查看>>
OSPF虚链路virtual-link
查看>>
使用WM_QUIT终止线程
查看>>
String字符串的常用方法
查看>>
文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr
查看>>
开启华为交换机路由器ssh访问
查看>>
linux 中的sar命令 与gnuplot绘图
查看>>
解决网站遭CC攻击(转载)
查看>>
Spring 项目全介绍
查看>>
Android应用程序组件Content Provider的启动过程源代码分析(3)
查看>>
部署及配置ISCSI Target,Livemigration系列之三
查看>>
rundeck Web页面配置node节点
查看>>
Java程序员,笔试必读
查看>>
linux 下 eclipse 开发环境的搭建
查看>>
android中DatePicker&TimePicker的应用
查看>>
JavaScript和C#通用gb2312和utf8编码解码函数简单实现
查看>>