hdu 6216 A Cubic number and A Cubic Number

题意:给定一个素数,判定它是不是两个立方数之差。

题解:对于a^3+b^3=(a-b)(a^2-a*b+b^2),而一个素数的因子只有1和其本身,在加上(a^2-a*b+b^2)一定是大于1的,所以只有(a-b)为1的时候,才可能为解。预处理一下,打个表就好了。

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll ans[800000];
int mycheck(int l,int r,ll key)
{
    while(r-l>10)
    {
        int mid=(l+r)/2;
        if(ans[mid] > key)
        {
            r=mid-1;
        }
        else if(ans[mid] < key) l=mid+1;
        else return 1;
    }
   // cout<<l<<‘ ‘<<r<<endl;
    for(int i=l;i<=r;i++) if(ans[i]==key) return 1;

    return -1;
}
int main()
{
    int t;
    for(ll i=2;i<=800000;i++)
    {
        ans[i-1]=i*i*i-(i-1)*(i-1)*(i-1);
    //    if(i<=10) cout<<ans[i-1]<<endl;
    }

    //cout<<ans[800000-1]<<endl;
    cin>>t;
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        int pos=mycheck(1,800000,n);
        if(pos==1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}
时间: 2024-12-19 23:26:10

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

[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

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

HDU 6216 A Cubic number and A Cubic Number(数学/二分查找)

题意: 给定一个素数p(p <= 1e12),问是否存在一对立方差等于p. 分析: 根据平方差公式: 因为p是一个素数, 所以只能拆分成 1*p, 所以 a-b = 1. 然后代入a = b + 1. 求出 3a2 + 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)

HDU6216 A Cubic number and A Cubic Number 和 广工的加强版

题目传送门_杭电版 题目传送门_广工版 广工版的是杭电版的加强版. 题意:判断一个质数是否是两个整数的立方差 ---- 数学题 题解: 根据立方差公式:\(a^3 - b^3 = (a - b)(a^2 + ab + b^2)\) 且 p 为质数 所以 \((a - b)(a^2 + ab + b^2)\) 其中任意一括号为 1,另一括号则为 p. 经计算检验(这里就不写啦),得 \((a - b) = 1\),\((a^2 + ab + b^2) = p\) . 因为\((a - b) = 1

hdu 2665 Kth number (poj 2104 K-th Number) 划分树

划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu.com/link?url=vIUKtsKYx7byeS2KCOHUI14bt_0sdHAa9BA1VceHdGsTv5jVq36SfZgBKdaHYUGqIGvIGrE_aJtqy0D0b1fCoq 个人感觉看代码是最好的学习方法. #include <cstdio> #include <c