题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3113
题目来源:翻blog翻来的。
简要题意: x3+y3=n 的解中 x 最小的。
数据范围:
0⩽n⩽106
题解
首先 x 可能是负的,需要枚举的范围差不多是绝对值
1000 。因为最极限的情况应该是 (x+1)3−x3>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;
}