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. 算法学习——st表

    st表是一种基于倍增思想的DP. 用于求一个数列中的某个区间的最大/最小值. 用st[i][j]表示从第i个开始往后2^j个点,最大的是多少. 我们令k[i]表示2^i等于多少 那么有转移方程 st[ ...

  2. ext大法好啊

    http://www.cnblogs.com/keshuqi/p/6257895.html

  3. codeforces 799C Fountains

    C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. spring 中的@Import注解和@ImportResource注解

    概述:@Import注解是引入带有@Configuration的java类. @ImportResource是引入spring配置文件.xml 案例的核心代码如下: package com.timo. ...

  5. HBase并行写机制(mvcc)

    HBase在保证高性能的同时,为用户提供了便于理解的一致性数据模型MVCC (Multiversion Concurrency Control),即多版本并发控制技术,把数据库的行锁与行的多个版本结合 ...

  6. Java 处理 XML 的三种主流技术及介绍

    Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...

  7. IDEA 用maven创建web项目编译时不能发布resources中的文件

    1.在pom.xml加入 <build> <resources> <resource> <directory>${basedir}/src/main/j ...

  8. 单选按钮radio与文字如何对齐?

    布局如下: <input type="radio" value="立即发送" name="a_1">立即发送 <input ...

  9. [POI2014] KUR-Couriers(洛谷P3567)

    洛谷题目链接:[POI2014]KUR-Couriers 题目描述 Byteasar works for the BAJ company, which sells computer games. Th ...

  10. 【BZOJ2440】完全平方数 [莫比乌斯函数]

    完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 小X自幼就很喜欢数. 但奇怪的是 ...