結果

問題 No.426 往復漸化式
ユーザー pekempeypekempey
提出日時 2016-10-05 05:41:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 82 ms / 5,000 ms
コード長 3,197 bytes
コンパイル時間 2,951 ms
コンパイル使用メモリ 194,400 KB
実行使用メモリ 42,692 KB
最終ジャッジ日時 2024-05-01 12:41:35
合計ジャッジ時間 4,974 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
42,452 KB
testcase_01 AC 27 ms
42,624 KB
testcase_02 AC 28 ms
42,452 KB
testcase_03 AC 31 ms
42,360 KB
testcase_04 AC 30 ms
42,568 KB
testcase_05 AC 43 ms
42,496 KB
testcase_06 AC 43 ms
42,624 KB
testcase_07 AC 46 ms
42,448 KB
testcase_08 AC 48 ms
42,632 KB
testcase_09 AC 68 ms
42,620 KB
testcase_10 AC 65 ms
42,488 KB
testcase_11 AC 44 ms
42,624 KB
testcase_12 AC 60 ms
42,452 KB
testcase_13 AC 70 ms
42,692 KB
testcase_14 AC 60 ms
42,624 KB
testcase_15 AC 48 ms
42,496 KB
testcase_16 AC 68 ms
42,656 KB
testcase_17 AC 82 ms
42,564 KB
testcase_18 AC 67 ms
42,568 KB
testcase_19 AC 40 ms
42,496 KB
testcase_20 AC 53 ms
42,496 KB
testcase_21 AC 64 ms
42,624 KB
testcase_22 AC 53 ms
42,564 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:120:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  120 |         for (int i = 0; i < 3; i++) scanf("%lld", &va[i][0]);
      |                                     ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:121:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  121 |         for (int i = 0; i < 2; i++) scanf("%lld", &vb[i][0]);
      |                                     ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:134:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  134 |                         scanf("%d", &k);
      |                         ~~~~~^~~~~~~~~~
main.cpp:136:66: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  136 |                                 for (int j = 0; j < 3; j++) scanf("%lld", &seg[k + N].A[i][j]);
      |                                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:141:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  141 |                         scanf("%d", &k);
      |                         ~~~~~^~~~~~~~~~
main.cpp:143:66: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  143 |                                 for (int j = 0; j < 2; j++) scanf("%lld", &seg[k + N].B[i][j]);
      |                                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:148:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  148 |   

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;

template<int H, int W>
struct Matrix {
	long long a[H][W] = {};

	static Matrix I() {
		Matrix<H, W> res;
		for (int i = 0; i < H; i++) res[i][i] = 1;
		return res;
	}

	Matrix operator+(const Matrix<H, W> &b) const {
		Matrix<H, W> s;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				s.a[i][j] = a[i][j] + b.a[i][j];
				if (s.a[i][j] >= mod) s.a[i][j] -= mod;
			}
		}
		return s;
	}

	template<int N>
	Matrix<H, N> operator*(const Matrix<W, N> &b) const {
		Matrix<H, N> s;
		const long long modl = 8 * mod * mod;
		for (int i = 0; i < H; i++) {
			for (int k = 0; k < W; k++) {
				for (int j = 0; j < N; j++) {
					s[i][j] += a[i][k] * b.a[k][j];
					if (s[i][j] >= modl) s[i][j] -= modl;
				}
			}
			for (int j = 0; j < N; j++) s[i][j] %= mod;
		}
		return s;
	}

	long long *operator[](int i) {
		return a[i];
	}
};

const int N = 1 << 17;

struct Tuple {
	Matrix<3, 3> A;
	Matrix<2, 2> B;
	Matrix<2, 3> S;

	Tuple() {
		A = Matrix<3, 3>::I();
		B = Matrix<2, 2>::I();
	}

	Tuple(Matrix<3, 3> A, Matrix<2, 2> B, Matrix<2, 3> S) : A(A), B(B), S(S) {}

	Tuple operator*(const Tuple &r) const {
		return Tuple(r.A * A, B * r.B, S + B * r.S * A);
	}
};

Tuple seg[N * 2];

void build() {
	for (int k = 0; k < N; k++) {
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 3; j++) {
				seg[k + N].S[i][j] = k * 6 + i * 3 + j;
			}
		}
		for (int i = 0; i < 3; i++) seg[k + N].A[i][i] = 1;
		for (int i = 0; i < 2; i++) seg[k + N].B[i][i] = 1;
	}

	for (int k = N - 1; k > 0; k--) {
		seg[k] = seg[k * 2 + 0] * seg[k * 2 + 1];
	}
}

void update(int k) {
	k += N;
	while (k > 1) {
		k >>= 1;
		seg[k] = seg[k * 2 + 0] * seg[k * 2 + 1];
	}
}

Tuple query(int l, int r) {
	Tuple L, R;
	for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
		if (l & 1) L = L * seg[l++];
		if (r & 1) R = seg[--r] * R;
	}
	return L * R;
}

// I6 A0 A1 | A2 A3 A4 A5
// I5 A0 A1 | A2 A3 A4   
// I4 A0 A1 | A2 A3
// I3 A0 A1 | A2
// -------------------------------
// I2 A0 A1                           
// I1 A0

int main() {
	int n;
	cin >> n;

	Matrix<3, 1> va;
	Matrix<2, 1> vb;

	for (int i = 0; i < 3; i++) scanf("%lld", &va[i][0]);
	for (int i = 0; i < 2; i++) scanf("%lld", &vb[i][0]);

	build();

	int q;
	cin >> q;

	for (int ii = 0; ii < q; ii++) {
		string s;
		cin >> s;

		if (s == "a") {
			int k;
			scanf("%d", &k);
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) scanf("%lld", &seg[k + N].A[i][j]);
			}
			update(k);
		} else if (s == "b") {
			int k;
			scanf("%d", &k);
			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 2; j++) scanf("%lld", &seg[k + N].B[i][j]);
			}
			update(k);
		} else if (s == "ga") {
			int k;
			scanf("%d", &k);

			auto ans = query(0, k).A * va;
			for (int i = 0; i < 3; i++) {
				printf("%lld ", ans[i][0]);
			}
			printf("\n");
		} else if (s == "gb") {
			int k;
			scanf("%d", &k);

			auto X = query(k + 1, n + 1);
			auto Y = query(0, k + 1);
			auto ans = X.B * vb + X.S * Y.A * va;
			for (int i = 0; i < 2; i++) {
				printf("%lld ", ans[i][0]);
			}
			printf("\n");
		}
	}
}
0