結果
| 問題 |
No.1516 simple 門松列 problem Re:MASTER
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-05-21 21:30:41 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 6,000 ms |
| コード長 | 5,553 bytes |
| コンパイル時間 | 1,759 ms |
| コンパイル使用メモリ | 156,340 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-22 11:18:34 |
| 合計ジャッジ時間 | 2,712 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
struct ModInt(int M_) {
import std.conv : to;
alias M = M_;
int x;
this(ModInt a) { x = a.x; }
this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
ref ModInt opAssign(long a) { return (this = ModInt(a)); }
ref ModInt opOpAssign(string op)(ModInt a) {
static if (op == "+") { x += a.x; if (x >= M) x -= M; }
else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
else static if (op == "/") { this *= a.inv(); }
else static assert(false);
return this;
}
ref ModInt opOpAssign(string op)(long a) {
static if (op == "^^") {
if (a < 0) return (this = inv()^^(-a));
ModInt t2 = this, te = ModInt(1);
for (long e = a; e > 0; e >>= 1) {
if (e & 1) te *= t2;
t2 *= t2;
}
x = cast(int)(te.x);
return this;
} else return mixin("this " ~ op ~ "= ModInt(a)");
}
ModInt inv() const {
int a = x, b = M, y = 1, z = 0, t;
for (; ; ) {
t = a / b; a -= t * b;
if (a == 0) {
assert(b == 1 || b == -1);
return ModInt(b * z);
}
y -= t * z;
t = b / a; b -= t * a;
if (b == 0) {
assert(a == 1 || a == -1);
return ModInt(a * y);
}
z -= t * y;
}
}
ModInt opUnary(string op: "-")() const { return ModInt(-x); }
ModInt opBinary(string op, T)(T a) const {
return mixin("ModInt(this) " ~ op ~ "= a");
}
ModInt opBinaryRight(string op)(long a) const {
return mixin("ModInt(a) " ~ op ~ "= this");
}
bool opCast(T: bool)() const { return (x != 0); }
string toString() const { return x.to!string; }
}
enum MO = 998244353;
alias Mint = ModInt!MO;
// Berlekamp-Massey
// F: field
// \sum_{j=1}^0 cs[j] as[i - j] = 0 (d <= i < |as|), cs[0] = 1
// M must be prime
F[] findLinearRecurrence(F)(F[] as) {
import std.algorithm : min;
const n = cast(int)(as.length);
int d, m;
auto cs = new F[n + 1], bs = new F[n + 1];
cs[0] = bs[0] = 1;
F invBef = 1;
foreach (i; 0 .. n) {
++m;
F dif = as[i];
foreach (j; 1 .. d + 1) dif += cs[j] * as[i - j];
if (dif.x != 0) {
auto csDup = cs.dup;
const r = dif * invBef;
foreach (j; m .. n) cs[j] -= r * bs[j - m];
if (2 * d <= i) {
d = i + 1 - d;
m = 0;
bs = csDup;
invBef = dif.inv;
}
}
}
return cs[0 .. d + 1];
}
enum LIM = 1000;
Mint solve(Mint[] as, Mint[] cs, long N) {
const d = cast(int)(cs.length) - 1;
Mint[] mul(Mint[] fs, Mint[] gs) {
auto hs = new Mint[d + d - 1];
foreach (i; 0 .. d) foreach (j; 0 .. d) {
hs[i + j] += fs[i] * gs[j];
}
foreach_reverse (i; d .. d + d - 1) {
foreach (j; 1 .. d + 1) {
hs[i - j] -= cs[j] * hs[i];
}
}
hs.length = d;
return hs;
}
auto xs = new Mint[d];
auto ys = new Mint[d];
xs[1] = 1;
ys[0] = 1;
for (long e = N; e; e >>= 1) {
if (e & 1) ys = mul(ys, xs);
xs = mul(xs, xs);
}
Mint ans;
foreach (i; 0 .. d) {
ans += as[i] * ys[i];
}
return ans;
}
void main() {
try {
for (; ; ) {
const N = readLong();
const K = readInt();
auto dp0 = new Mint[][][](LIM, K, K);
auto dp1 = new Mint[][][](LIM, K, K);
foreach (a; 0 .. K) foreach (b; 0 .. K) {
dp0[2][a][b] = 1;
dp1[2][a][b] = a + b;
}
foreach (n; 3 .. LIM) {
foreach (a; 0 .. K) foreach (b; 0 .. K) foreach (c; 0 .. K) {
if (a != c && ((a < b && b > c) || (a > b && b < c))) {
dp0[n][b][c] += dp0[n - 1][a][b];
dp1[n][b][c] += dp1[n - 1][a][b] + dp0[n - 1][a][b] * c;
}
}
}
auto as0 = new Mint[LIM];
auto as1 = new Mint[LIM];
foreach (n; 2 .. LIM) {
foreach (a; 0 .. K) foreach (b; 0 .. K) {
as0[n] += dp0[n][a][b];
as1[n] += dp1[n][a][b];
}
}
auto cs0 = findLinearRecurrence(as0);
auto cs1 = findLinearRecurrence(as1);
debug {
writeln("K = ", K);
writeln("|cs0| = ", cs0.length);
writeln("|cs1| = ", cs1.length);
writeln("cs0 = ", cs0);
writeln("cs1 = ", cs1);
}
const ans0 = solve(as0, cs0, N);
const ans1 = solve(as1, cs1, N);
writeln(ans0, " ", ans1);
}
} catch (EOFException e) {
}
}