Skip to content

Instantly share code, notes, and snippets.

@avijitagarwal

avijitagarwal/CLMARKS.java Secret

Created Jul 14, 2020
Embed
What would you like to do?
import java.util.Scanner;
class CLMARKS {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int t = in.nextInt();
while (t-- > 0) {
long n = in.nextInt();
long m = in.nextInt();
long k = in.nextInt();
long pos, ans;
if (n <= 2 * k) { // Observation 1
pos = n;
ans = m;
} else if (n <= 2 * (k + m)) { // Observation 2
pos = n;
ans = m - (n - 2 * k + 1) / 2;
} else { // Observation 3
long mod = n % k, finmod = (m * 2) % k, dif = (finmod - mod + k) % k;
long x = 2 * m + 3 * k + 1, pow = 4;
long prepos = 2 * (k + m) - dif, preans = Math.min(m, dif / 2);
while (true) {
if (n < x) {
pos = prepos;
ans = preans;
break;
}
finmod = (x + k - 1) % k;
dif = (finmod - mod + k) % k;
prepos = x + k - 1 - dif;
preans = Math.min(dif / 2, m);
if (n < x + k) {
pos = prepos;
ans = preans;
break;
}
x = x + pow * k;
pow = pow * 2;
}
}
sb.append(pos).append(" ").append(ans).append("\n");
}
System.out.print(sb);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.