4

Getting different answers for same testcase in diff environments for Dytechlab C...

 1 year ago
source link: http://codeforces.com/blog/entry/107722
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Getting different answers for same testcase in diff environments for Dytechlab Cup 2022 QUES B

By pottiVJ, history, 21 hour(s) ago,

I have attached 3 screenshots, one in local, another in CodeChef online compiler as well as submission on code forces. for Testcase 2 — case 4, gives correct output as expected in codechef online compiler as well as in local, but gives the wrong answer in codeforces submission. Can anyone please help.

question link : https://codeforces.com/contest/1737/problem/B submission : https://codeforces.com/contest/1737/submission/175028607

20 hours ago, # |

The same happens to me. The inbuilt sqrt give some precision error for long integers in different systems. You can either use sqrtl (made especially for long long) or find it simply using binary search.

The following code will have different results on codeforces vs my local machine.

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll bs_sqrt(ll x) {
    ll left = 0, right = 2000000123;
    while (right > left) {
        ll mid = (left + right) / 2;
        if (mid * mid > x)
            right = mid;
        else
            left = mid + 1;
    }
    return left - 1;
}

int main() {
    ll l, r;
    cin >> l >> r;
    ll sql_1 = sqrt(l), sqr_1 = sqrt(r);
    ll sql_2 = sqrtl(l), sqr_2 = sqrtl(r);
    ll sql_3 = bs_sqrt(l), sqr_3 = bs_sqrt(r);
    cout << sql_1 << " " << sqr_1 << '\n';
    cout << sql_2 << " " << sqr_2 << '\n';
    cout << sql_3 << " " << sqr_3 << '\n';
    return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK