結果

問題 No.426 往復漸化式
ユーザー koba-e964koba-e964
提出日時 2016-10-13 08:28:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,349 ms / 5,000 ms
コード長 3,126 bytes
コンパイル時間 1,371 ms
コンパイル使用メモリ 91,184 KB
実行使用メモリ 181,496 KB
最終ジャッジ日時 2023-08-25 02:58:20
合計ジャッジ時間 22,539 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 338 ms
132,368 KB
testcase_01 AC 344 ms
132,616 KB
testcase_02 AC 345 ms
132,468 KB
testcase_03 AC 394 ms
132,936 KB
testcase_04 AC 395 ms
132,980 KB
testcase_05 AC 635 ms
136,856 KB
testcase_06 AC 640 ms
136,820 KB
testcase_07 AC 755 ms
181,332 KB
testcase_08 AC 741 ms
181,364 KB
testcase_09 AC 1,055 ms
181,236 KB
testcase_10 AC 1,056 ms
181,304 KB
testcase_11 AC 734 ms
181,268 KB
testcase_12 AC 1,064 ms
181,324 KB
testcase_13 AC 1,174 ms
181,412 KB
testcase_14 AC 1,055 ms
181,496 KB
testcase_15 AC 858 ms
181,224 KB
testcase_16 AC 1,296 ms
181,268 KB
testcase_17 AC 1,349 ms
181,392 KB
testcase_18 AC 1,290 ms
181,332 KB
testcase_19 AC 634 ms
181,408 KB
testcase_20 AC 822 ms
181,412 KB
testcase_21 AC 992 ms
181,408 KB
testcase_22 AC 822 ms
181,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<ll> VL;
const ll mod = 1e9 + 7;

typedef vector<VL> VVL;
VVL add(const VVL &a, const VVL &b) {
  int n = a.size();
  int m = a[0].size();
  VVL ret(n, VL(m, 0));
  REP(i, 0, n) {
    REP(j, 0, m) {
      ret[i][j] = (a[i][j] + b[i][j]) % mod;
    }
  }
  return ret;
}
VVL mul(const VVL &a, const VVL &b) {
  int n = a.size();
  int m = b.size();
  int l = b[0].size();
  VVL ret(n, VL(l, 0));
  REP(i, 0, n) {
    REP(j, 0, m) {
      REP(k, 0, l) {
	ret[i][k] += a[i][j] * b[j][k];
	ret[i][k] %= mod;
      }
    }
  }
  return ret;
}
struct dat{VVL a, b, s;};
dat elem_mul(const dat &abs1, const dat &abs2) {
  VVL a = mul(abs2.a, abs1.a);
  VVL b = mul(abs1.b, abs2.b);
  VVL s = add(abs1.s, mul(mul(abs1.b, abs2.s), abs1.a));
  return dat{a, b, s};
}

const int N = 1 << 17;
dat ary[2*N];
dat st_e;
void init(dat e){
  for (int i = 0; i < 2 * N - 1; i++) {
    ary[i] = e;
  }
  st_e = e;
}
void update(int k, dat v) {
  k += N - 1;
  ary[k] = v;
  while (k > 0) {
    k = (k - 1) / 2;
    ary[k] = elem_mul(ary[2 * k + 1], ary[2 * k + 2]);
  }
}
void update_all(const dat *vals, int len) {
  for (int k = 0; k < std::min(N, len); ++k) {
    ary[k + N - 1] = vals[k];
  }
  for (int k = std::min(N, len); k < N; ++k) {
    ary[k + N - 1] = st_e;
  }
  for (int b = N / 2; b >= 1; b /= 2) {
    for (int k = 0; k < b; ++k) {
      ary[k + b - 1] = elem_mul(ary[k * 2 + b * 2 - 1], ary[k * 2 + b * 2]);
    }
  }
}
dat querySub(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) return st_e;
  if (a <= l && r <= b) return ary[k];
  dat vl = querySub(a, b, 2 * k + 1, l, (l + r) / 2);
  dat vr = querySub(a, b, 2 * k + 2, (l + r) / 2, r);
  return elem_mul(vl, vr);
}
dat query(int a, int b) {
  return querySub(a, b + 1, 0, 0, N);
}
int main(void){
  int n, q;
  cin >> n;
  VVL a(3, {0}), b(2, {0});
  REP(i, 0, 3) {
    cin >> a[i][0];
  }
  REP(i, 0, 2) {
    cin >> b[i][0];
  }
  VVL u3(3), u2(2);
  REP(i, 0, 3) {
    VL t(3);
    t[i] = 1;
    u3[i] = t;
  }
  REP(i, 0, 2) {
    VL t(2);
    t[i] = 1;
    u2[i] = t;
  }
  
  init({u3, u2, VVL(2, VL(3))});
  vector<dat> ary(n + 1);
  REP(i, 0, n + 1) {
    VVL s(2, VL(3, 0));
    REP(k,0,6)s[k/3][k%3]=6*i+k;
    ary[i] = {u3, u2, s};
  }
  update_all(&ary[0], n + 1);
  cin >> q;
  REP(z, 0, q) {
    string qty;
    int i;
    cin >> qty >> i;
    dat cur = query(i, i);
    if (qty == "a") {
      REP(k, 0, 3) {
	REP(j, 0, 3) {
	  cin >> cur.a[k][j];
	}
      }
      update(i, cur);
    }
    if (qty == "b") {
      REP(k, 0, 2) {
	REP(j, 0, 2) {
	  cin >> cur.b[k][j];
	}
      }
      update(i, cur);
    }
    if (qty == "ga") {
      VVL r = mul(query(0, i - 1).a, a);
      cout << r[0][0] << " " << r[1][0] << " " << r[2][0] << "\n";
    }
    if (qty == "gb") {
      dat tmp = query(i + 1, n);
      VVL r = add(mul(tmp.b, b), mul(tmp.s, mul(query(0, i).a, a)));
      cout << r[0][0] << " " << r[1][0] << "\n";
    }
  }
}
0