题意:

给定一个素数p(p <= 1e12),问是否存在一对立方差等于p。

分析:

根据平方差公式:

因为p是一个素数, 所以只能拆分成 1*p, 所以 a-b = 1.

然后代入a = b + 1. 求出 3a² + 3a + 1 = p

化简得a(a+1) = (p-1)/3

令(p-1)/3 = T, 问题化为是否存在整数a使得a(a+1) == T, 那么令 t = (int)sqrt(T),只要判定一下t * (t+1) == T ? 即可

另一种做法是打一个a的表(a只要打到1e6), 然后二分查找是否, 主要锻炼一下二分查找的代码实现。

(这题在网络赛时候我是打表找出规律的, 发现符合的数一定是形如1 + 6*(1+2+3...+k)这样的, 以后碰到这种题可以先打表看看有无规律)

代码:

数学方法:

#include <bits/stdc++.h>
using namespace std;
int main(){
int T;
scanf("%d", &T);
while(T--){
double p;
scanf("%lf", &p);
double T = (p-1.0)/;
double a = floor(sqrt(T));
if(a*(a+) == T)
printf("YES\n");
else printf("NO\n");
}
return ;
}

二分查找:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#define ll long long using namespace std; const int MAXN = 1e6 + ;
ll tab[MAXN]; void init()
{
for (int i = ;i < MAXN;++i)
tab[i] = 3LL * i*i + * i + ;
} int main()
{
init();
int T;
scanf("%d", &T);
while(T--){
long long p;
scanf("%lld", &p);
int left = , right = MAXN - ;
int mid = (left+right) >> ;
int flag = ;
while(left <= right){
if(p == tab[mid]){
flag = ;
break;
}
else if(p > tab[mid]){
left = mid + ;
}
else right = mid - ;
mid = (left+right) >> ;
}
if(flag)
printf("YES\n");
else printf("NO\n");
}
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. HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】

    Problem Description A cubic number is the result of using a whole number in a multiplication three t ...

  3. 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 ...

  4. 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 ...

  5. hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  6. HDU 4614 线段树+二分查找

    Vases and Flowers 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4614 Problem Description Alice is s ...

  7. Q - Queue HDU - 5493(树状树组维护区间前缀和 + 二分找预留空位)

    Q - Queue HDU - 5493 Problem Description NNN people numbered from 1 to NNN are waiting in a bank for ...

  8. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

  9. [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& ...

随机推荐

  1. PostgreSQL - 转义字符

    转载至:postgresql字符转义 前言 在PostgreSQL 9之前的版本中,可以直接使用反斜杠\进行转义:比如:\b表示退格, \n表示换行, \t表示水平制表符,\r标示回车,\f表示换页. ...

  2. python中的栈

    # @File: stack # 列表实现栈 class MyStack(object): def __init__(self): self.data = [] def push(self, item ...

  3. Salazar Slytherin's Locket CodeForces - 855E

    Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...

  4. Party Games UVA - 1610

    题目 #include<iostream> #include<string> #include<algorithm> using namespace std; // ...

  5. oracle如何设置最大连接数

    查看session: select * from v$session where username is not null select username,count(username) from v ...

  6. django 相关问题

    和数据库的连接 session的实现 django app开发步骤 python环境准备 数据库安装 model定义 url mapping定义 view定义 template定义 如何查看数据库里的 ...

  7. J2sdk中的主要的包介绍

  8. [转]2010 Ruby on Rails 書單 與 練習作業

    原帖:http://wp.xdite.net/?p=1754 ========= 學習 Ruby on Rails 最快的途徑無非是直接使用 Rails 撰寫產品.而這個過程中若有 mentor 指導 ...

  9. [转]List of Visual Studio Project Type GUIDs

    本文转自:http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs There isn' ...

  10. AJPFX总结内部类

    内部类:内部类的访问规则:1. 内部类可以直接访问外部类中的成员,包括私有   原因是内部类中持有了一个外部类的引用,格式:外部类.this2. 外部类要访问内部类,必须建立内部类对象访问格式:1.  ...