結果
| 問題 |
No.426 往復漸化式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-23 02:19:43 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 767 ms / 5,000 ms |
| コード長 | 3,744 bytes |
| コンパイル時間 | 1,965 ms |
| コンパイル使用メモリ | 175,260 KB |
| 実行使用メモリ | 132,816 KB |
| 最終ジャッジ日時 | 2024-11-17 15:55:43 |
| 合計ジャッジ時間 | 16,317 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:135:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
135 | scanf("%d", &tmp);
| ~~~~~^~~~~~~~~~~~
main.cpp:140:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
140 | scanf("%d", &tmp);
| ~~~~~^~~~~~~~~~~~
main.cpp:155:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
155 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
main.cpp:160:46: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
160 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
main.cpp:167:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
167 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
main.cpp:172:46: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
172 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
main.cpp:179:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
179 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
main.cpp:189:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
189 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
struct Matrix {
vector<vector<long long>> a;
Matrix() {}
Matrix(int h, int w) : a(h, vector<long long>(w)) {}
Matrix(vector<vector<long long>> a) : a(a) {}
static Matrix I(int n) {
Matrix res(n, n);
for (int i = 0; i < n; i++) res[i][i] = 1;
return res;
}
Matrix operator+(const Matrix &b) const {
Matrix s(a.size(), a[0].size());
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); 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;
}
Matrix operator*(const Matrix &b) const {
Matrix s(a.size(), b.a[0].size());
const long long modl = 8 * mod * mod;
for (int i = 0; i < a.size(); i++) {
for (int k = 0; k < a[0].size(); k++) {
for (int j = 0; j < b.a[0].size(); 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 < b.a[0].size(); j++) s[i][j] %= mod;
}
return s;
}
vector<long long> &operator[](int i) {
return a[i];
}
void show() {
cerr << "show" << endl;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
cerr << a[i][j] << " ";
}
cerr << endl;
}
cerr << endl;
}
};
const int N = 1 << 17;
struct Tuple {
Matrix A, B, S;
Tuple() : S(2, 3) {
A = Matrix::I(3);
B = Matrix::I(2);
}
Tuple(Matrix A, Matrix B, Matrix 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);
}
};
Matrix va(3, 1), vb(2, 1);
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, int type, Matrix mat) {
k += N;
if (type == 0) {
seg[k].A = mat;
} else {
seg[k].B = mat;
}
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;
for (int i = 0; i < 3; i++) {
int tmp;
scanf("%d", &tmp);
va[i][0] = tmp;
}
for (int i = 0; i < 2; i++) {
int tmp;
scanf("%d", &tmp);
vb[i][0] = tmp;
}
build();
int q;
cin >> q;
for (int ii = 0; ii < q; ii++) {
string s;
cin >> s;
if (s == "a") {
int k;
scanf("%d", &k);
Matrix mat(3, 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int x;
scanf("%d", &x);
mat[i][j] = x;
}
}
update(k, 0, mat);
} else if (s == "b") {
int k;
scanf("%d", &k);
Matrix mat(2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int x;
scanf("%d", &x);
mat[i][j] = x;
}
}
update(k, 1, mat);
} 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);
}
}
}