結果

問題 No.426 往復漸化式
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-28 05:29:17
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,277 ms / 5,000 ms
コード長 5,892 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 108,536 KB
実行使用メモリ 285,208 KB
最終ジャッジ日時 2023-09-04 00:55:38
合計ジャッジ時間 18,020 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 35 ms
11,148 KB
testcase_04 AC 32 ms
11,080 KB
testcase_05 AC 263 ms
55,720 KB
testcase_06 AC 264 ms
55,156 KB
testcase_07 AC 818 ms
191,068 KB
testcase_08 AC 807 ms
191,072 KB
testcase_09 AC 1,034 ms
247,172 KB
testcase_10 AC 1,028 ms
247,720 KB
testcase_11 AC 730 ms
181,808 KB
testcase_12 AC 874 ms
225,464 KB
testcase_13 AC 1,108 ms
269,540 KB
testcase_14 AC 1,004 ms
263,480 KB
testcase_15 AC 767 ms
195,088 KB
testcase_16 AC 969 ms
255,316 KB
testcase_17 AC 1,277 ms
285,208 KB
testcase_18 AC 1,068 ms
284,960 KB
testcase_19 AC 755 ms
190,656 KB
testcase_20 AC 936 ms
245,068 KB
testcase_21 AC 1,022 ms
245,216 KB
testcase_22 AC 944 ms
244,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
  a1 = A0 a0
  a2 = A1 A0 a0
  a3 = A2 A1 A0 a0
  a4 = A3 A2 A1 A0 a0
  
  b3 = B4 b4 + K4 A3 A2 A1 A0 a0
  b2 = B3 B4 b4 + B3 K4 A3 A2 A1 A0 a0 + K3 A2 A1 A0 a0
  b1 = B2 B3 B4 b4 + B2 B3 K4 A3 A2 A1 A0 a0 + B2 K3 A2 A1 A0 a0 + K2 A1 A0 a0
  b0 = B1 B2 B3 B4 b4 + B1 B2 B3 K4 A3 A2 A1 A0 a0 + B1 B2 K3 A2 A1 A0 a0 + B1 K2 A1 A0 a0 + K1 A0 a0
     = B1 B2 B3 B4 b4 + B1 B2 (B3 K4 A3 A2 + K3 A2) A1 A0 a0 + (B1 K2 A1 A0 + K1 A0) a0
*/

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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)); }


enum MO = 10L^^9 + 7;

long[][] add(in long[][] A, in long[][] B) {
  assert(A.length == B.length);
  assert(A[0].length == B[0].length);
  const l = A.length, m = A[0].length;
  auto C = new long[][](l, m);
  foreach (i; 0 .. l) foreach (j; 0 .. m) {
    C[i][j] = (A[i][j] + B[i][j]) % MO;
  }
  return C;
}
long[][] mul(in long[][] A, in long[][] B) {
  assert(A[0].length == B.length);
  const l = A.length, m = A[0].length, n = B[0].length;
  auto C = new long[][](l, n);
  foreach (i; 0 .. l) foreach (k; 0 .. m) foreach (j; 0 .. n) {
    (C[i][j] += A[i][k] * B[k][j]) %= MO;
  }
  return C;
}

struct Element {
  long[][] A, B, C;
  Element opBinary(string op)(in Element o) const if (op == "*") {
    return Element(mul(o.A, A), mul(B, o.B), add(C, mul(B, mul(o.C, A))));
  }
}
enum IDENTITY = Element([[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[1, 0], [0, 1]], [[0, 0, 0], [0, 0, 0]]);

class SegmentTree {
  int n;
  Element[] prod;
  this(Element[] ini) {
    const n_ = cast(int)(ini.length);
    for (n = 1; n < n_; n <<= 1) {}
    prod = new Element[n << 1];
    prod[n .. n + n_] = ini[];
    prod[n + n_ .. $] = IDENTITY;
    foreach_reverse (a; 1 .. n) {
      prod[a] = prod[a << 1] * prod[a << 1 | 1];
    }
  }
  void update(int a, Element val) {
    prod[a += n] = val;
    for (; a >>= 1; ) {
      prod[a] = prod[a << 1] * prod[a << 1 | 1];
    }
  }
  Element rangeProd(int a, int b) const {
    Element retL = IDENTITY, retR = IDENTITY;
    for (a += n, b += n; a <= b; a >>= 1, b >>= 1) {
      if ( a & 1) retL = retL * prod[a++];
      if (~b & 1) retR = prod[b--] * retR;
    }
    return retL * retR;
  }
}

int N;
long[][] U, V;
int Q;
string[] T;
int[] I;
long[][][] W;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      U = new long[][](3, 1);
      foreach (i; 0 .. 3) {
        U[i][0] = readLong();
      }
      V = new long[][](2, 1);
      foreach (i; 0 .. 2) {
        V[i][0] = readLong();
      }
      Q = readInt();
      T = new string[Q];
      I = new int[Q];
      W = new long[][][Q];
      foreach (q; 0 .. Q) {
        T[q] = readToken();
        switch (T[q]) {
          case "a": {
            I[q] = readInt();
            W[q] = new long[][](3, 3);
            foreach (i; 0 .. 3) foreach (j; 0 .. 3) {
              W[q][i][j] = readLong();
            }
          } break;
          case "b": {
            I[q] = readInt() - 1;
            W[q] = new long[][](2, 2);
            foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
              W[q][i][j] = readLong();
            }
          } break;
          case "ga": {
            I[q] = readInt();
          } break;
          case "gb": {
            I[q] = readInt();
          } break;
          default: assert(false);
        }
      }
      
      auto A = new long[][][](N, 3, 3);
      auto B = new long[][][](N, 2, 2);
      auto K = new long[][][](N, 2, 3);
      foreach (x; 0 .. N) {
        foreach (i; 0 .. 3) foreach (j; 0 .. 3) {
          A[x][i][j] = (i == j) ? 1 : 0;
        }
        foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
          B[x][i][j] = (i == j) ? 1 : 0;
        }
        foreach (i; 0 .. 2) foreach (j; 0 .. 3) {
          K[x][i][j] = 6 * (x + 1) + i * 3 + j;
        }
      }
      auto ini = new Element[N];
      foreach (x; 0 .. N) {
        ini[x] = Element(A[x], B[x], mul(K[x], A[x]));
      }
      auto seg = new SegmentTree(ini);
      
      foreach (q; 0 .. Q) {
        switch (T[q]) {
          case "a": {
            A[I[q]] = W[q];
            seg.update(I[q], Element(A[I[q]], B[I[q]], mul(K[I[q]], A[I[q]])));
          } break;
          case "b": {
            B[I[q]] = W[q];
            seg.update(I[q], Element(A[I[q]], B[I[q]], mul(K[I[q]], A[I[q]])));
          } break;
          case "ga": {
            const resL = seg.rangeProd(0, I[q] - 1);
            const ans = mul(resL.A, U);
            writeln(ans[0][0], " ", ans[1][0], " ", ans[2][0]);
          } break;
          case "gb": {
            const resL = seg.rangeProd(0, I[q] - 1);
            const resR = seg.rangeProd(I[q], N - 1);
            const ans = add(mul(resR.C, mul(resL.A, U)), mul(resR.B, V));
            writeln(ans[0][0], " ", ans[1][0]);
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0