結果

問題 No.3410 Happiest Art
コンテスト
ユーザー The Forsaking
提出日時 2025-12-25 22:53:53
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 1,403 ms / 4,000 ms
コード長 3,904 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,201 ms
コンパイル使用メモリ 208,276 KB
実行使用メモリ 102,096 KB
最終ジャッジ日時 2025-12-25 22:54:39
合計ジャッジ時間 11,710 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 44
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:41:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   41 |     scanf("%d%d%d%d", &n, &m, &x, &y);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:44:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |         scanf("%d", w + i);
      |         ~~~~~^~~~~~~~~~~~~
main.cpp:46:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |             scanf("%s", s[i][j] + 1);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000010, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m, w[N];


ll qmi(ll a, ll b, ll c) { ll res = 1; while (b) { if (b & 1) res = res * a % c; a = a * a % c; b >>= 1; } return res; }
bool isprime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
    return true;
}

int findPrime(int n) {
    while (!isprime(n)) n++;
    return n;
}

typedef array<ll, 2> Hash;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int P = findPrime(rng() % 900000000 + 100000000);
ll p[N], p2[N];
Hash h[N];
void init() {
    p2[0] = p[0] = 1;
    for (int i = 1; i < N; i++) p[i] = p[i - 1] * 13331 % P, p2[i] = p2[i - 1] * 131 % P;
}

char s[101][102][102], e[10001];
int x, y;
Hash h2;

int main() {
    init();
    scanf("%d%d%d%d", &n, &m, &x, &y);
    map<Hash, int> ma;
    for (int i = 1; i < n + 1; i++) {
        scanf("%d", w + i);
        for (int j = 1; j < x + 1; j++) {
            scanf("%s", s[i][j] + 1);
            for (int k = 1; k < y + 1; k++) h[i] = {(h[i][0] * 13331 + s[i][j][k]) % P, (h[i][1] * 131 + s[i][j][k]) % P};
        }
        ma[h[i]] += i <= m ? 1 : -1;
        if (w[i]) {
            for (int j = 1; j < x + 1; j++)
                for (int k = 1; k < y + 1; k++) {
                    Hash t = {(h[i][0] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p[x * y - (j - 1) * y - k] % P + P) % P, \
                        (h[i][1] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p2[x * y - (j - 1) * y - k] % P + P) % P};
                    ma[t] += i <= m ? 1 : -1;
                }
        }
    }
    int res = x * y > 20 || ma.size() != qmi(2, x * y, MOD) ? 0 : -n;
    Hash t = {-1, -1};
    for (auto [a, b] : ma) res = max(res, b);
    printf("%d\n", res);
    for (auto [a, b] : ma)
        if (b == res)
            t = a;
    if (t[0] != -1) {
        for (int i = 1; i < n + 1; i++) {
            if (h[i] == t) {
                for (int j = 1; j < x + 1; j++) puts(s[i][j] + 1);
                return 0;
            }
            if (w[i]) {
                for (int j = 1; j < x + 1; j++)
                    for (int k = 1; k < y + 1; k++) {
                        Hash g = {(h[i][0] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p[x * y - (j - 1) * y - k] % P + P) % P, \
                            (h[i][1] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p2[x * y - (j - 1) * y - k] % P + P) % P};
                        if (t == g) {
                            for (int l = 1; l < x + 1; l++) {
                                for (int o = 1; o < y + 1; o++) putchar(j == l && k == o ? s[i][l][o] ^ '.' ^ '#' : s[i][l][o]);
                                puts("");
                            }
                            return 0;
                        }
                    }
            }
        }
    } else {
        for (int i = 1; i < x * y + 1; i++) h2 = {(h2[0] * 13331 + (e[i] = '.')) % P, (h2[1] * 131 + (e[i] = '.')) % P};
        while (1) {
            if (!ma.count(h2)) {
                for (int i = 1; i < x + 1; i++) {
                    for (int j = 1; j < y + 1; j++) putchar(e[(i - 1) * y + j]);
                    puts("");
                }
                return 0;
            }
            for (int i = x * y; i; i--) {
                h2 = {(h2[0] - e[i] * p[x * y - i] % P + P) % P, (h2[1] - e[i] * p2[x * y - i] % P + P) % P};
                if (e[i] == '.') {
                    e[i] = '#';
                    h2 = {(h2[0] + e[i] * p[x * y - i] % P + P) % P, (h2[1] + e[i] * p2[x * y - i] % P + P) % P};
                    break;
                } else e[i] = '.';
                h2 = {(h2[0] + e[i] * p[x * y - i] % P + P) % P, (h2[1] + e[i] * p2[x * y - i] % P + P) % P};
            }
        }
    }
    return 0;
}
0