Finding Bovine Roots
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4374 | Accepted: 876 |
Description
The cows are trying to find their roots. They are not so smart as humans when they find square roots, the cows lose the integer portion of their square root calculation. Thus, instead of calculating the square root of 2 to be 1.4142135623730950488016887242096980785696, they deduce that it is 0.4142135623730950488016887242096980785696. The square root of 16 is calculated to be 0 (obviously an error).
The erroneous ciphering does, however, lead to an interesting question. Given a string of digits of length L (1 <= L <= 9), what is the smallest integer whose bovine square root decimal part begins with those digits?
By way of example, consider the string "123". The square root of 17 is approximately 4.1231056256176605498214098559740770251472 so the bovine square root is: 0.1231056256176605498214098559740770251472 whose decimal part (just after the period) starts with 123. It turns out that 17 is the smallest integer with this property.
The erroneous ciphering does, however, lead to an interesting question. Given a string of digits of length L (1 <= L <= 9), what is the smallest integer whose bovine square root decimal part begins with those digits?
By way of example, consider the string "123". The square root of 17 is approximately 4.1231056256176605498214098559740770251472 so the bovine square root is: 0.1231056256176605498214098559740770251472 whose decimal part (just after the period) starts with 123. It turns out that 17 is the smallest integer with this property.
Input
Line 1: A single integer, L
Line 2: L digits (with no spaces)
Line 2: L digits (with no spaces)
Output
Line 1: A single integer that is the smallest integer whose bovine square root starts with the supplied string
Sample Input
3 123 大体题意: 给你一个长度小于等于9的字符串,表示某个小数的后缀, 比如字符串是123,那么小数是0.123. 让你求出最小的整数x,满足sqrt(x) 去掉整数的后缀包含字符串,说的很乱,仔细读提吧,很明了! 思路: 既然X是字符串t 加上某个整数的平方(近似值),那么一定是小于x的, 拿个具体例子来说 比如说 样例 0.123, 那么必然存在一个整数k 使得(k+0.123)*(k+0.123) < x 和 (k + 0.124) * (k + 0.124) > x 所以只要枚举发现上述不等式成立即可!#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const double EPS[20] = {1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12}; int main(){ int n,a; scanf("%d%d",&n,&a); double t = a; t*=EPS[n]; int i; for (i = 1;; ++i){ if ((ll)((t+i)*(t+i)) + 1 < ((t+i+EPS[n])*(t+i+EPS[n])))break; } printf("%I64d\n",(ll)((t+i)*(t+i)) + 1); return 0; }