fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t;
  6. cin >> t;
  7. while (t--) {
  8. long long s, k, m;
  9. cin >> s >> k >> m;
  10.  
  11. long long flipsCount = m / k;
  12. // Number of times the hourglass was flipped
  13.  
  14. long long sandAfterFlip;
  15. // Amount of sand remaining right after the last flip
  16.  
  17. if (k >= s) {
  18. // Sand always finishes before the next flip
  19. sandAfterFlip = s;
  20. } else {
  21. if (flipsCount % 2 == 0)
  22. // Even number of flips → sand is fully restored
  23. sandAfterFlip = s;
  24. else
  25. // Odd number of flips → only k minutes of sand
  26. sandAfterFlip = k;
  27. }
  28.  
  29. long long timeAfterLastFlip = m % k;
  30. // Minutes passed after the last flip
  31.  
  32. long long remainingSand = max(0LL, sandAfterFlip - timeAfterLastFlip);
  33. // Sand that will continue falling after Vadim leaves
  34.  
  35. cout << remainingSand << '\n';
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5284KB
stdin
6
8 8 12
5 10 17
12 2 3
16 7 7
1 1 10
2 60 15
stdout
4
0
1
7
1
0