Блог пользователя NeverMa573r

Автор NeverMa573r, история, 15 месяцев назад, По-английски

What is the difference between the prewritten sqrt function that I used here : https://codeforces.me/contest/1737/submission/222667221 and my own written sqrt function that I wrote here : https://codeforces.me/contest/1737/submission/222667916?

I understand stuff about precision and things like that, but I'm just rounding down, so even if something is like 2.0000067 it should still yield the correct answer. Please explain! I feel like this just happens over and over again: everytime I use sqrt(x) it always fails to get AC and I have to write my own sqrt function, even if my algo is correct.

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
15 месяцев назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится
    public long sqrt(long x) {
    	long start = 0, end = (long)3e9, ans = 1;
        while(start<=end){
            long mid = (start+end)/2;
            if(mid*mid<=x){
                ans = mid;
                start = mid+1;
            }
            else
                end = mid-1;
        }
        return ans;
    }

Inbuilt square root functions loose precision for bigger numbers like 1e18 in cpp you can use sqrtl (that gives correct ans always) or this function