結果

問題 No.3370 AB → BA
コンテスト
ユーザー msksknkn
提出日時 2025-11-22 17:18:14
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,209 bytes
コンパイル時間 2,630 ms
コンパイル使用メモリ 80,876 KB
実行使用メモリ 64,576 KB
最終ジャッジ日時 2025-11-22 17:18:26
合計ジャッジ時間 11,060 ms
ジャッジサーバーID
(参考情報)
judge7 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package no3370_AB_to_BA;
import java.util.*;
public class Main {
	public static long MOD = 998244353;
	public static long[] fac;
	public static long[] div;
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		ArrayList<Integer> a = new ArrayList<>();
		String s = sc.next();
		int n = s.length();
		a.add(-1);
		for(int i = 0;i < n;i++) {
			if(s.charAt(i) == 'A')a.add(i);
		}fac = new long[n + 1];
		div = new long[n + 1];
		fac[0] = 1;div[0] = 1;
		for(int i = 1;i <= n;i++) {
			fac[i] = fac[i - 1] * (long)i;
			div[i] = pow(fac[i],MOD - 2);
		}long ans = 1;
		int one = 0;
		for(int i = 1;i < a.size();i++) {
			one++;
			long add = ans * (long)(a.get(i) - a.get(i - 1)) % MOD;
			//System.out.println(add);
			add += c(a.get(i - 1) + 1,one);
			ans = add;
			ans %= MOD;
		}System.out.print(ans);
	}public static long c(int a,int b) {
		if(a < b) {
			return 0;
		}return fac[a] * div[b] % MOD * div[a - b] % MOD;
	}
	public static long pow(long a,long b) {
		long ret = 1;
		long val = a;
		while(b > 0) {
			if((b & 1) != 0) {
				ret = ret * val % MOD;
			}val = val * val % MOD;
			b >>= 1;
		}return ret;
	}

}
0