unable to understand why if(last*2 < g) is done,please someone explain. Thankyou.
include "bits/stdc++.h"
using namespace std;
int main() { int t; scanf ("%d", &t); while (t--) { long long n, k, g; scanf ("%lld %lld %lld", &n, &k, &g);
long long stolen = min((g - 1) / 2 * n, k * g);
long long rest = (k * g - stolen) % g;
if (rest > 0) {
stolen -= (g - 1) / 2;
long long last = ((g - 1) / 2 + rest) % g;
if (last * 2 < g) {
stolen += last;
} else {
stolen -= g - last;
}
}
printf ("%lld\n", stolen);
}
}