Question: given a positive integer N, find the smallest positive integer (x, y) of y that satisfies n = x ^ 3-y ^ 3 ).
Analysis: number theory, divide and conquer.
X ^ 3-y ^ 3 = (x-y) (x ^ 2 + xy + y ^ 2 );
Because both X and Y are positive integers and x> Y, x ^ 3-y ^ 3> (x-y) (3Y ^ 3 );
Because N is 1 ~ Both 10000 (x-y) and (x ^ 2 + xy + y ^ 2) are 1 ~ An integer within 10000;
So, 0 ≤ y ≤ 60, and then the second Integer Range (Y, Y + 10000) is X.
Note: How are all number theory (⊙ _ ⊙ ).
#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; }
Ultraviolet A 11428-cubes