結果

問題 No.426 往復漸化式
ユーザー pekempeypekempey
提出日時 2016-09-23 03:06:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 111 ms / 5,000 ms
コード長 3,181 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 165,912 KB
実行使用メモリ 42,624 KB
最終ジャッジ日時 2024-04-28 20:32:52
合計ジャッジ時間 4,192 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
42,496 KB
testcase_01 AC 42 ms
42,624 KB
testcase_02 AC 54 ms
42,496 KB
testcase_03 AC 44 ms
42,528 KB
testcase_04 AC 57 ms
42,496 KB
testcase_05 AC 68 ms
42,500 KB
testcase_06 AC 75 ms
42,624 KB
testcase_07 AC 66 ms
42,496 KB
testcase_08 AC 78 ms
42,496 KB
testcase_09 AC 91 ms
42,556 KB
testcase_10 AC 106 ms
42,496 KB
testcase_11 AC 64 ms
42,368 KB
testcase_12 AC 101 ms
42,496 KB
testcase_13 AC 100 ms
42,560 KB
testcase_14 AC 100 ms
42,368 KB
testcase_15 AC 69 ms
42,616 KB
testcase_16 AC 111 ms
42,496 KB
testcase_17 AC 111 ms
42,624 KB
testcase_18 AC 110 ms
42,624 KB
testcase_19 AC 59 ms
42,588 KB
testcase_20 AC 89 ms
42,496 KB
testcase_21 AC 91 ms
42,496 KB
testcase_22 AC 90 ms
42,496 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