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

【代码】:

  1. #include <iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<map>
  6. #include<set>
  7. #include<string>
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. int t,p,flag;
  13. scanf("%d",&t);
  14. while(t--)
  15. {
  16. flag=;
  17. scanf("%d",&p);
  18. for(int i=;i<=1e6+;i++)
  19. {
  20. if(*i*i+*i+==p)
  21. {
  22. flag=;
  23. break;
  24. }
  25. }
  26. if(flag) printf("YES\n");
  27. else printf("NO\n");
  28. }
  29. return ;
  30. }
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <string.h>
  7. #include <string>
  8. #define ll long long
  9.  
  10. using namespace std;
  11.  
  12. const int MAXN = 1e6 + ;
  13. ll tab[MAXN];
  14.  
  15. void init()
  16. {
  17. for (ll i = ;i < MAXN;++i)
  18. tab[i] = * i*i + * i + ;
  19. }
  20.  
  21. int main()
  22. {
  23. int T;
  24. scanf("%d", &T);
  25. init();
  26. for (int i = ;i <= T;++i)
  27. {
  28. bool flag = false;
  29. ll v;
  30. scanf("%I64d", &v);
  31. int left = , right = MAXN-;
  32. int mid = (left + right) >> ;
  33. while (left <= right)
  34. {
  35. if (v == tab[mid])
  36. {
  37. flag = true;
  38. break;
  39. }
  40. else if (v > tab[mid])
  41. left = mid + ;
  42. else
  43. right = mid - ;
  44. mid = (left + right) >> ;
  45. }
  46. if (flag)
  47. printf("YES\n");
  48. else
  49. printf("NO\n");
  50. }
  51. //system("pause");
  52. return ;
  53. }

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

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. Sublime Text 2.0.2 注册码激活

    直接输入注册码就可以了 ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 ...

  2. 【题解】【CF Round #278】Tourists

    圆方树第二题…… 图中询问的是指定两点之间简单路径上点的最小权值.若我们建出圆方树,圆点的权值为自身权值,方点的权值为所连接的圆点的权值最小值(即点双连通分量中的最小权值).我们可以发现其实就是这两点 ...

  3. http缓存知多少

    很久没有写博客了,趁现在也快过年,最近项目不是很忙,写一篇博客做为2018年的开始,重拾刚毕业的几年前写博客的冲动.http协议是每个程序猿应该需要知道的东西,不管是前端人员还是后端人员,以前在上家公 ...

  4. 【BZOJ 1592】[Usaco2008 Feb]Making the Grade 路面修整 dp优化之转移变状态

    我们感性可证离散(不离散没法做),于是我们就有了状态转移的思路(我们只考虑单不减另一个同理),f[i][j]到了第i块高度为j的最小话费,于是我们就可以发现f[i][j]=Min(f[i-1][k]) ...

  5. (一)STM32固件库详解(转载)

    本篇博文是转载自emouse,因为不能直接转载,所以是复制过来再发布的. emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/   1.1 基于标准外设库的 ...

  6. 自建git服务器搭建使用记录

    git在push的时候出现insufficient permission for adding an object错误 //解决方法,在git库的目录下 //明明一开始创建user的时候有执行这个命令 ...

  7. 51Nod 1212无向图最小生成树

    prim #include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f ][]; ],lowc[]; ],int n) ...

  8. linux编译动态库 fPIC作用

    在生成动态库时,常常习惯性的加上fPIC选项,fPIC有什么作用和意义,加不加有什么区别,这里做下小结: fPIC的全称是 Position Independent Code, 用于生成位置无关代码. ...

  9. 【洛谷 P3834】 可持久化线段树1(主席树)

    题目链接 主席树=可持久化权值线段树. 如果你不会可持久化线段树,请右转 如果你不会权值线段树,请自行脑补,就是线段树维护值域里有多少个数出现. 可持久化线段树是支持查询历史版本的. 我们对每个数都进 ...

  10. 【洛谷 SP283】NAPTIME - Naptime(DP)

    题目链接 先考虑如果只有一天,那么该怎么做. 设\(f[i][j][1]\)表示前\(i\)个小时睡了\(j\)个小时并且第\(j\)个小时正在睡觉时的最大体力,\(f[i][j][1]\)表示前\( ...