結果

問題 No.510 二次漸化式
ユーザー pekempeypekempey
提出日時 2017-04-30 18:00:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 3,000 ms
コード長 2,127 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 75,956 KB
実行使用メモリ 20,440 KB
最終ジャッジ日時 2023-10-11 23:51:29
合計ジャッジ時間 4,491 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 31 ms
4,352 KB
testcase_03 AC 32 ms
4,352 KB
testcase_04 AC 31 ms
4,348 KB
testcase_05 AC 32 ms
4,352 KB
testcase_06 AC 45 ms
5,196 KB
testcase_07 AC 47 ms
5,196 KB
testcase_08 AC 44 ms
5,152 KB
testcase_09 AC 45 ms
5,120 KB
testcase_10 AC 15 ms
4,352 KB
testcase_11 AC 15 ms
4,352 KB
testcase_12 AC 15 ms
4,352 KB
testcase_13 AC 15 ms
4,348 KB
testcase_14 AC 15 ms
4,348 KB
testcase_15 AC 15 ms
4,348 KB
testcase_16 AC 56 ms
20,204 KB
testcase_17 AC 56 ms
20,192 KB
testcase_18 AC 57 ms
20,176 KB
testcase_19 AC 57 ms
20,228 KB
testcase_20 AC 57 ms
20,228 KB
testcase_21 AC 56 ms
20,232 KB
testcase_22 AC 56 ms
20,264 KB
testcase_23 AC 84 ms
20,400 KB
testcase_24 AC 84 ms
20,436 KB
testcase_25 AC 83 ms
20,440 KB
testcase_26 AC 83 ms
20,268 KB
testcase_27 AC 84 ms
20,180 KB
testcase_28 AC 83 ms
20,172 KB
testcase_29 AC 83 ms
20,240 KB
testcase_30 AC 83 ms
20,268 KB
testcase_31 AC 98 ms
20,136 KB
testcase_32 AC 97 ms
20,248 KB
testcase_33 AC 97 ms
20,188 KB
testcase_34 AC 64 ms
20,264 KB
testcase_35 AC 54 ms
20,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int mod = 1e9 + 7;

struct modint {
	int n;
	modint(int n = 0) : n(n) {}
};

modint operator+(modint a, modint b) { return modint((a.n += b.n) >= mod ? a.n - mod : a.n); }
modint operator-(modint a, modint b) { return modint((a.n -= b.n) < 0 ? a.n + mod : a.n); }
modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); }
modint &operator+=(modint &a, modint b) { return a = a + b; }
modint &operator-=(modint &a, modint b) { return a = a - b; }
modint &operator*=(modint &a, modint b) { return a = a * b; }

using V = std::array<modint, 4>;
using M = std::array<V, 4>;

M operator*(const M &a, const M &b) {
	M c = {};
	for (int i = 0; i < 4; i++) {
		for (int k = 0; k < 4; k++) {
			for (int j = 0; j < 4; j++) {
				c[i][j] += a[i][k] * b[k][j];
			}
		}
	}
	return c;
}

M getmat(modint x, modint y) {
	M c = {
		V{1, 0, x, 0},
		V{0, y, 0, 1},
		V{0, 2 * y, y * y, 1},
		V{0, 0, 0, 1},
	};
	return c;
}

int main() {
	int n, q;
	std::cin >> n >> q;

	std::vector<int> xs(n);
	std::vector<int> ys(n);

	int N = 1;
	while (N < n) {
		N *= 2;
	}

	std::vector<M> seg(N * 2);
	for (int i = 0; i < N * 2; i++) {
		seg[i] = getmat(0, 0);
	}
	for (int i = N - 1; i >= 1; i--) {
		seg[i] = seg[i * 2 + 1] * seg[i * 2];
	}

	auto setval = [&](int k, M v) {
		k += N;
		seg[k] = v;
		while (k > 1) {
			k /= 2;
			seg[k] = seg[k * 2 + 1] * seg[k * 2];
		}
	};

	auto getval = [&](int k) {
		if (k == N) {
			return seg[1];
		}
		k += N;
		M ret = {};
		for (int i = 0; i < 4; i++) {
			ret[i][i] = 1;
		}
		while (k > 1) {
			if (k & 1) {
				ret = ret * seg[k - 1];
			}
			k /= 2;
		}
		return ret;
	};

	while (q--) {
		char c;
		scanf(" %c", &c);
		if (c == 'x') {
			int i, v;
			scanf("%d %d", &i, &v);
			xs[i] = v;
			setval(i, getmat(xs[i], ys[i]));
		} else if (c == 'y') {
			int i, v;
			scanf("%d %d", &i, &v);
			ys[i] = v;
			setval(i, getmat(xs[i], ys[i]));
		} else {
			int i;
			scanf("%d", &i);
			auto m = getval(i);
			printf("%d\n", (m[0][0] + m[0][1] + m[0][2] + m[0][3]).n);
		}
	}
}
0