HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】
Problem Description
For each test case, a line contains a prime number p (2≤p≤10
.
is a difference of two cubic numbers, or 'NO' if not.
2
3
5
7
11
13
17
19
23
29
NO
NO
YES
NO
NO
NO
YES
NO
NO
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【数学思维+枚举/二分】的更多相关文章
- hdu 6216 A Cubic number and A Cubic Number【数学题】
hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...
- 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 ...
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...
- 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 ...
- [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& ...
- 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 ...
- 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 ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
随机推荐
- Sublime Text 2.0.2 注册码激活
直接输入注册码就可以了 ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 ...
- 【题解】【CF Round #278】Tourists
圆方树第二题…… 图中询问的是指定两点之间简单路径上点的最小权值.若我们建出圆方树,圆点的权值为自身权值,方点的权值为所连接的圆点的权值最小值(即点双连通分量中的最小权值).我们可以发现其实就是这两点 ...
- http缓存知多少
很久没有写博客了,趁现在也快过年,最近项目不是很忙,写一篇博客做为2018年的开始,重拾刚毕业的几年前写博客的冲动.http协议是每个程序猿应该需要知道的东西,不管是前端人员还是后端人员,以前在上家公 ...
- 【BZOJ 1592】[Usaco2008 Feb]Making the Grade 路面修整 dp优化之转移变状态
我们感性可证离散(不离散没法做),于是我们就有了状态转移的思路(我们只考虑单不减另一个同理),f[i][j]到了第i块高度为j的最小话费,于是我们就可以发现f[i][j]=Min(f[i-1][k]) ...
- (一)STM32固件库详解(转载)
本篇博文是转载自emouse,因为不能直接转载,所以是复制过来再发布的. emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 1.1 基于标准外设库的 ...
- 自建git服务器搭建使用记录
git在push的时候出现insufficient permission for adding an object错误 //解决方法,在git库的目录下 //明明一开始创建user的时候有执行这个命令 ...
- 51Nod 1212无向图最小生成树
prim #include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f ][]; ],lowc[]; ],int n) ...
- linux编译动态库 fPIC作用
在生成动态库时,常常习惯性的加上fPIC选项,fPIC有什么作用和意义,加不加有什么区别,这里做下小结: fPIC的全称是 Position Independent Code, 用于生成位置无关代码. ...
- 【洛谷 P3834】 可持久化线段树1(主席树)
题目链接 主席树=可持久化权值线段树. 如果你不会可持久化线段树,请右转 如果你不会权值线段树,请自行脑补,就是线段树维护值域里有多少个数出现. 可持久化线段树是支持查询历史版本的. 我们对每个数都进 ...
- 【洛谷 SP283】NAPTIME - Naptime(DP)
题目链接 先考虑如果只有一天,那么该怎么做. 设\(f[i][j][1]\)表示前\(i\)个小时睡了\(j\)个小时并且第\(j\)个小时正在睡觉时的最大体力,\(f[i][j][1]\)表示前\( ...