poj 3175

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.

Input

Line 1: A single integer, L 

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

題意:給定一個平方根小數部分的前幾位,求最小的原是數值。

思路:我在場上以爲是字典樹。。。結果被當成傻子。。。賽後看了別人的題解也是很不理解,後來照着他們的討論演算了一下,

枚舉整數部分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;

        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章