Finding Bovine Roots
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4408 | Accepted: 882 |
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
Sample Output
17
Hint
Be careful of floating point arithmetic. It engenders rounding errors that can be surprising.
Explanation of the sample:
sqrt(17) ~= 4.1231056256176605498214098559740770251472
Explanation of the sample:
sqrt(17) ~= 4.1231056256176605498214098559740770251472
题意:给定一个平方根小数部分的前几位,求最小的原是数值。
思路:我在场上以为是字典树。。。结果被当成傻子。。。赛后看了别人的题解也是很不理解,后来照着他们的讨论演算了一下,
枚举整数部分i,然后把平方数算出来,因为最终的结果肯定是整数,而且给的位数肯定是不够的,也就是说i+小数部分的平方肯定是比正解小的,取整再加上那个一就是正解了,但是只有这样还不行,要验证这个数是否正确,还需要一个浮点数来比较,就是i+小数部分,再加上10的-9次方,这个值的平方肯定是比原数打的。这个为什么是正确的呢,因为如果不满足条件的话,i+小数部分,再加上10的-9次方算出来的比原来的数字大一点点,而i+小数部分的平方再加上一就比原来的数值大很多,所以上述结果是正确的。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
double ep[] = {1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,13-7,1e-8,1e-9,1e-10};
int main()
{
int len,num;
scanf("%d%d",&len,&num);
double p = num*ep[len];
for(int i = 1;;i++)
{
double now =(p+i+ep[len])*(p+i+ep[len]);
double s = (long long)((p+i)*(p+i))+1;
if(now > s)
{
printf("%.0f\n",s);
return 0;
}
}
}