Here is an AC submission:
AC
#include <bits/stdc++.h>
#define int unsigned long long
#define pb push_back
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int>>
using namespace std;
int mysqrt(int x) {
int l = 0, r = x, ans = 0;
while (l <= r) {
int mid = l + (r-l)/2;
if ((__uint128_t)mid * mid <= x) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
void solve(){
int k;
cin >> k;
unsigned long long l=0,r=ULLONG_MAX;
while(l<=r){
int mid = l+(r-l)/2;
int take = mysqrt(mid);
int num = mid-take;
if(num < k){
l = mid+1;
} else{
r = mid - 1;
}
}
cout << r+1 << "\n";
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
Here is an WA submission:
WA
#include <bits/stdc++.h>
#define int unsigned long long
#define pb push_back
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int>>
using namespace std;
int mysqrt(int x) {
int l = 0, r = x, ans = 0;
while (l <= r) {
int mid = l + (r-l)/2;
if ((__uint128_t)mid * mid <= x) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
void solve(){
int k;
cin >> k;
unsigned long long l=0,r=ULLONG_MAX;
while(l<=r){
int mid = l+(r-l)/2;
int take = mysqrt(mid);
int num = mid-take;
if(num < k){
l = mid+1;
} else{
r = mid - 1;
}
}
cout << r+1 << "\n";
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
The only difference is a custom sqrt function. This was my worse contest ever because of this. Wasted like 40 mins trying figure this out and needed like 1 more min to solve C. Someone please explain because this is so weird and frustrating.
Man both your codes are same :)
I believe B was too tight on constraints https://codeforces.net/contest/2020/submission/283621067 Here's my solution you can check. Make hi = 2*k and it would work.
Still doesn't work: https://codeforces.net/contest/2020/submission/283671671
I guess you pasted same codes
oh shoot lol youre right, either way i got a good answer
You pasted same code.
I found your submissions.
seems like you didn't consider floating precision error.
here's mine with STL 283569591
Ok thanks. I will do some research into floating point imprecision and how to avoid it.