結果
| 問題 |
No.3044 よくあるカエルさん
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-02-28 22:51:23 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
AC
|
| 実行時間 | 184 ms / 2,000 ms |
| コード長 | 4,426 bytes |
| コンパイル時間 | 8,322 ms |
| コンパイル使用メモリ | 171,164 KB |
| 実行使用メモリ | 189,868 KB |
| 最終ジャッジ日時 | 2025-02-28 22:51:35 |
| 合計ジャッジ時間 | 11,264 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 20 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (108 ミリ秒)。 main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
#nullable enable
using System.Numerics;
void Run()
{
var n = Int();
var t = Int();
var k = Int() - 1;
var l = Int() - 1;
long i6;
{
var m6 = new Matrix(new int[1, 1]);
m6[0, 0] = 6;
m6 = m6.Power(Matrix.p - 2);
i6 = m6[0, 0];
}
var v = new Matrix(new int[t, 1]);
var a = new Matrix(new int[t, t]);
v[0, 0] = 1;
for (var i = 1; i < t; i++) a[i - 1, i] = 1;
a[0, 0] = i6 * k;
a[1, 0] = i6 * (l - k);
a[t - 1, 0] = i6 * (6 - l);
var mns = a.Power(n - 1) * v;
var ans = mns[0, 0];
Out(ans);
}
#region
AtCoderIO _io_;
var _backend_ = new StandardIOBackend();
_io_ = new(){ Backend = _backend_ };
Run();
_backend_.Flush();
string String() => _io_.Next();
int Int() => int.Parse(String());
void Out(object? x, string? sep = null) => _io_.Out(x, sep);
class AtCoderIO
{
public required StandardIOBackend Backend { get; init; }
Memory<string> _input = Array.Empty<string>();
int _iter = 0;
public string Next()
{
while (_iter >= _input.Length) (_input, _iter) = (Backend.ReadLine().Trim().Split(' '), 0);
return _input.Span[_iter++];
}
public void Out(object? x, string? separator = null)
{
if (x == null) return;
separator ??= Environment.NewLine;
if (x is System.Collections.IEnumerable a and not string)
{
var objects = a.Cast<object>();
if (separator == Environment.NewLine && !objects.Any()) return;
x = string.Join(separator, objects);
}
Backend.WriteLine(x);
}
}
class StandardIOBackend
{
readonly StreamReader _sr = new(Console.OpenStandardInput());
readonly StreamWriter _sw = new(Console.OpenStandardOutput()) { AutoFlush = false };
public string ReadLine() => _sr.ReadLine()!;
public void WriteLine(object? value) => _sw.WriteLine(value);
public void Flush() => _sw.Flush();
}
#endregion
static class Extensions
{
public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();
}
readonly struct Matrix : IAdditionOperators<Matrix, Matrix, Matrix>, IMultiplyOperators<Matrix, Matrix, Matrix>
{
long[,] V { get; init; }
public long this[int i, int j] { get => V[i, j]; set { V[i, j] = Mod(value); } }
public int X => V.GetLength(0);
public int Y => V.GetLength(1);
public const int p = 998244353;
static long Mod(long v)
{
var res = v % p;
if (res < 0) res += p;
return res;
}
public Matrix(int[,] matrix)
{
var (x, y) = (matrix.GetLength(0), matrix.GetLength(1));
var v = new long[x, y];
for (var i = 0; i < x; i++) for (var j = 0; j < y; j++) v[i, j] = Mod(matrix[i, j]);
V = v;
}
public static implicit operator Matrix(int[,] matrix) => new(matrix);
static Matrix With(long[,] matrix) => new(){ V = matrix };
public static Matrix IdentityMatrix(int size)
{
var v = new long[size, size];
for (var i = 0; i < size; i++) v[i, i] = 1;
return With(v);
}
public static Matrix operator +(Matrix a, Matrix b)
{
var (v, va, vb, x, y) = (new long[a.X, a.Y], a.V, b.V, a.X, a.Y);
for (var i = 0; i < x; i++) for (var j = 0; j < y; j++)
{
var sum = va[i, j] + vb[i, j];
if (sum > p) sum -= p;
v[i, j] = sum;
}
return With(v);
}
public static Matrix operator *(Matrix a, Matrix b)
{
var (v, va, vb, x, y, z) = (new long[a.X, b.Y], a.V, b.V, a.X, b.Y, a.Y);
for (var i = 0; i < x; i++) for (var k = 0; k < z; k++)
{
var c = va[i, k];
if (c != 0) for (var j = 0; j < y; j++) v[i, j] += vb[k, j] * c % p;
}
for (var i = 0; i < x; i++) for (var j = 0; j < y; j++) v[i, j] = Mod(v[i, j]);
return With(v);
}
public static Matrix operator *(Matrix a, long k)
{
var (v, va, x, y) = (new long[a.X, a.Y], a.V, a.X, a.Y);
for (var i = 0; i < x; i++) for (var j = 0; j < y; j++) v[i, j] = va[i, j] * k % p;
return With(v);
}
public Matrix Power(long k)
{
var c = IdentityMatrix(X);
var c2 = this;
while (k > 0)
{
if ((k & 1) > 0) c *= c2;
c2 *= c2;
k >>= 1;
}
return c;
}
}