結果

問題 No.1533 Don't be Negative!
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-06-04 23:23:50
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 65 ms / 8,000 ms
コード長 12,180 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 156,384 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-22 11:25:08
合計ジャッジ時間 4,656 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 4 ms
6,820 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 21 ms
6,940 KB
testcase_12 AC 17 ms
6,940 KB
testcase_13 AC 8 ms
6,940 KB
testcase_14 AC 14 ms
6,944 KB
testcase_15 AC 18 ms
6,940 KB
testcase_16 AC 33 ms
6,944 KB
testcase_17 AC 8 ms
6,940 KB
testcase_18 AC 26 ms
6,944 KB
testcase_19 AC 12 ms
6,940 KB
testcase_20 AC 26 ms
6,944 KB
testcase_21 AC 17 ms
6,940 KB
testcase_22 AC 24 ms
6,940 KB
testcase_23 AC 16 ms
6,944 KB
testcase_24 AC 25 ms
6,944 KB
testcase_25 AC 25 ms
6,944 KB
testcase_26 AC 9 ms
6,940 KB
testcase_27 AC 22 ms
6,944 KB
testcase_28 AC 32 ms
6,940 KB
testcase_29 AC 33 ms
6,944 KB
testcase_30 AC 59 ms
6,940 KB
testcase_31 AC 5 ms
6,944 KB
testcase_32 AC 15 ms
6,940 KB
testcase_33 AC 39 ms
6,940 KB
testcase_34 AC 3 ms
6,944 KB
testcase_35 AC 16 ms
6,940 KB
testcase_36 AC 38 ms
6,940 KB
testcase_37 AC 14 ms
6,944 KB
testcase_38 AC 47 ms
6,940 KB
testcase_39 AC 38 ms
6,940 KB
testcase_40 AC 49 ms
6,940 KB
testcase_41 AC 34 ms
6,940 KB
testcase_42 AC 42 ms
6,940 KB
testcase_43 AC 38 ms
6,940 KB
testcase_44 AC 33 ms
6,940 KB
testcase_45 AC 44 ms
6,940 KB
testcase_46 AC 46 ms
6,940 KB
testcase_47 AC 48 ms
6,940 KB
testcase_48 AC 48 ms
6,940 KB
testcase_49 AC 43 ms
6,940 KB
testcase_50 AC 65 ms
6,940 KB
testcase_51 AC 57 ms
6,940 KB
testcase_52 AC 43 ms
6,944 KB
testcase_53 AC 54 ms
6,948 KB
testcase_54 AC 62 ms
6,940 KB
testcase_55 AC 61 ms
6,940 KB
testcase_56 AC 52 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;


enum DATA = [

// M = 1, K = 0
[
  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-4, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 1, K = 1
[
  [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 2, K = 0
[
  [0, 99824436, 299473304, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 299473306, -149736654, -249561090, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [399297734, -299473332, 299473278, -9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 2, K = 1
[
  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-1, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-3, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 2, K = 2
[
  [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-1, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-3, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 3, K = 0
[
  [0, -452287368, -180009503, -340239546, -49799024, 451586725, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, -356246365, 288661955, -289344629, 52062632, -410213287, -258804093, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [56913184, 432346183, 379816413, -47679327, -23893675, 213029109, 184860043, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [50445436, -417796192, -486100547, 84253275, -366200244, 49295716, -480636199, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-108004946, 266028116, 449594855, 28781191, -215795466, 52817218, 221832082, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 3, K = 1
[
  [0, 175599997, 384770591, 433050330, 334966453, 99824429, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-429967448, -115860809, -411769635, -205215530, -134824600, -59155216, 73944020, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-379184968, -139955191, -431165726, -178174768, 89225794, -107218836, 295776105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-243241409, -411946274, -474588732, 60386582, 284932316, -155282127, -73943994, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [123538026, 108170837, -204291439, 308963182, -56693167, 147887622, -295776132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 3, K = 2
[
  [0, 421480952, 138645039, -169146948, -399297747, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [8318703, 415935147, 133099245, -410389343, -399297736, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [30501912, -244939583, 203346064, -415935182, -303170543, -406692155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-332747992, -55457453, 258805007, 240318763, 73944260, -184860035, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-83186874, 180238952, 360477489, -457528502, -443664123, 110916042, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 3, K = 3
[
  [0, -332748117, 166374057, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [166374059, -249561088, -166374062, -249561093, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [499122174, -415935155, -415935155, 499122174, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [332748151, -499122114, -83186994, -249561082, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 4, K = 0
[
  [0, -267755483, 369222961, 461808853, -359677444, 380132399, -180968798, 481046694, 20488741, 133099233, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 290332580, -246893316, 37958111, 393499767, -485841401, -409540381, -340251652, 25586736, -378111030, 206667773, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-324024955, -241393270, 354558207, 376577624, -160539074, 130554452, -167057741, -139097408, 404987747, -361863744, -343146530, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-40940071, -405131450, -459578451, -132443626, 163518437, 364671601, -172543843, 484635922, 475529754, 106582812, 452329433, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [353411913, -29904769, -413175266, -214481388, 74430394, 242789619, -423448745, -115641853, -373952564, -291154229, 374341648, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 4, K = 1
[
  [0, -376898106, 386033059, -241867814, -27232042, -421942950, -369595720, -446975280, 39523102, 472152601, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-11441204, -342702539, 45208975, 302133848, 315033095, 117997198, -407444841, -32551269, -399811741, -218561011, -42893321, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [381971063, 171851654, 285983487, -81717264, 493851606, 191937080, 238824083, 273931075, -197703680, 7423730, 358744069, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-81214863, 23816693, -60017311, 13710632, -342613658, -181506205, 487474379, -211030041, -330610701, -369711134, -397737901, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [6865913, -349516168, -141907942, 178076525, 5687034, 128645675, 157679623, 450793957, 49601730, 184645967, -483524755, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [294442099, 25249615, -330215826, 275626860, 338201184, 93239136, -238037241, 484595663, -466323241, -465671324, -183271378, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 4, K = 2
[
  [0, -366639318, -69284393, 76704069, -351437963, -292069379, 171721672, 464109903, 378787623, 442587288, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-454479503, 468184790, 325886306, 426995207, 322844807, 178601177, 200359545, -48057993, -334907033, -381805974, 144277498, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [257131165, 205394570, -233216577, -352099876, 18490531, 33348599, -392168194, 23061008, -387705556, 148303023, -202768398, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [162679376, -239655557, -308470474, 413857592, -172046772, 396928567, -338301251, 92128140, 163096156, 489241758, -350945241, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [61933628, -323283717, 356252651, -490370831, 27068249, 299530580, 79422468, 417972908, 46689557, 152797968, 483524708, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [405093067, -445565878, -204442100, 258856646, -214127619, 78884347, -399191508, 297438962, 251988587, 137130404, 487424054, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 4, K = 3
[
  [0, -128366827, 12207843, 287616860, -474135891, 354736728, 323220409, -366746889, 153239251, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-161771485, 20661539, -106246779, 490570686, -215331626, 9355563, -382596086, -238864380, 444393912, 499122168, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-402346446, 278590078, -245828103, -87202115, 111780264, 296349021, 263727921, -368001366, 18060353, -374341629, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-206317533, 359893443, 106684951, -461551705, 257827755, -140005809, -73727011, 113427861, 227926982, 440631354, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [76591550, 156367248, -457907490, -412532278, 214414497, 276243057, 222157641, 335054072, -487801337, 210567115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [487167407, 17375125, 229152361, -167361463, 2386004, 489011232, 72686873, -231821734, 333551824, 19496956, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
,
// M = 4, K = 4
[
  [0, -291154604, -459839406, -258804106, 76254791, -166374065, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [101673036, -294235604, 443664155, 288073600, 46215023, -160212053, -258804097, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [110916040, 360477130, -358936645, 382044060, 16945405, -12324062, -36972025, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [-406692044, -237236650, 41594264, 146348210, -268046784, -172535986, 369720138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  [305021728, 443671398, 388984226, 15409330, 128633091, -486797971, 147888065, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

];


enum LIM = 10;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      const K = readInt();
      
      Mint r;
      foreach (k; -M .. +M + 1) {
        if (abs(k) != K) {
          r += 1;
        }
      }
      
      auto dp = new Mint[][LIM];
      dp[0] = [Mint(1)];
      foreach (i; 1 .. LIM) {
        dp[i] = new Mint[2 * M * i + 1];
        foreach (j; 0 .. 2 * M * (i - 1) + 1) {
          foreach (k; -M .. +M + 1) {
            if (abs(k) != K) {
              dp[i][j + M + k] += dp[i - 1][j];
            }
          }
        }
      }
      auto as = new Mint[LIM];
      foreach (i; 0 .. LIM) {
        as[i] = dp[i][M * i];
      }
      const data = DATA[M * (M + 1) / 2 - 1 + K];
      const d = cast(int)(data.length) - 1;
      debug {
        writeln("================");
        writefln("N = %s, M = %s, K = %s", N, M, K);
        writeln("as = ", as);
        writeln("data = ", data);
      }
      
      as.length = N + 1;
      foreach (i; LIM .. N + 1) {
        Mint numer;
        foreach (j; 1 .. d + 1) {
          Mint coef;
          foreach_reverse (c; data[j]) {
            (coef *= (i - j)) += c;
          }
          numer += coef * as[i - j];
        }
        Mint denom;
        foreach_reverse (c; data[0]) {
          (denom *= i) += c;
        }
        as[i] = -numer / denom;
      }
      
      auto rr = new Mint[N + 1];
      rr[0] = 1;
      foreach (i; 1 .. N + 1) {
        rr[i] = rr[i - 1] * r;
      }
      
      Mint ans;
      foreach (i; 0 .. N + 1) {
        ans += (N - i + 1) * rr[N - i] * (rr[i] - as[i]) / 2;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0