fork download
  1. #include<bits/stdc++.h>
  2. #include <queue>
  3. typedef long long int ll;
  4. using namespace std;
  5.  
  6.  
  7. int main() {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(NULL);
  10. int t, n;
  11. cin>>t;
  12. long long int sum = 0, a, b, c, d, med, z;
  13. while (t > 0) {
  14. t--;
  15. priority_queue <ll> max;
  16. priority_queue <ll, vector<ll>, greater<ll> > min;
  17. cin>>a>>b>>c>>n;
  18. d = b;
  19. sum = 1;
  20. max.push(1l);
  21. b += d;
  22. for(int i = 2; i <= n; i++) {
  23. int mxs = max.size(), mis = min.size();
  24. ll mxp = max.top();
  25. ll mip = -1l;
  26. if(mis != 0) mip = min.top();
  27. med = mxp;
  28. z = ((a * med) + b + c) % 1000000007;
  29. b += d;
  30. sum += z;
  31. if(mxs == mis) {
  32. if(mis == 0) max.push(z);
  33. else if(mxp <= z && mip >= z) {
  34. max.push(z);
  35. }
  36. else if(mip < z) {
  37. max.push(min.top());
  38. min.pop();
  39. min.push(z);
  40. }
  41. else {
  42. max.push(z);
  43. }
  44. }
  45. else {
  46. if(mis == 0) {
  47. if(mxp <= z) min.push(z);
  48. else {
  49. min.push(max.top());
  50. max.pop();
  51. max.push(z);
  52. }
  53. }
  54. else if(mxp <= z) {
  55. min.push(z);
  56. }
  57. else {
  58. min.push(max.top());
  59. max.pop();
  60. max.push(z);
  61. }
  62. }
  63. }
  64. cout<<sum<<endl;
  65. }
  66. }
  67.  
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
Standard output is empty