Advertisement
daniel_079

SumOnSection

Mar 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package LabaFirst;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SumOnSection {
  6.  
  7.     public static void build (long a[], long tree[]) {
  8.         tree[0] = a[0];
  9.         for (int i = 1; i < a.length; i++) {
  10.             tree[i] = tree[i-1] + a[i];
  11.         }
  12.     }
  13.  
  14.     public static void main (String args[]) {
  15.         long answer = 0;
  16.         Scanner in = new Scanner(System.in);
  17.         int n = in.nextInt();
  18.         long x = in.nextLong();
  19.         long y = in.nextLong();
  20.         long[]a = new long[n];
  21.         a[0] = in.nextLong();
  22.         for (int i = 1; i < n; i++) {
  23.             a[i] = (long) ((x * a[i - 1] + y + Math.pow(2, 16)) % Math.pow(2, 16));
  24.         }
  25.         int m = in.nextInt();
  26.         long z = in.nextLong();
  27.         long t = in.nextLong();
  28.         long[]b = new long[2 * m];
  29.         b[0] = in.nextLong();
  30.         for (int i = 1; i < b.length; i++) {
  31.             b[i] = (long) ((z * b[i - 1] + t + Math.pow(2, 30)) % Math.pow(2, 30));
  32.         }
  33.         long[]c = new long[2 * m];
  34.         for (int i = 0; i < c.length; i++) {
  35.             c[i] = b[i] % n;
  36.         }
  37.         long tree[] = new long[n];
  38.         build(a, tree);
  39.         for (int i = 0; i < m; i++) {
  40.             if (c[2*i] < c[2*i + 1]) {
  41.                 if (c[2*i] == 0) {
  42.                     answer = answer + tree[(int) c[2*i + 1]];
  43.                 } else {
  44.                     answer = answer + tree[(int) c[2*i + 1]] - tree[(int) (c[2*i] - 1)];
  45.                 }
  46.             } else {
  47.                 if (c[2*i + 1] == 0) {
  48.                     answer = answer + tree[(int) c[2*i]];
  49.                 } else {
  50.                     answer = answer + tree[(int) c[2*i]] - tree[(int) (c[2*i + 1] - 1)];
  51.                 }
  52.             }
  53.         }
  54.         in.close();
  55.         System.out.println(answer);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement