Advertisement
Guest User

Untitled

a guest
Oct 19th, 2014
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class oc141019D {
  4.  
  5.     static int cur = 0, a, b;
  6.     static int nextRand() {
  7.         cur = cur * a + b;
  8.         return cur >>> 16;
  9.     }
  10.  
  11.     static void conv(int[] f, int sign) {
  12.         for (int bit = 1; bit < f.length; bit *= 2) {
  13.             int mask = f.length - bit - 1;
  14.             for (int i = mask; ; i = (i - 1) & mask) {
  15.                 f[i | bit] += sign * f[i];
  16.                 if (i == 0) {
  17.                     break;
  18.                 }
  19.             }
  20.         }
  21.     }
  22.  
  23.     public static void solve(Input in, PrintWriter out) throws IOException {
  24.         int n = in.nextInt();
  25.         int tests = in.nextInt();
  26.         a = in.nextInt();
  27.         b = in.nextInt();
  28.         for (int test = 0; test < tests; ++test) {
  29.             int[] f = new int[1 << n];
  30.             for (int i = 0; i < 1 << n; ++i) {
  31.                 f[i] = nextRand();
  32.             }
  33.             int[] g = new int[1 << n];
  34.             for (int i = 0; i < 1 << n; ++i) {
  35.                 g[i] = nextRand();
  36.             }
  37.             conv(f, 1);
  38.             conv(g, 1);
  39.             for (int i = 0; i < 1 << n; ++i) {
  40.                 f[i] *= g[i];
  41.             }
  42.             conv(f, -1);
  43.             int ans = 0;
  44.             for (int i = 0; i < 1 << n; ++i) {
  45.                 ans += f[i] * (i + 1);
  46.             }
  47.             out.println((long)(ans) & ((1L << 32) - 1));
  48.         }
  49.     }
  50.  
  51.     public static void main(String[] args) throws IOException {
  52.         PrintWriter out = new PrintWriter("convolution.out");
  53.         solve(new Input(new BufferedReader(new FileReader("convolution.in"))), out);
  54.         out.close();
  55.     }
  56.  
  57.     static class Input {
  58.         BufferedReader in;
  59.         StringBuilder sb = new StringBuilder();
  60.  
  61.         public Input(BufferedReader in) {
  62.             this.in = in;
  63.         }
  64.  
  65.         public Input(String s) {
  66.             this.in = new BufferedReader(new StringReader(s));
  67.         }
  68.  
  69.         public String next() throws IOException {
  70.             sb.setLength(0);
  71.             while (true) {
  72.                 int c = in.read();
  73.                 if (c == -1) {
  74.                     return null;
  75.                 }
  76.                 if (" \n\r\t".indexOf(c) == -1) {
  77.                     sb.append((char)c);
  78.                     break;
  79.                 }
  80.             }
  81.             while (true) {
  82.                 int c = in.read();
  83.                 if (c == -1 || " \n\r\t".indexOf(c) != -1) {
  84.                     break;
  85.                 }
  86.                 sb.append((char)c);
  87.             }
  88.             return sb.toString();
  89.         }
  90.  
  91.         public int nextInt() throws IOException {
  92.             return Integer.parseInt(next());
  93.         }
  94.  
  95.         public long nextLong() throws IOException {
  96.             return Long.parseLong(next());
  97.         }
  98.  
  99.         public double nextDouble() throws IOException {
  100.             return Double.parseDouble(next());
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement