結果
| 問題 |
No.510 二次漸化式
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-04-30 18:41:56 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 3,000 ms |
| コード長 | 2,122 bytes |
| コンパイル時間 | 760 ms |
| コンパイル使用メモリ | 75,276 KB |
| 実行使用メモリ | 20,440 KB |
| 最終ジャッジ日時 | 2024-09-13 23:54:30 |
| 合計ジャッジ時間 | 2,236 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 34 |
ソースコード
#include <iostream>
#include <string>
#include <algorithm>
#include <array>
#include <vector>
const int mod = 1e9 + 7;
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
char readChar() {
int c;
while ((c = getchar()) < 'a');
return c;
}
int readInt() {
int n, c;
while ((c = getchar()) < '0');
n = c - '0';
while ((c = getchar()) >= '0') {
n = n * 10 + c - '0';
}
return n;
}
void writeInt(int n) {
int d[11];
int i = 0;
do {
d[i++] = n % 10;
n /= 10;
} while (n > 0);
while (i > 0) {
putchar(d[--i] + '0');
}
putchar('\n');
}
// stupid...
struct foo {
long long a, b, c, d, e, f, g, h;
};
foo operator*(const foo &x, const foo &y) {
foo ret;
ret.a = (y.a + x.a * y.d + x.b * y.f) % mod;
ret.b = (y.b + x.b * y.g) % mod;
ret.c = (x.c + y.c + x.a * y.e + x.b * y.h) % mod;
ret.d = x.d * y.d % mod;
ret.e = (x.e + x.d * y.e) % mod;
ret.f = (y.d * x.f + y.f * x.g) % mod;
ret.g = x.g * y.g % mod;
ret.h = (y.e * x.f + x.h + x.g * y.h) % mod;
return ret;
}
foo getmat(long long x, long long y) {
foo c;
c.a = 0;
c.b = x;
c.c = 0;
c.d = y;
c.e = 1;
c.f = 2 * y % mod;
c.g = y * y % mod;
c.h = 1;
return c;
}
int main() {
int n = readInt();
int q = readInt();
std::vector<int> xs(n);
std::vector<int> ys(n);
int N = 1;
while (N < n) {
N *= 2;
}
std::vector<foo> seg(N * 2, getmat(0, 0));
auto setval = [&](int k, foo 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;
foo ret = {};
ret.d = 1;
ret.g = 1;
while (k > 1) {
if (k & 1) {
ret = ret * seg[k - 1];
}
k /= 2;
}
return ret;
};
while (q--) {
char c = readChar();
if (c == 'x') {
int i = readInt();
int v = readInt();
xs[i] = v;
setval(i, getmat(xs[i], ys[i]));
} else if (c == 'y') {
int i = readInt();
int v = readInt();
ys[i] = v;
setval(i, getmat(xs[i], ys[i]));
} else {
int i = readInt();
auto m = getval(i);
writeInt((m.a + m.b + m.c + 1) % mod);
}
}
}