Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class polynoms_nn {
- static long[] get(int[] aN) {
- long[] a = new long[(aN.length >> 6) + 1];
- for (int i = 0; i < aN.length; i++) {
- a[i >> 6] |= (long) aN[i] << i;
- }
- return a;
- }
- static void gcd(long[][] f, long[] a, long[] b) {
- int bitLength1 = getBitLength(a);
- int bitLength2 = getBitLength(b);
- if (bitLength1 == 0) {
- f[1][0] = 1;
- return;
- }
- if (bitLength2 == 0) {
- f[0][0] = 1;
- return;
- }
- boolean flipped = false;
- if (bitLength1 < bitLength2) {
- flipped = true;
- long[] t = a;
- a = b;
- b = t;
- bitLength1 ^= bitLength2;
- bitLength2 ^= bitLength1;
- bitLength1 ^= bitLength2;
- }
- int dif = bitLength1 - bitLength2;
- xorShift(a, b, dif);
- gcd(f, a, b);
- xorShift(f[1], f[0], dif);
- if (flipped) {
- long[] t = f[0];
- f[0] = f[1];
- f[1] = t;
- }
- }
- private static void xorShift(long[] a, long[] b, int dif) {
- int whole = dif >> 6;
- dif &= 63;
- for (int i = 0; i < b.length; i++) {
- if (i + whole < a.length) {
- a[i + whole] ^= b[i] << dif;
- }
- if (dif != 0 && i + whole + 1 < a.length) {
- a[i + whole + 1] ^= b[i] >>> (64 - dif);
- }
- }
- }
- private static int getBitLength(long[] a) {
- for (int i = a.length - 1; i >= 0; i--) {
- if (a[i] != 0) {
- return (i << 6) + Long.numberOfTrailingZeros(Long.highestOneBit(a[i])) + 1;
- }
- }
- return 0;
- }
- static int[][] solve(int[] aN, int[] bN) {
- long[] a = get(aN);
- long[] b = get(bN);
- long[][] f = new long[2][Math.max(a.length, b.length)];
- gcd(f, a, b);
- int[] ret1 = fromBitSet(f[0]);
- int[] ret2 = fromBitSet(f[1]);
- return new int[][]{ret1, ret2};
- }
- private static int[] fromBitSet(long[] a) {
- int[] ret1 = new int[getBitLength(a)];
- if (ret1.length == 0) return new int[1];
- for (int i = 0; i < ret1.length; i++) {
- ret1[i] = (int) ((a[i >> 6] >>> i) & 1);
- }
- return ret1;
- }
- static int readInt(BufferedReader input) throws IOException {
- int c = input.read();
- while (c <= 32) c = input.read();
- int ans = 0;
- while (c > 32) {
- ans = ans * 10 + c - '0';
- c = input.read();
- }
- return ans;
- }
- public static void main(String[] args) throws IOException {
- long time = System.currentTimeMillis();
- InputStream inputStream = System.in;
- PrintStream outputStream = System.out;
- if (System.getProperty("NOFILES") == null) {
- inputStream = new FileInputStream(new File("polynoms.in"));
- outputStream = new PrintStream("polynoms.out");
- }
- BufferedReader input = new BufferedReader(new InputStreamReader(inputStream));
- PrintWriter output = new PrintWriter(outputStream);
- int t = readInt(input);
- for (int i = 0; i < t; i++) {
- int[] a = new int[readInt(input) + 1];
- for (int j = 0; j < a.length; j++) {
- a[j] = readInt(input);
- }
- int[] b = new int[readInt(input) + 1];
- for (int j = 0; j < b.length; j++) {
- b[j] = readInt(input);
- }
- int[][] ans = solve(a, b);
- for (int[] e : ans) {
- output.print(e.length - 1);
- for (int anE : e) {
- output.print(' ');
- output.print(anE);
- }
- output.println();
- }
- }
- output.close();
- System.err.print(System.currentTimeMillis() - time);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment