結果

問題 No.950 行列累乗
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-13 01:28:41
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 9,723 bytes
コンパイル時間 1,394 ms
コンパイル使用メモリ 131,440 KB
実行使用メモリ 11,496 KB
最終ジャッジ日時 2023-09-04 03:52:33
合計ジャッジ時間 6,586 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 45 ms
7,520 KB
testcase_06 RE -
testcase_07 AC 2 ms
4,376 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 73 ms
11,496 KB
testcase_24 RE -
testcase_25 AC 49 ms
9,276 KB
testcase_26 AC 68 ms
9,796 KB
testcase_27 AC 94 ms
11,244 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 AC 73 ms
9,988 KB
testcase_41 AC 72 ms
9,868 KB
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 AC 61 ms
10,880 KB
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(359): Warning: statement is not reachable
Main.d(359): Warning: statement is not reachable

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// floor(a^(1/k))
ulong floorKthRoot(ulong a, ulong k) {
  import core.bitop : bsr;
  import std.algorithm : min;
  if (a == 0) {
    return 0;
  } else if (k <= 1) {
    return a;
  } else if (k == 2) {
    ulong b = a, x = 0, y = 0;
    for (int e = bsr(a) & ~1; e >= 0; e -= 2) {
      x <<= 1;
      y <<= 1;
      if (b >= (y | 1) << e) {
        b -= (y | 1) << e;
        x |= 1;
        y += 2;
      }
    }
    return x;
  } else if (k <= 40) {
    // min x s.t. x^k >= 2^64
    enum ulong[] HIS =
        [0, 0, 4294967296UL, 2642246, 65536, 7132, 1626, 566, 256, 139, 85, 57,
         41, 31, 24, 20, 16, 14, 12, 11, 10, 9, 8, 7, 7, 6, 6, 6, 5, 5, 5, 5,
         4, 4, 4, 4, 4, 4, 4, 4, 4];
    ulong lo = 1UL << (bsr(a) / k);
    ulong hi = min(1UL << (bsr(a) / k + 1), HIS[cast(size_t)(k)]);
    for (; lo + 1 < hi; ) {
      const ulong mid = (lo + hi) / 2;
      ulong b = mid * mid;
      foreach (i; 2 .. k) b *= mid;
      ((b <= a) ? lo : hi) = mid;
    }
    return lo;
  } else if (k <= 63) {
    return ((1UL << k) <= a) ? 2 : 1;
  } else {
    return 1;
  }
}


// xorshift
uint xrand() {
  static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}

// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b; a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) y = -y;
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a; b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) x = -x;
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}

// (a / 2) mod m
long div2(long a, long m)
in {
  assert(1 <= m && m < 1L << 62, "div2: 1 <= m < 2^62 must hold");
  assert(m % 2 != 0, "div2: m must not be divisibly by 3");
  assert(0 <= a && a < m, "div2: 0 <= a < m must hold");
}
do {
  return (a + (a % 2) * m) / 2;
}

// (a / 3) mod m
long div3(long a, long m)
in {
  assert(1 <= m && m < 1L << 61, "div3: 1 <= m < 2^61 must hold");
  assert(m % 3 != 0, "div3: m must not be divisibly by 3");
  assert(0 <= a && a < m, "div3: 0 <= a < m must hold");
}
do {
  return (a + (((3 - a % 3) * (m % 3)) % 3) * m) / 3;
}

// Jacobi symbol (a/m)
int jacobi(long a, long m)
in {
  assert(m > 0, "jacobi: m > 0 must hold");
  assert(m & 1, "jacobi: m must be odd");
}
do {
  import core.bitop : bsf;
  import std.algorithm.mutation : swap;
  int s = 1;
  if (a < 0) a = a % m + m;
  for (; m > 1; ) {
    a %= m;
    if (a == 0) return 0;
    const r = bsf(a);
    if ((r & 1) && ((m + 2) & 4)) s = -s;
    a >>= r;
    if (a & m & 2) s = -s;
    swap(a, m);
  }
  return s;
}

// sqrt(a) (mod p)
//   p: be prime
//   (b + sqrt(b^2 - a))^((p+1)/2) in F_p(sqrt(b^2 - a))
long[] modSqrt(long a, long p)
in {
  assert(p < 1L << 31, "modSqrt: p < 2^31 must hold");
  assert(-p * p <= a && a <= p * p, "modSqrt: -p^2 <= a <= p^2 must hold");
}
do {
  if (p == 2) return [a & 1];
  const j = jacobi(a, p);
  if (j == 0) return [0];
  if (j == -1) return [];
  long b, d;
  for (; ; ) {
    b = xrand() % p;
    d = (b * b - a) % p;
    if (d < 0) d += p;
    if (jacobi(d, p) == -1) break;
  }
  long[2] mul(in long[2] x, in long[2] y) {
    return [(x[0] * y[0] + d * ((x[1] * y[1]) % p)) % p,
            (x[0] * y[1] + x[1] * y[0]) % p];
  }
  long[2] f = [b, 1], g = [1, 0];
  for (long e = (p + 1) >> 1; e; e >>= 1) {
    if (e & 1) g = mul(g, f);
    f = mul(f, f);
  }
  assert(g[1] == 0);
  return (g[0] < p - g[0]) ? [g[0], p - g[0]] : [p - g[0], g[0]];
}

// Roots of f0 + f1 T + T^2 in F_p[T] with multiplicity
//   p: prime
long[] modRoots2(long f0, long f1, long p)
in {
  assert(2 <= p && p < 1L << 31, "modRoots2: 2 <= p < 2^31 must hold");
  assert(0 <= f0 && f0 < p, "modRoots2: 0 <= f0 < p must hold");
  assert(0 <= f1 && f1 < p, "modRoots2: 0 <= f1 < p must hold");
}
do {
  import std.algorithm : sort;
  if (p == 2) {
    if (f0 == 0 && f1 == 0) return [0, 0];
    if (f0 == 0 && f1 == 1) return [0, 1];
    if (f0 == 1 && f1 == 0) return [1, 1];
    return [];
  } else {
    const f12 = f1.div2(p);
    auto ts = modSqrt(f12 * f12 - f0, p);
    foreach (ref t; ts) {
      if ((t -= f12) < 0) t += p;
    }
    sort(ts);
    switch (ts.length) {
      case 0: return [];
      case 1: return [ts[0], ts[0]];
      case 2: return ts;
      default: assert(false);
    }
  }
}


Int mod(Int)(Int a, Int m) {
  if ((a %= m) < 0) a += m; return a;
}
Int gcd(Int)(Int a, Int b) { return (b != 0) ? gcd(b, a % b) : a; }
Int lcm(Int)(Int a, Int b) { return a / gcd(a, b) * b; }
Int gojo(Int)(Int a, Int b, out Int x, out Int y) {
  if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; }
  x = 1; y = 0; return a;
}
Int modInv(Int)(Int a, Int m) { Int x, y; gojo(a, m, x, y); return mod(x, m); }
long modLogP(long a, long b, long m) {
	a = mod(a, m); b = mod(b, m);
	if (m == 1) return 0;
	long k, al = 1;
	Tuple!(long,long)[] as;
	for (k = 0; k * k < m; ++k) {
		as ~= tuple(al, k);
		al = (al * a) % m;
	}
	as.sort;
	al = modInv(al, m);
	for (long i = 0; i < k; ++i) {
		int pos = as.lowerBound(tuple(b, 0L));
		if (pos < as.length && as[pos][0] == b) return i * k + as[pos][1];
		b = (b * al) % m;
	}
	return -1;
}
long modLog(long a, long b, long m) {
	a = mod(a, m); b = mod(b, m);
	long f, g, r = 1 % m;
	for (f = 0; (g = gcd(a, m)) > 1; ++f) {
		if (b % g != 0) return (r == b) ? f : -1;
		b /= g; m /= g;
		r = (r * (a / g)) % m;
	}
	long res = modLogP(a, b * modInv(r, m) % m, m);
	return (res != -1) ? (f + res) : -1;
}


long P;
long[][] A, B;
long detA;

long[][] inv(long[][] a) {
  const det = mod(a[0][0] * a[1][1] - a[0][1] * a[1][0], P);
  const t = modInv(det, P);
  auto b = [[a[1][1], -a[0][1]], [-a[1][0], a[0][0]]];
  foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
    b[i][j] = mod(t * b[i][j], P);
  }
  return b;
}
long[][] mul(long[][] a, long[][] b) {
  auto c = new long[][](2, 2);
  foreach (i; 0 .. 2) foreach (k; 0 .. 2) foreach (j; 0 .. 2) {
    c[i][j] = mod(c[i][j] + a[i][k] * b[k][j], P);
  }
  return c;
}

long solveBBGS() {
  assert(detA != 0);
  const m = cast(int)(floorKthRoot(P, 2) + 2);
  auto als = new Tuple!(long[][], int)[m];
  long[][] al = [[1, 0], [0, 1]];
  foreach (l; 0 .. m) {
    als[l] = tuple(al, l);
    al = mul(al, A);
  }
  als.sort;
  auto invA = inv(A);
  auto invAm = inv(al);
  long[][] amk = B;
  amk = mul(amk, invA);
  foreach (k; 0 .. m) {
    const pos = als.lowerBound(tuple(amk, 0));
    if (pos < m && als[pos][0] == amk) {
      return 1 + k * cast(long)(m) + als[pos][1];
    }
    amk = mul(amk, invAm);
  }
  return -1;
}

/*
  c^-1 A c = [[r, u], [0, s]]
  
  a00 c00 + a01 c10 = c00 r
  a10 c00 + a11 c10 = c10 r
  
  a00 c01 + a01 c11 = c01 s + c00 u
  a10 c01 + a11 c11 = c11 s + c10 u
*/
long[][] getTr(long[] ts) {
  auto c = new long[][](2, 2);
  // tasukete
  return null;
}

void main() {
  try {
    for (; ; ) {
      P = readLong();
      A = new long[][](2, 2);
      foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
        A[i][j] = readLong();
      }
      B = new long[][](2, 2);
      foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
        B[i][j] = readLong();
      }
      
      detA = mod(A[0][0] * A[1][1] - A[0][1] * A[1][0], P);
      
      // (A[0][0] - T) (A[1][1] - T) - A[0][1] * A[1][0]
      auto ts = modRoots2(detA, mod(-(A[0][0] + A[1][1]), P), P);
      debug {
        writeln("ts = ", ts);
      }
      
      long ans;
      if (ts.length == 2) {
        if (ts[0] == ts[1]) {
          // yabai
          auto c = getTr(ts);
          auto invC = inv(c);
          auto a = mul(mul(invC, A), c);
          auto b = mul(mul(invC, B), c);
          debug {
            writeln("a = ", a);
            writeln("b = ", b);
          }
          import core.stdc.stdlib;
          exit(1);
        } else {
          if (ts[0] == 0 && ts[1] == 0) {
            // zero
            if (B[0][0] == 0 && B[0][1] == 0 && B[1][0] == 0 && B[1][1] == 0) {
              ans = 1;
            } else {
              ans = -1;
            }
          } else if (ts[0] == 0) {
            // ?
            import core.stdc.stdlib;
            exit(1);
          } else {
            ans = solveBBGS();
          }
        }
      } else {
        import core.stdc.stdlib;
        exit(1);
        ans = solveBBGS();
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0