IzhanVarsky

Криптография

Mar 19th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.charset.Charset;
  3. import java.nio.charset.StandardCharsets;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     private static int r;
  8.     private static int[][] t;
  9. //    private static final int[] neitral = {1, 0, 0, 1};
  10.  
  11.     private static int[] getMulInRange(int v, int left, int right, int a, int b) {
  12.         if (a > right || b < left) return new int[]{1, 0, 0, 1};
  13.         if (a == left && b == right) return t[v];
  14.         int mid = (left + right) / 2;
  15.         return mul(getMulInRange(v * 2 + 1, left, mid, a, Math.min(b, mid)),
  16.                 getMulInRange(v * 2 + 2, mid + 1, right, Math.max(a, mid + 1), b));
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         try (Scanner in = new Scanner(new FileReader("crypto.in"));
  21.              BufferedWriter writer = new BufferedWriter(new FileWriter("crypto.out"))){
  22.             r = in.nextInt();
  23.             int n = in.nextInt();
  24.             int m = in.nextInt();
  25.             int newN = getNewN(n);
  26.             t = new int[newN * 2 - 1][4];
  27.             // build
  28.             for (int i = 0; i < newN * 2 - 1; i++)
  29.                 t[i] = new int[]{1, 0, 0, 1};
  30.             for (int i = 0; i < n; i++) {
  31.                 int q = newN + i - 1;
  32.                 for (int j = 0; j < 4; j++) {
  33.                     int z = in.nextInt();
  34.                     t[q][j] = z;
  35.                 }
  36.             }
  37.  
  38.             for (int i = newN - 2; i >= 0; i--)
  39.                 setMul(i);
  40.             //
  41.             for (int i = 0; i < m; i++) {
  42.                 int res[] = getMulInRange(0, 0, newN - 1, in.nextInt() - 1, in.nextInt() - 1);
  43.                 writer.write(res[0] + " " + res[1] + '\n');
  44.                 writer.write(res[2] + " " + res[3] + '\n' + '\n');
  45.             }
  46.         } catch(IOException e){
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     private static void setMul(int i) {
  52.         t[i] = mul(t[i * 2 + 1], t[i * 2 + 2]);
  53.     }
  54.  
  55.     private static int getNewN(int n) {
  56.         int res = 1;
  57.         while (res < n)
  58.             res <<= 1;
  59.         return res;
  60.     }
  61.  
  62.     private static int[] reverse(int[] a) {
  63.         int det = a[0] * a[3] - a[1] * a[2];
  64.         if (det == 0) det = 1;
  65.         int res[] = new int[4];
  66.         res[0] = a[3] / det;
  67.         res[1] = -a[2] / det;
  68.         res[2] = -a[1] / det;
  69.         res[3] = a[0] / det;
  70.         for (int i = 0; i < 4; i++) {
  71.             res[i] %= r;
  72.             if (res[i] < 0) res[i] += r;
  73.         }
  74.         return res;
  75.     }
  76.  
  77.     private static int[] mul(int[] a, int[] b) {
  78.         int res[] = new int[4];
  79.         res[0] = a[0] * b[0] + a[1] * b[2];
  80.         res[1] = a[0] * b[1] + a[1] * b[3];
  81.         res[2] = a[2] * b[0] + a[3] * b[2];
  82.         res[3] = a[2] * b[1] + a[3] * b[3];
  83.         for (int i = 0; i < 4; i++) {
  84.             res[i] %= r;
  85.             if (res[i] < 0) res[i] += r;
  86.         }
  87.         return res;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment