Cerinta completa

Consider a zero-indexed matrix with rows and columns, where each row is filled gradually. Given the first row of the matrix, you can generate the elements in the subsequent rows using the following formula:

Each row is generated one by one, from the second row through the last row. Given the first row of the matrix, find and print the elements of the last row as a single line of space-separated integers.

Note: The operator denotes bitwise XOR.

Input Format

The first line contains two space-separated integers denoting the respective values of (the number of columns in the matrix) and (the number of rows in the matrix).
The second line contains space-separated integers denoting the respective values of the elements in the matrix’s first row.

Constraints

Output Format

Print space-separated integers denoting the respective values of the elements in the last row of the matrix.

Sample Input 0

4 2
6 7 1 3

Sample Output 0

1 6 2 5

Explanation 0

We use the formula given above to calculate the values in the last row of the matrix:

We then print each value (in order) as a single line of space-separated integers.


Limbajul de programare folosit: java8

Cod:

import java.io.*;

public class Solution {
    private static class FastScanner {
        private final InputStream in;
        private final byte[] buffer = new byte[1 << 16];
        private int ptr = 0, len = 0;
        FastScanner(InputStream is) { this.in = is; }
        private int read() throws IOException {
            if (ptr >= len) {
                len = in.read(buffer);
                ptr = 0;
                if (len <= 0) return -1;
            }
            return buffer[ptr++];
        }
        long nextLong() throws IOException {
            int c;
            do { c = read(); } while (c <= ' ' && c != -1);
            int sign = 1;
            if (c == '-') { sign = -1; c = read(); }
            long val = 0;
            while (c > ' ') {
                val = val * 10 + (c - '0');
                c = read();
            }
            return sign == 1 ? val : -val;
        }
        int nextInt() throws IOException { return (int) nextLong(); }
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner(System.in);
        int n = fs.nextInt();
        long m = fs.nextLong();

        int[] cur = new int[n];
        for (int i = 0; i < n; i++) cur[i] = fs.nextInt();

        long steps = m - 1;
        int[] nxt = new int[n];

        int bit = 0;
        while (steps > 0) {
            if ((steps & 1L) == 1L) {
                int shift = (int) ((1L << bit) % n);
                for (int i = 0; i < n; i++) {
                    int j = i + shift;
                    if (j >= n) j -= n;
                    nxt[i] = cur[i] ^ cur[j];
                }
                int[] tmp = cur;
                cur = nxt;
                nxt = tmp;
            }
            steps >>= 1;
            bit++;
        }

        StringBuilder out = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (i > 0) out.append(' ');
            out.append(cur[i]);
        }
        System.out.println(out);
    }
}

Scor obtinut: 1.0

Submission ID: 464618021

Link challenge: https://www.hackerrank.com/challenges/xor-matrix/problem

XOR Matrix