結果

問題 No.1927 AB-CD
ユーザー tentententen
提出日時 2022-07-28 14:37:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,725 bytes
コンパイル時間 4,729 ms
コンパイル使用メモリ 73,436 KB
実行使用メモリ 54,664 KB
最終ジャッジ日時 2023-09-25 06:34:05
合計ジャッジ時間 7,823 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,312 KB
testcase_01 AC 41 ms
49,284 KB
testcase_02 AC 40 ms
49,480 KB
testcase_03 AC 41 ms
49,380 KB
testcase_04 AC 117 ms
52,516 KB
testcase_05 AC 114 ms
52,640 KB
testcase_06 AC 117 ms
52,628 KB
testcase_07 AC 112 ms
52,700 KB
testcase_08 AC 50 ms
49,396 KB
testcase_09 AC 108 ms
52,340 KB
testcase_10 AC 108 ms
52,416 KB
testcase_11 AC 110 ms
52,360 KB
testcase_12 AC 79 ms
52,192 KB
testcase_13 AC 98 ms
51,884 KB
testcase_14 AC 46 ms
49,340 KB
testcase_15 AC 57 ms
49,664 KB
testcase_16 AC 115 ms
52,636 KB
testcase_17 AC 95 ms
52,404 KB
testcase_18 AC 100 ms
52,908 KB
testcase_19 AC 109 ms
52,336 KB
testcase_20 AC 125 ms
52,512 KB
testcase_21 AC 110 ms
52,416 KB
testcase_22 AC 79 ms
52,128 KB
testcase_23 AC 123 ms
54,500 KB
testcase_24 AC 128 ms
54,588 KB
testcase_25 AC 130 ms
54,664 KB
testcase_26 AC 137 ms
54,408 KB
testcase_27 AC 45 ms
49,200 KB
testcase_28 AC 42 ms
49,372 KB
testcase_29 AC 42 ms
49,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int ab = 0;
        for (char c : sc.next().toCharArray()) {
            if (c == 'A' || c == 'B') {
                ab++;
            }
        }
        System.out.println(getComb(n, ab));
    }
    
    static long getComb(long n, long r) {
        if (n - r < r) {
            return getComb(n, n - r);
        }
        long ans = 1;
        long div = 1;
        for (int i = 1; i <= r; i++) {
            ans *= n - i + 1;
            ans %= MOD;
            div *= i;
            div %= MOD;
        }
        return ans * pow(div, MOD - 2) % MOD;
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0