fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. static class Reader {
  6. final private int BUFFER_SIZE = 1 << 16;
  7. private DataInputStream din;
  8. private byte[] buffer;
  9. private int bufferPointer, bytesRead;
  10.  
  11. public Reader() {
  12. din = new DataInputStream(System.in);
  13. buffer = new byte[BUFFER_SIZE];
  14. bufferPointer = bytesRead = 0;
  15. }
  16.  
  17. public Reader(String file_name) throws IOException {
  18. din = new DataInputStream(new FileInputStream(file_name));
  19. buffer = new byte[BUFFER_SIZE];
  20. bufferPointer = bytesRead = 0;
  21. }
  22.  
  23. public String nextLine() throws IOException {
  24. byte[] buf = new byte[64]; // line length
  25. int cnt = 0, c;
  26. while ((c = read()) != -1) {
  27. if (c == '\n')
  28. break;
  29. buf[cnt++] = (byte) c;
  30. }
  31. return new String(buf, 0, cnt);
  32. }
  33.  
  34. public String next() throws IOException {
  35. byte[] buf = new byte[64]; // line length
  36. int cnt = 0, c;
  37. while ((c = read()) != -1) {
  38. if (c == ' ')
  39. break;
  40. buf[cnt++] = (byte) c;
  41. }
  42. return new String(buf, 0, cnt);
  43. }
  44.  
  45. public int nextInt() throws IOException {
  46. int ret = 0;
  47. byte c = read();
  48. while (c <= ' ')
  49. c = read();
  50. boolean neg = (c == '-');
  51. if (neg)
  52. c = read();
  53. do {
  54. ret = ret * 10 + c - '0';
  55. } while ((c = read()) >= '0' && c <= '9');
  56.  
  57. if (neg)
  58. return -ret;
  59. return ret;
  60. }
  61.  
  62. public long nextLong() throws IOException {
  63. long ret = 0;
  64. byte c = read();
  65. while (c <= ' ')
  66. c = read();
  67. boolean neg = (c == '-');
  68. if (neg)
  69. c = read();
  70. do {
  71. ret = ret * 10 + c - '0';
  72. } while ((c = read()) >= '0' && c <= '9');
  73. if (neg)
  74. return -ret;
  75. return ret;
  76. }
  77.  
  78. public double nextDouble() throws IOException {
  79. double ret = 0, div = 1;
  80. byte c = read();
  81. while (c <= ' ')
  82. c = read();
  83. boolean neg = (c == '-');
  84. if (neg)
  85. c = read();
  86.  
  87. do {
  88. ret = ret * 10 + c - '0';
  89. } while ((c = read()) >= '0' && c <= '9');
  90.  
  91. if (c == '.') {
  92. while ((c = read()) >= '0' && c <= '9') {
  93. ret += (c - '0') / (div *= 10);
  94. }
  95. }
  96.  
  97. if (neg)
  98. return -ret;
  99. return ret;
  100. }
  101.  
  102. private void fillBuffer() throws IOException {
  103. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  104. if (bytesRead == -1)
  105. buffer[0] = -1;
  106. }
  107.  
  108. private byte read() throws IOException {
  109. if (bufferPointer == bytesRead)
  110. fillBuffer();
  111. return buffer[bufferPointer++];
  112. }
  113.  
  114. public void close() throws IOException {
  115. if (din == null)
  116. return;
  117. din.close();
  118. }
  119. }
  120.  
  121. static Reader s = new Reader();
  122. static PrintWriter out = new PrintWriter(System.out);
  123.  
  124. public static void main(String[] args) throws IOException {
  125. int t = s.nextInt();
  126. long sum = 0, a, b, c, d, med, z; int n;
  127. while (t > 0) {
  128. t--;
  129. PriorityQueue<Long> max = new PriorityQueue<>(new Compare1());
  130. PriorityQueue<Long> min = new PriorityQueue<>();
  131. a = s.nextLong();
  132. b = s.nextLong();
  133. c = s.nextLong();
  134. n = s.nextInt();
  135. d = b;
  136. sum = 1;
  137. max.add(1l);
  138. b += d;
  139. for(int i = 2; i <= n; i++) {
  140. int mxs = max.size(), mis = min.size();
  141. long mxp = max.peek();
  142. long mip = -1l;
  143. if(mis != 0) mip = min.peek();
  144. med = mxp;
  145. z = ((a * med) + b + c) % 1000000007;
  146. b += d;
  147. //if(i == 1) z = 1;
  148. sum += z;
  149. if(mxs == mis) {
  150. if(mis == 0) max.add(z);
  151. else if(mxp <= z && mip >= z) {
  152. max.add(z);
  153. }
  154. else if(mip < z) {
  155. max.add(min.poll());
  156. min.add(z);
  157. }
  158. else {
  159. max.add(z);
  160. }
  161. }
  162. else {
  163. if(mis == 0) {
  164. if(mxp <= z) min.add(z);
  165. else {
  166. min.add(max.poll());
  167. max.add(z);
  168. }
  169. }
  170. else if(mxp <= z) {
  171. min.add(z);
  172. }
  173. else {
  174. min.add(max.poll());
  175. max.add(z);
  176. }
  177. }
  178. }
  179. max = null; min = null;
  180. sb.append(sum + "\n");
  181. }
  182. out.print(sb);
  183. out.flush();
  184. }
  185.  
  186. }
  187.  
  188.  
  189. class Compare1 implements Comparator<Long>{
  190.  
  191. @Override
  192. public int compare(Long p1, Long p2) {
  193. return (p1 - p2 > 0) ? -1 : 1;
  194. }
  195. }
Success #stdin #stdout 0.08s 34120KB
stdin
2
1 0 0 3
3 1 2 6
stdout
3
103