Hdu 4282 A very hard mathematic problem

Source: Internet
Author: User

 
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;
}
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.