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\),所以两个整数a,b必然是相邻的,且通过计算,1e6的立方减去 999,999的立方已经大于1e12。

因此可以开个保存两个相邻整数的立方差,大小为1e6的数组保存立方差的值,然后用二分搜索该数组就可以。

但是!

广工版的题目 p 的范围是1e15,要使得立方差为1e15得要开到1e7的立方,这样就会爆long long。所以上述方法已经不够用了。

让我们回到 \((a - b) = 1\),\((a^2 + ab + b^2) = p\) 这条式子。

根据前一条式子可以把后面一条式子化成: \(3b^2 + 3b + 1 = p\)

把 b 当作未知量,把 p 当作常数,可把式子堪称一元二次方程了,可以弄出

\(\Delta = 12p - 3\)

然后根据求根公式可以写成:$ b = \frac{-3 \pm \sqrt{12p - 3}}{6}$

所以只需要验证两个条件:

1.\(12p - 3\) 开方后是一个整数

2.\(12p - 3\) 开方后 - 3 能否被 6 整除就可以了(因为b是正整数)

// 杭电原题:http://acm.hdu.edu.cn/showproblem.php?pid=6216
// 广工加强题:https://ac.nowcoder.com/acm/contest/3036/K
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long LL;
int T;
LL p, ans[9000006];

void init(){
    for(LL i = 2; i <= 9000000; i++){
        ans[i - 1] = i * i * i - (i - 1) * (i - 1) * (i - 1);
    }
}

bool check(LL p){
    double a = sqrt(12 * p - 3);
    long long b = sqrt(12 * p - 3);
//    printf("a - b:%.16f, b:%lld\n", a - b, b);
    if(a - b < 0.000001 &&((b - 3) % 6 == 0) return true;
    else return false;
}

int main()
{
    // 搜索做法
    init();
    scanf("%d", &T);
    while(T--){
        scanf("%lld", &p);
        bool ok = false;
        int left = 1, right = 1000000;
        while(left <= right){
            int mid = (left + right) / 2;
            if(ans[mid] == p) { ok = true; break; }
            else if(p < ans[mid]) right = mid - 1;
            else left = mid + 1;
        }
        if(ok) printf("YES\n");
        else printf("NO\n");
    }

    // 数学解法
    scanf("%d", &T);
    while(T--){
        scanf("%lld", &p);
        if(check(p)) printf("YES\n");
        else printf("NO\n");
    }
    printf("i:%d num:%lld\n", 9000000, ans[8999999]);
    return 0;
}

原文地址:https://www.cnblogs.com/Ayanowww/p/12003672.html

时间: 2024-11-07 01:37:08

HDU6216 A Cubic number and A Cubic Number 和 广工的加强版的相关文章

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

[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

题意:给定一个素数,判定它是不是两个立方数之差. 题解:对于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

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)

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,