結果

問題 No.426 往復漸化式
ユーザー pekempeypekempey
提出日時 2016-10-05 05:39:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 3,181 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 164,088 KB
実行使用メモリ 42,672 KB
最終ジャッジ日時 2024-11-21 17:08:40
合計ジャッジ時間 4,129 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
42,396 KB
testcase_01 AC 39 ms
42,516 KB
testcase_02 AC 39 ms
42,496 KB
testcase_03 AC 42 ms
42,512 KB
testcase_04 AC 44 ms
42,504 KB
testcase_05 AC 60 ms
42,664 KB
testcase_06 AC 59 ms
42,460 KB
testcase_07 AC 62 ms
42,524 KB
testcase_08 AC 63 ms
42,452 KB
testcase_09 AC 88 ms
42,480 KB
testcase_10 AC 89 ms
42,496 KB
testcase_11 AC 61 ms
42,516 KB
testcase_12 AC 83 ms
42,520 KB
testcase_13 AC 99 ms
42,580 KB
testcase_14 AC 83 ms
42,556 KB
testcase_15 AC 67 ms
42,548 KB
testcase_16 AC 94 ms
42,564 KB
testcase_17 AC 111 ms
42,520 KB
testcase_18 AC 94 ms
42,544 KB
testcase_19 AC 56 ms
42,672 KB
testcase_20 AC 76 ms
42,516 KB
testcase_21 AC 90 ms
42,464 KB
testcase_22 AC 73 ms
42,552 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:117:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  117 |         for (int i = 0; i < 3; i++) scanf("%lld", &va[i][0]);
      |                                     ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:118:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  118 |         for (int i = 0; i < 2; i++) scanf("%lld", &vb[i][0]);
      |                                     ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:131:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  131 |                         scanf("%d", &k);
      |                         ~~~~~^~~~~~~~~~
main.cpp:133:66: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  133 |                                 for (int j = 0; j < 3; j++) scanf("%lld", &seg[k + N].A[i][j]);
      |                                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:138:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  138 |                         scanf("%d", &k);
      |                         ~~~~~^~~~~~~~~~
main.cpp:140:66: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  140 |                                 for (int j = 0; j < 2; j++) scanf("%lld", &seg[k + N].B[i][j]);
      |                                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:145:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  145 |   

ソースコード

diff #

#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");
			fflush(stdout);
		} 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");
			fflush(stdout);
		}
	}
}
0