結果
| 問題 |
No.2838 Diagonals
|
| コンテスト | |
| ユーザー |
chro_96
|
| 提出日時 | 2024-08-09 22:27:16 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 11 ms / 2,000 ms |
| コード長 | 1,817 bytes |
| コンパイル時間 | 979 ms |
| コンパイル使用メモリ | 31,744 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-08-09 22:27:19 |
| 合計ジャッジ時間 | 1,059 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 20 |
ソースコード
#include <stdio.h>
typedef struct tree {
struct tree *p;
} tree;
tree *get_root (tree *t) {
if (t == NULL || t->p == NULL) {
return t;
}
t->p = get_root(t->p);
return t->p;
}
long long power_mod (long long a, long long b, long long mod_num) {
long long ans = 1LL;
if (b > 0LL) {
ans = power_mod(a, b/2LL, mod_num);
ans = (ans * ans) % mod_num;
if (b%2LL == 1LL) {
ans = (ans * a) % mod_num;
}
}
return ans;
}
int main () {
int n = 0;
int m = 0;
char s[500][501] = {};
int res = 0;
int ans = 0;
long long mod_num = 998244353LL;
tree t[500][500] = {};
res = scanf("%d", &n);
res = scanf("%d", &m);
for (int i = 0; i < n; i++) {
res = scanf("%s", s[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '#') {
ans++;
if (i <= 0 || s[i-1][j] != '#') {
ans++;
}
if (j <= 0 || s[i][j-1] != '#') {
ans++;
}
}
t[i][j].p = NULL;
}
}
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '#' && s[i+1][j] == '#') {
tree *rt1 = get_root(t[i]+j);
tree *rt2 = get_root(t[i+1]+j);
if (rt1 != rt2) {
rt2->p = rt1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m-1; j++) {
if (s[i][j] == '#' && s[i][j+1] == '#') {
tree *rt1 = get_root(t[i]+j);
tree *rt2 = get_root(t[i]+(j+1));
if (rt1 != rt2) {
rt2->p = rt1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '#' && t[i][j].p == NULL) {
ans--;
}
}
}
printf("%lld\n", power_mod(2LL, (long long)ans, mod_num));
return 0;
}
chro_96