Skip to content

Instantly share code, notes, and snippets.

@avijitagarwal
Created July 14, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avijitagarwal/c54b4db891017a44831e605da50a9ddb to your computer and use it in GitHub Desktop.
Save avijitagarwal/c54b4db891017a44831e605da50a9ddb to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define int long long
pair<int, int> getWinner(int N, int M, int K) {
if (N <= 2 * K) return {N, M};
int start = M * 2 + K + 1, end = M + K << 1;
int x = max(0ll, (N - end) / K);
if (N - x * K > end) ++x;
int can = N - x * K, marks = min(M, end - can >> 1);
pair<int, int> winner = {can, marks};
for (int i = 1; start <= N; ++i) {
x = max(0ll, (N - end) / K);
if (N - x * K > end) ++x;
can = N - x * K; marks = min(M, end - can >> 1);
if (can >= start and can <= end) {
winner = {can, marks};
}
start += K * (1ll << i);
end = start + K - 1;
}
return winner;
}
main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int i, j, k;
int tc; cin >> tc;
while(tc--) {
int N, M, K; cin >> N >> M >> K;
pair<int, int> winner = getWinner(N, M, K);
cout << winner.first << " " << winner.second << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment