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

2017 ACM/ICPC Asia Regional Qingdao Online

【题意】:询问一个质数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=0;
        scanf("%d",&p);
        for(int i=1;i<=1e6+10;i++)
        {
            if(3*i*i+3*i+1==p)
            {
                flag=1;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

#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 + 5;
ll tab[MAXN];  

void init()
{
    for (ll i = 0;i < MAXN;++i)
        tab[i] = 3 * i*i + 3 * i + 1;
}  

int main()
{
    int T;
    scanf("%d", &T);
    init();
    for (int i = 1;i <= T;++i)
    {
        bool flag = false;
        ll v;
        scanf("%I64d", &v);
        int left = 0, right = MAXN-1;
        int mid = (left + right) >> 1;
        while (left <= right)
        {
            if (v == tab[mid])
            {
                flag = true;
                break;
            }
            else if (v > tab[mid])
                left = mid + 1;
            else
                right = mid - 1;
            mid = (left + right) >> 1;
        }
        if (flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    //system("pause");
    return 0;
} 

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

时间: 2024-08-09 02:17:51

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^3=(x-y)(x^2+xy+y^2),看(x-y),就能很快想到不相邻的立方数之差是不可能是素数的:),,然后把y=x+1代入,得:p=3x^2+3x+1,所以可得判断条件为:①p-1必须能被3整除:②(p-1)/3必须能表示为两个相邻整数之积. 1 #include<iostream> 2 #incl

2017 ACM/ICPC Asia Regional Qingdao Online 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/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description A cubic number is the result of using a whole number in a mul

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树+暴力枚举边)

题目大意:给你1000个点,每个点上有一个数目代表这个城市有多少人,让你把这N个点构成一颗生成树,你可以删除其中的任意一条边.让你求出一个比例A/B是的这个比例最大,A表示你删除那条边上两个城市的人口数之和,B表示的是去掉这条变这可生成树上其他的边的总长度. 解体思路:先求出来最小生成树,然后暴力枚举生成树的边,B=总数-这条边的长度.A = 将这条连断开之后左右集合中权值最大的两个数的和. 这样保证了B最小的情况下,去找最大的A,所以是可行的解.生成树的同时建边,然后dfs找最大值. PS:这

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

[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","3","4.0","five", undefined, .00]; var num = str.map( (s) => { return Number(s); }); console.log(num); //[1, 1.23, 3

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

&#39;Invalid update: invalid number of sections. The number of sections contained in the table view aft

问题:(删除tableview中的section时) Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (5) must be equal to th