HDU 6216 (2017icpc青岛网络赛)

A Cubic number and A Cubic Number

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2478    Accepted Submission(s): 1015


 

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

 

 

Recommend

liuyiding

 

题目大意:

先输入一个 n  

然后判断之后输入的 n 个质数中能够满足 与两个不同数的立方之差相等

若满足条件 输出YES 否则输出 NO

 

思考:

队友先看的这题 但是她把数据范围看错了 以为是1000  直接暴力模拟 WA了两次

然后就交给我了 只告诉我 判断立方差 (我就直接忽略了质数这个关键的点

改了数据范围 发现果断超时 (毕竟最大有10000

比赛开始大概30分钟 两个队伍已经A了 但是我们还在TLE 

我认真地想了想 可能是线段树 (判断多个连续区间相加的和 是否相等于输入的值

就找了一个线段树模板 改了一下就交题了

还是超时

听到别的队说有规律 可是就是想不出来啊啊啊啊啊啊

比赛结束之后 马上补题 第一个A题的小姐姐告诉我 

就是数学题啊(也可以打表找规律

 

分析:

(看图片吧,打字不方便...)

 

所以 如果 n 满足 n = 3 * x * x + 3 * x + 1 或者  n = 3 * x * x - 3 * x + 1(x是整数)

以上两个等式作用相同 是 a 与 b 的相互转化

 

放代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
#include <stdlib.h>
#include <set>
#include <queue>
#include <malloc.h>
using namespace std;
int main()
{
    int t;
    __int64 n;
    scanf("%d",&t);
    while (t--)
    {
        bool  flag = false;
        scanf("%I64d",&n);
        if ((n-1)%3==0)
        {
            long long mm = (long long)sqrt((n-1)/3);
            if (mm*(mm+1) ==(n-1)/3)
                flag = true;
        }
        if ( flag == false)
            printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

代码其实很简单 只要能推导出数学公式就能打出来了...

(以后一定好好打表 

很久没写博客了 (暗示很久没好好做题了emmm

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值