niyaznigmatullin

Untitled

Oct 14th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class polynoms_nn {
  5.  
  6.     static long[] get(int[] aN) {
  7.         long[] a = new long[(aN.length >> 6) + 1];
  8.         for (int i = 0; i < aN.length; i++) {
  9.             a[i >> 6] |= (long) aN[i] << i;
  10.         }
  11.         return a;
  12.     }
  13.  
  14.     static void gcd(long[][] f, long[] a, long[] b) {
  15.         int bitLength1 = getBitLength(a);
  16.         int bitLength2 = getBitLength(b);
  17.         if (bitLength1 == 0) {
  18.             f[1][0] = 1;
  19.             return;
  20.         }
  21.         if (bitLength2 == 0) {
  22.             f[0][0] = 1;
  23.             return;
  24.         }
  25.         boolean flipped = false;
  26.         if (bitLength1 < bitLength2) {
  27.             flipped = true;
  28.             long[] t = a;
  29.             a = b;
  30.             b = t;
  31.             bitLength1 ^= bitLength2;
  32.             bitLength2 ^= bitLength1;
  33.             bitLength1 ^= bitLength2;
  34.         }
  35.         int dif = bitLength1 - bitLength2;
  36.         xorShift(a, b, dif);
  37.         gcd(f, a, b);
  38.         xorShift(f[1], f[0], dif);
  39.         if (flipped) {
  40.             long[] t = f[0];
  41.             f[0] = f[1];
  42.             f[1] = t;
  43.         }
  44.     }
  45.  
  46.     private static void xorShift(long[] a, long[] b, int dif) {
  47.         int whole = dif >> 6;
  48.         dif &= 63;
  49.         for (int i = 0; i < b.length; i++) {
  50.             if (i + whole < a.length) {
  51.                 a[i + whole] ^= b[i] << dif;
  52.             }
  53.             if (dif != 0 && i + whole + 1 < a.length) {
  54.                 a[i + whole + 1] ^= b[i] >>> (64 - dif);
  55.             }
  56.         }
  57.     }
  58.  
  59.     private static int getBitLength(long[] a) {
  60.         for (int i = a.length - 1; i >= 0; i--) {
  61.             if (a[i] != 0) {
  62.                 return (i << 6) + Long.numberOfTrailingZeros(Long.highestOneBit(a[i])) + 1;
  63.             }
  64.         }
  65.         return 0;
  66.     }
  67.  
  68.     static int[][] solve(int[] aN, int[] bN) {
  69.         long[] a = get(aN);
  70.         long[] b = get(bN);
  71.         long[][] f = new long[2][Math.max(a.length, b.length)];
  72.         gcd(f, a, b);
  73.         int[] ret1 = fromBitSet(f[0]);
  74.         int[] ret2 = fromBitSet(f[1]);
  75.         return new int[][]{ret1, ret2};
  76.     }
  77.  
  78.     private static int[] fromBitSet(long[] a) {
  79.         int[] ret1 = new int[getBitLength(a)];
  80.         if (ret1.length == 0) return new int[1];
  81.         for (int i = 0; i < ret1.length; i++) {
  82.             ret1[i] = (int) ((a[i >> 6] >>> i) & 1);
  83.         }
  84.         return ret1;
  85.     }
  86.  
  87.     static int readInt(BufferedReader input) throws IOException {
  88.         int c = input.read();
  89.         while (c <= 32) c = input.read();
  90.         int ans = 0;
  91.         while (c > 32) {
  92.             ans = ans * 10 + c - '0';
  93.             c = input.read();
  94.         }
  95.         return ans;
  96.     }
  97.  
  98.     public static void main(String[] args) throws IOException {
  99.         long time = System.currentTimeMillis();
  100.         InputStream inputStream = System.in;
  101.         PrintStream outputStream = System.out;
  102.         if (System.getProperty("NOFILES") == null) {
  103.             inputStream = new FileInputStream(new File("polynoms.in"));
  104.             outputStream = new PrintStream("polynoms.out");
  105.         }
  106.         BufferedReader input = new BufferedReader(new InputStreamReader(inputStream));
  107.         PrintWriter output = new PrintWriter(outputStream);
  108.         int t = readInt(input);
  109.         for (int i = 0; i < t; i++) {
  110.             int[] a = new int[readInt(input) + 1];
  111.             for (int j = 0; j < a.length; j++) {
  112.                 a[j] = readInt(input);
  113.             }
  114.             int[] b = new int[readInt(input) + 1];
  115.             for (int j = 0; j < b.length; j++) {
  116.                 b[j] = readInt(input);
  117.             }
  118.             int[][] ans = solve(a, b);
  119.             for (int[] e : ans) {
  120.                 output.print(e.length - 1);
  121.                 for (int anE : e) {
  122.                     output.print(' ');
  123.                     output.print(anE);
  124.                 }
  125.                 output.println();
  126.             }
  127.         }
  128.         output.close();
  129.         System.err.print(System.currentTimeMillis() - time);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment