結果
問題 | No.1481 Rotation ABC |
ユーザー |
👑 |
提出日時 | 2021-03-21 11:25:40 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 1,010 bytes |
コンパイル時間 | 300 ms |
コンパイル使用メモリ | 30,592 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-22 02:37:10 |
合計ジャッジ時間 | 1,971 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <stdio.h>const int Mod = 998244353;long long div_mod(long long x, long long y, long long z){if (x % y == 0) return x / y;else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;}int main(){int N;char S[100001];scanf("%d", &N);scanf("%s", S);int i, j, count[3] = {};long long fact[100001];for (i = 0; S[i] != 0; i++) count[S[i] - 'A']++;for (i = 1, fact[0] = 1; i <= N; i++) fact[i] = fact[i-1] * i % Mod;if (count[0] == 0 || count[1] == 0 || count[2] == 0) printf("1\n");else {for (i = 0; S[i] != 'A'; i++);if (i == 0 && S[N-1] == 'A') {for (i = N - 1; S[i] == 'A'; i--);i++;}for (j = i; S[j] == 'A'; j++) if (j == N - 1) j = -1;for (; S[j] == 'C'; j++) if (j == N - 1) j = -1;for (; S[j] == 'B'; j++) if (j == N - 1) j = -1;if (j != i) printf("%lld\n", (div_mod(div_mod(div_mod(fact[N], fact[count[0]], Mod), fact[count[1]], Mod), fact[count[2]], Mod) + Mod - N) %Mod);else printf("1\n");}fflush(stdout);return 0;}