Codeforces 1490C Sum of Cubes

C. Sum of Cubes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a positive integer xx. Check whether the number xx is representable as the sum of the cubes of two positive integers.

Formally, you need to check if there are two integers aa and bb (1a,b1≤a,b) such that a3+b3=xa3+b3=x.

For example, if x=35x=35, then the numbers a=2a=2 and b=3b=3 are suitable (23+33=8+27=3523+33=8+27=35). If x=4x=4, then no pair of numbers aa and bb is suitable.

Input

The first line contains one integer tt (1t1001≤t≤100) — the number of test cases. Then tt test cases follow.

Each test case contains one integer xx (1x10121≤x≤1012).

Please note, that the input for some test cases won't fit into 3232-bit integer type, so you should use at least 6464-bit integer type in your programming language.

Output

For each test case, output on a separate line:

  • "YES" if xx is representable as the sum of the cubes of two positive integers.
  • "NO" otherwise.

You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

Example
input
Copy
7
1
2
4
34
35
16
703657519796
output
Copy
NO
YES
NO
NO
YES
YES
YES
Note

The number 11 is not representable as the sum of two cubes.

The number 22 is represented as 13+1313+13.

The number 44 is not representable as the sum of two cubes.

The number 3434 is not representable as the sum of two cubes.

The number 3535 is represented as 23+3323+33.

The number 1616 is represented as 23+2323+23.

The number 703657519796703657519796 is represented as 57793+7993357793+79933.

 

确定好两个数的范围, 预处理一下, 用set 插入查找

//2021-03-14 19:56:14
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long ll;

int T;
set<ll> rec;


int main(){
    for(int i = 1; i <= 1e4; i++){
        rec.insert((ll)i*i*i);
    }
    scanf("%d", &T);
    while(T--){
        ll x;
        scanf("%lld", &x);
        bool ok = 0;
        for(auto i: rec){
            if(rec.find(x-i) != rec.end()) ok = 1;
        }
        if(ok) printf("YES\n");
        else printf("NO\n");

    }

    return 0;
}

 二分做法

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long ll;

int T;

void solve() {
    ll x;
    scanf("%lld", &x);
    for (ll i = 1; i <= 10000; i++){
        ll l = 1, r = 10000;
        while(l < r){
            ll mid = l+r >> 1;
            ll sum = i*i*i + mid*mid*mid;
            if(sum > x) r = mid;
            else if(sum < x) l = mid + 1;
            else {
                printf("YES\n");
                return;
            }
        }
    }
    printf("NO\n");
}

int main(){
    scanf("%d", &T);
    while(T--){
        solve();
    }

    return 0;
}

 

posted @ 2021-03-14 20:25  sinEagle  阅读(207)  评论(0编辑  收藏  举报