結果

問題 No.1927 AB-CD
ユーザー allegrogikenallegrogiken
提出日時 2022-05-06 23:06:04
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 4,594 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 126,904 KB
実行使用メモリ 9,420 KB
最終ジャッジ日時 2024-06-22 15:07:44
合計ジャッジ時間 3,109 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
8,464 KB
testcase_01 AC 46 ms
8,308 KB
testcase_02 AC 45 ms
7,864 KB
testcase_03 AC 47 ms
8,632 KB
testcase_04 AC 52 ms
9,028 KB
testcase_05 AC 50 ms
9,420 KB
testcase_06 AC 49 ms
8,344 KB
testcase_07 AC 49 ms
8,064 KB
testcase_08 AC 49 ms
8,796 KB
testcase_09 AC 52 ms
8,500 KB
testcase_10 AC 51 ms
8,236 KB
testcase_11 AC 50 ms
8,400 KB
testcase_12 AC 50 ms
9,420 KB
testcase_13 AC 48 ms
8,660 KB
testcase_14 AC 46 ms
8,144 KB
testcase_15 AC 46 ms
8,064 KB
testcase_16 AC 49 ms
8,340 KB
testcase_17 AC 48 ms
8,316 KB
testcase_18 AC 50 ms
9,076 KB
testcase_19 AC 49 ms
8,872 KB
testcase_20 AC 51 ms
8,960 KB
testcase_21 AC 50 ms
8,480 KB
testcase_22 AC 48 ms
8,932 KB
testcase_23 AC 51 ms
8,696 KB
testcase_24 AC 52 ms
8,144 KB
testcase_25 AC 52 ms
8,240 KB
testcase_26 AC 52 ms
8,240 KB
testcase_27 AC 45 ms
8,624 KB
testcase_28 AC 45 ms
8,044 KB
testcase_29 AC 45 ms
8,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main() { runSolver(); }

void problem() {
  auto N = scan!int;
  auto S = scan;
  enum long NMAX = 3 * 10^^5 + 1;
  enum long MOD = 998_244_353;

  auto solve() {
    auto fermet = FermetCalculator!MOD(NMAX);

    long ans = 1;
    long countAB = S.count!(c => c == 'A' || c == 'B');
    long countCD = N - countAB;
    ans = (ans * fermet.factrial[countAB + countCD]) % MOD;
    ans = (ans * fermet.inverse[countAB]) % MOD;
    ans = (ans * fermet.inverse[countCD]) % MOD;
    
    return ans;
  }

  outputForAtCoder(&solve);
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

// -----------------------------------------------

struct FermetCalculator(uint MD) {
  long[] factrial; // 階乗
  long[] inverse;  // 逆元
  
  this(long size) {
    factrial = new long[size + 1];
    inverse = new long[size + 1];
    factrial[0] = 1;
    inverse[0] = 1;
    
    for (long i = 1; i <= size; i++) {
      factrial[i] = (factrial[i - 1] * i) % MD;  // 階乗を求める
      inverse[i] = pow(factrial[i], MD - 2) % MD; // フェルマーの小定理で逆元を求める
    }
  }
  
  long combine(long n, long k) {
    if (n < k) return 1;
    return factrial[n] * inverse[k] % MD * inverse[n - k] % MD;
  }
  
  long pow(long x, long n) { //x^n 計算量O(logn)
    long ans = 1;
    while (n > 0) {
      if ((n & 1) == 1) {
        ans = ans * x % MD;
      }
      x = x * x % MD; //一周する度にx, x^2, x^4, x^8となる
      n >>= 1; //桁をずらす n = n >> 1
    }
    return ans;
  }
}
0