hdu 3113 Sum of Cubes 数学 枚举 剪枝

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3113

题目来源:翻blog翻来的。

简要题意: x3+y3=n 的解中 x 最小的。

数据范围:0n106

题解

首先 x 可能是负的,需要枚举的范围差不多是绝对值1000

因为最极限的情况应该是 (x+1)3x3>106

然后就是直接枚举了,暴力获得答案。

实现

这题的剪枝很重要,直接暴力的话得跪,用浮点数,公式直接算还得跪。

之后试了下二分,过了,但是比较慢400+ms,加了个小剪枝后100+ms。

看网上的代码跑了下62ms,而且还是能继续优化的,给跪啊。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
int a[2005];
const int INF = 1e9;

int main()
{
    for (int i = 0; i <= 2000; i++) {
        a[i] = i-1000;
        a[i] = a[i]*a[i]*a[i];
    }
    int n;
    while (scanf("%d", &n) == 1 && n) {
        int x = INF, y, temp;
        for (int i = 0; i <= 2000; i++) {
            temp = n-a[i];
            if (temp < 0) break;
            if (binary_search(a+2000-i, a+2001, temp)) {
                x = i-1000;
                y = lower_bound(a, a+2001, temp)-a-1000;
                break;
            }
        }
        if (x == INF) {
            puts("Impossible");
        } else {
            printf("%d %d\n", x, y);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值