#include <iostream>
#include <random>
#include <cstdint>
#include <limits>
int AboutX(std::int32_t X, std::int32_t A){
if (A > X)return 1;
if (A < X) return -1;
return 0;
}
std::uint32_t BinarySearch(std::int32_t X, std::int32_t Add, int Judge){
int F = 1;
if (Add < 0) F *= -1;
if (Add == 0) Add = 1*F;
if (Judge == F) return X + Add;
if (Judge == -F) return X - Add;
return X;
}
std::pair<std::int32_t,int> Process(int A){
std::int32_t X = 0;
std::int32_t Add = 0;
int i = 0;
int Judge = 0;
do{
if (i == 1){
if (Judge > 0){
Add = std::numeric_limits<std::int32_t>::max();
}
else{
Add = std::numeric_limits<std::int32_t>::min();
}
}
Add /= 2;
X = BinarySearch(X, Add, Judge);
Judge = AboutX(X, A);
i++;
} while (Judge != 0);
return std::make_pair(X,i);
}
int main(){
std::random_device rd;
std::mt19937 mt(rd());
//std::uniform_int_distribution<std::int32_t> uid(std::numeric_limits<std::int32_t>::min(),std::numeric_limits<std::int32_t>::max());
std::uniform_real_distribution<> urd;
std::int32_t A1 = (std::int32_t)(std::numeric_limits<std::int32_t>::max()*urd(mt));
auto X1 = Process(A1);
std::int32_t A2 = (std::int32_t)(std::numeric_limits<std::int32_t>::min()*urd(mt));
auto X2 = Process(A2);
std::cout << "X=>" << X1.first << ',' << "Answer=>" << A1 << ' ' << std::endl << "Search for "<<X1.second<<" Count!" << std::endl<< "this is " << ((A1 == X1.first) ? "Valid" : "Invalid") << std::endl;
std::cout << "X=>" << X2.first << ',' << "Answer=>" << A2 << ' ' << std::endl << "Search for "<<X2.second<<" Count!" << std::endl<< "this is " << ((A2 == X2.first) ? "Valid" : "Invalid") << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8Y3N0ZGludD4KI2luY2x1ZGUgPGxpbWl0cz4KCmludCBBYm91dFgoc3RkOjppbnQzMl90IFgsIHN0ZDo6aW50MzJfdCBBKXsKCWlmIChBID4gWClyZXR1cm4gMTsKC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