結果
| 問題 |
No.426 往復漸化式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-28 05:29:17 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 1,040 ms / 5,000 ms |
| コード長 | 5,892 bytes |
| コンパイル時間 | 909 ms |
| コンパイル使用メモリ | 124,656 KB |
| 実行使用メモリ | 285,216 KB |
| 最終ジャッジ日時 | 2024-06-22 01:03:55 |
| 合計ジャッジ時間 | 15,276 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 22 |
ソースコード
/*
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) {
}
}