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
 
【题意】:询问一个质数p是否可以写成两个立方数的差。
【分析】:

x^3-y^3
=(x^3-x^2*y)+x^2*y-(y^3-x*y^2)-x*y^2
=x^2(x-y)-y^2(y-x)+xy(x-y)

=(x-y)(x^2+xy+y^2)=p(p是质数)——> x-y=1以及x^2+xy+y^2=p

代入消元:p=3x^2+3x+1

【代码】:

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<string>
using namespace std; int main()
{
int t,p,flag;
scanf("%d",&t);
while(t--)
{
flag=;
scanf("%d",&p);
for(int i=;i<=1e6+;i++)
{
if(*i*i+*i+==p)
{
flag=;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <string>
#define ll long long using namespace std; const int MAXN = 1e6 + ;
ll tab[MAXN]; void init()
{
for (ll i = ;i < MAXN;++i)
tab[i] = * i*i + * i + ;
} int main()
{
int T;
scanf("%d", &T);
init();
for (int i = ;i <= T;++i)
{
bool flag = false;
ll v;
scanf("%I64d", &v);
int left = , right = MAXN-;
int mid = (left + right) >> ;
while (left <= right)
{
if (v == tab[mid])
{
flag = true;
break;
}
else if (v > tab[mid])
left = mid + ;
else
right = mid - ;
mid = (left + right) >> ;
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
//system("pause");
return ;
}

预处理+二分查找//参考

HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】的更多相关文章

  1. hdu 6216 A Cubic number and A Cubic Number【数学题】

    hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...

  2. 2017青岛网络赛1011 A Cubic number and A Cubic Number

    A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/3276 ...

  3. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  4. POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)

    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...

  5. [Javascript] Use Number() to convert to Number if possilbe

    Use map() and Number() to convert to number if possilbe or NaN. var str = ["1","1.23& ...

  6. Ugly Number,Ugly Number II,Super Ugly Number

    一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...

  7. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  8. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  9. HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

    HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...

随机推荐

  1. Python 装饰器和抽象类

    #装饰器:对类或者函数进行功能的扩展 ''' #第一步:基本函数 def la(): print('脚踏黄河两岸,手拿机密文件,前面机枪扫射,后面炮火连天') #调用函数 la() la() #第二步 ...

  2. [AT3857]Median Sum

    题目大意:给定$n$个数,第$i$个数为$a_i$,记这$n$个数的所有非空子集的和分别为$s_1,s_2,\dots,s_{2^n-1}$:求$s$的中位数. 题解:假设考虑的是所有子集,包括空子集 ...

  3. [NOI2010]能量采集 解题报告

    [NOI2010]能量采集 题目描述 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. ...

  4. 获取Parameter参数值,方便调试使用

    #region #warning 调试使用,获取sql参数化,拼接出完整的sql语句,复制sql明文到mssql中运行 string debugSql = queryHelper.CommandTex ...

  5. 【BZOJ 2744 朋友圈】

    Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1570  Solved: 532[Submit][Status][Discuss] Descripti ...

  6. NOIP2010 codevs1069 洛谷P1525 关押罪犯

    Problem Description S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极 不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用 ...

  7. git使用笔记(四)远程操作

    By francis_hao    Nov 19,2016 以一张图说明远程操作,图片来自参考[2] git clone 从远端主机克隆一个版本库,若省略directory则生成一个和远端同名的版本库 ...

  8. 运动目标前景检测之ViBe源代码分析

    一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差.背景减除(GMM.CodeBook. SOBS. SACON. VIBE. W4.多帧平均……).光流(稀 ...

  9. bzoj 3771 Triple FFT 生成函数+容斥

    Triple Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 847  Solved: 482[Submit][Status][Discuss] Desc ...

  10. LVS+Keepalived搭建MyCAT高可用負載均衡集群

    1.前面我们已经搭建好mysql主主,并且用mycat实现双写功能,主要配置文件: [root@mycat2 conf]# cat schema.xml <?xml version=" ...