1 idea: enumerate the z range (2-31), then enumerate the value of x 1-> pow (x, z)> = k/2, find the value of y in the last two parts.
2 analysis: 1 The formula can tell that the range of z is 2-31, but the range of x and y is not well determined, so the brute force is definitely TLE.
2. These similar questions are usually fixed by two and then searched for the third question in the second part.
3. Pay attention to the use of (left + right)/2 for Binary Search. Therefore, the data type must be long so that it will not exceed int (N times in this region, not explained ), in addition, the current value tmp may be greater than the long range in binary search. Therefore, when tmp is determined, it means that the mid is greater than y at this time.
4 because pow functions are relatively slow to use, you can write a Pow function for big data. Pay attention to the type of data returned.
3 code:
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstdio>
# Include <cstring>
# Include <cmath>
Using namespace std;
# Define maxn1010
Int k, ans;
Long Pow (long x, long y ){
Long tmp = x;
For (long I = 1; I <y; I ++)
X * = tmp;
Return x;
}
Void solve (){
Int x, y, z;
Long left, right, mid, tmp;
Ans = 0;
For (z = 2; z <32; z ++ ){
For (x = 1; x ++ ){
If (Pow (x, z)> = k/2)
Break;
Left = x + 1;
Right = k;
While (left <= right ){
Mid = (left + right)/2;
Tmp = Pow (x, z) + Pow (mid, z) + mid * x * z;
If (tmp = k ){
Ans ++;
Break;
}
Else if (tmp> k | tmp <0)/* pay attention to data overflow in this region */
Right = mid-1;
Else
Left = mid + 1;
}
}
} Www.2cto.com
Printf ("% d \ n", ans );
}
Int main (){
// Freopen ("input.txt", "r", stdin );
While (scanf ("% d", & k)
Solve ();
Return 0;
}