fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void solve(){
  4. long long int a,b,k;
  5. cin>>a>>b>>k;
  6.  
  7. long long int diff = a - b;
  8.  
  9. if(k==1){
  10. cout<<0;
  11. }
  12. else if(k==2){
  13. cout<<min(a,b);
  14. }
  15. else if(diff > 0){ // diff is positive so 1 6 2 8 3 10 4 12 5 14 7 16 pattern
  16. // 0 50 10 60 20 70 30 80 40 90 50 100
  17. // a b && a/b = 5
  18.  
  19. long long int div = a/b;
  20.  
  21. if(k <= div + 1){
  22. cout<<b * (k - 1)<<"\n";
  23. return;
  24. }
  25.  
  26. k -= (div + 1);
  27. if(k & 1){
  28. cout<<a+((k/2)*b);
  29. }else{
  30. cout<<(div+(k/2))*b;
  31. }
  32.  
  33.  
  34. }else{ //diff will be negative so 1 2 3 4 5 6 7 pattern
  35. // 0 1 3 4 6 7 9
  36. if(k&1){
  37. cout<<b*((k-1)/2);
  38. }else{
  39. cout<<b*((k)/2)+diff;
  40. }
  41.  
  42. }
  43. cout<<"\n";
  44. }
  45. signed main() {
  46. // your code goes here
  47. int tc;
  48. cin>>tc;
  49.  
  50. while(tc--){
  51.  
  52. solve();
  53.  
  54. }
  55. }
Success #stdin #stdout 0s 5284KB
stdin
6
16 41 1501
50 10 5
50 10 6
50 10 7
50 10 8
50 10 16
stdout
30750
40
50
50
60
100