Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <memory.h>
  4. #include <time.h>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <functional>
  8. #include <set>
  9. #include <map>
  10. #include <string>
  11. #include <cstring>
  12. #include <sstream>
  13. #include <fstream>
  14. #include <stack>
  15. #include <queue>
  16. #include <bitset>
  17. #include <vector>
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <assert.h>
  21.  
  22. #define ALL(x) x.begin(), x.end()
  23. #define fill(a, b) memset(a, b, sizeof(a))
  24. #define abs(x) ((x)>0?(x):-(x))
  25. #define sqr(x) (1ll*(x)*(x))
  26. #define min(a,b) ((a)>(b)?(b):(a))
  27. #define max(a,b) ((a)<(b)?(b):(a))
  28. #define gcd(a,b) __gcd(a,b)
  29. #define F first
  30. #define S second
  31. #define SS stringstream
  32. #define CC(x) cout << x << endl
  33. #define CCS(x) cout << x << " "
  34. #define pw(x) (1ll<<(x))
  35. #define pb push_back
  36. #define mp make_pair
  37. #define FIN freopen("1.in", "r", stdin)
  38. #define FOUT freopen("1.out", "w", stdout)
  39. #define FILE FIN; FOUT
  40. #define SC(x) scanf("%d", &x)
  41. #define PR(x) printf("%d\n", x)
  42. #define PRS(x) printf("%d ", x)
  43. #define bits(mask) __builtin_popcount(mask)
  44. #define bit(mask, i) ((mask & pw(i-1)) > 0)
  45. #define pii pair<int,int>
  46. #define vi vector<int>
  47. #define vii vector<pair<int,int> >
  48. #define forr(i, l, r) for (int i = l; i <= r; i++)
  49. #define ford(i, r, l) for (int i = r; i >= l; i--)
  50. #define SRD srand((int)time(NULL))
  51. #define GC getchar()
  52. #define PC(x) putchar(x)
  53. #define left lft
  54. #define right rght
  55. #define log lg
  56. #define y1 yyy1
  57.  
  58. typedef long long LL;
  59. typedef unsigned long long ULL;
  60. typedef double DD;
  61. typedef long double LD;
  62. typedef unsigned char UC;
  63. typedef unsigned int UI;
  64. typedef unsigned short US;
  65.  
  66. using namespace std;
  67.  
  68.     inline LL solve(LL x, LL y, LL k, LL t)
  69.     {
  70.         if (t < 0 || k < 0) return -1e18;
  71.         LL q = min(k, t);
  72.         x += y*q;
  73.         k -= q; t -= q;
  74.         return x+t;
  75.     }
  76.  
  77. int main(int argc, char * argv[])
  78. {
  79.     //ios_base::sync_with_stdio(0);
  80.     //FIN;
  81.     int ttt;
  82.     SC(ttt);
  83.     while (ttt--)
  84.     {
  85.         int x, y, k, t;
  86.         SC(x); SC(y); SC(k); SC(t);
  87.         if (y == 1) {PR(x+t); continue;}
  88.         if (x%y == 0) CC(solve(x, y, k, t)); else
  89.         {
  90.             LL next = 1ll*((x/y)+1)*y;
  91.             LL r1 = solve(next, y, k-1, t);
  92.             LL r2 = solve(next, y, k, t-(next-x));
  93.             CC(max(r1, r2));
  94.         }
  95.     }
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement