标签:blog io os for sp on 2014 log bs
题目:给定一个正整数N求出满足N = x^3 - y^3的y最小的正整数对(x,y)。
分析:数论,分治。
x^3 - y^3 = (x-y)(x^2 + xy + y^2);
因为,x、y都是正整数,且x > y,则x^3 - y^3 > (x-y)(3y^3);
因为N是1~10000(x-y)与(x^2 + xy + y^2)都是1~10000内的整数;
所以,0 ≤ y ≤ 60,然后二分整数区间(y,y+10000)求x即可。
说明:怎么都是数论(⊙_⊙)。
#include <iostream> #include <cstdlib> #include <cmath> using namespace std; int bs(int n, int y) { long long l = y+1,r = y+10000LL,x,k; while (l <= r) { x = (l+r)/2; k = (x-y)*(x*x+x*y+y*y); if (k == n) return x; if (k < n) l = x+1LL; if (k > n) r = x-1LL; } return -1; } int main() { int n,x; while (cin >> n && n) { x = -1; for (int y = 0 ; y < 60 ; ++ y) { x = bs(n ,y); if (x != -1) { cout << x << " " << y << endl; break; } } if (x == -1) cout << "No solution" << endl; } return 0; }
标签:blog io os for sp on 2014 log bs
原文地址:http://blog.csdn.net/mobius_strip/article/details/40537207