結果

問題 No.611 Day of the Mountain
ユーザー PachicobuePachicobue
提出日時 2017-12-24 17:02:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 535 ms / 2,017 ms
コード長 5,319 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 217,932 KB
実行使用メモリ 318,848 KB
最終ジャッジ日時 2024-05-09 23:49:48
合計ジャッジ時間 5,636 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 524 ms
318,848 KB
testcase_01 AC 524 ms
318,848 KB
testcase_02 AC 523 ms
318,848 KB
testcase_03 AC 535 ms
318,848 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 201712111LL;
constexpr ld PI = static_cast<ld>(3.1415926535898);

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;

int H, W;
vector<ll>& prev(vector<vector<vector<ll>>>& dp, const int i, const int j)
{
    return (j == 0 ? dp[i - 1][W - 1] : dp[i][j - 1]);
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> H >> W;
    vector<string> A(H);
    for (int i = 0; i < H; i++) {
        cin >> A[i];
    }
    if (H < W) {
        vector<string> A_(W);
        for (int i = 0; i < W; i++) {
            for (int j = 0; j < H; j++) {
                A_[i].push_back(A[j][i]);
            }
        }
        swap(H, W);
        A.clear();
        A = A_;
    }

    vector<vector<ll>> dist(H, vector<ll>(W, 0));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            dist[i][j] = (isdigit(A[i][j]) ? A[i][j] - '0' : 1);
        }
    }
    for (int i = 1; i < W; i++) {
        dist[0][i] += dist[0][i - 1];
    }
    for (int i = 1; i < H; i++) {
        dist[i][0] += dist[i - 1][0];
    }
    for (int i = 1; i < H; i++) {
        for (int j = 1; j < W; j++) {
            dist[i][j] = min(dist[i - 1][j], dist[i][j - 1]) + dist[i][j];
        }
    }
    //        return (j == 0 ? A[i][W - 1] : A[i + 1][j - 1]);
    auto fromLeft = [&](const int i, const int j) {
        assert(j != 1);
        return (j == 0 ? (dist[i][W - 1] == dist[i][W - 2] + (isdigit(A[i][W - 1]) ? A[i][W - 1] - '0' : 1)) : (dist[i + 1][j - 1] == dist[i + 1][j - 2] + (isdigit(A[i + 1][j - 1]) ? A[i + 1][j - 1] - '0' : 1)));
    };
    auto fromAbove = [&](const int i, const int j) {
        return (j == 0 ? (dist[i][W - 1] == dist[i - 1][W - 1] + (isdigit(A[i][W - 1]) ? A[i][W - 1] - '0' : 1)) : (dist[i + 1][j - 1] == dist[i][j - 1] + (isdigit(A[i + 1][j - 1]) ? A[i + 1][j - 1] - '0' : 1)));
    };


    const ll T = dist[H - 1][W - 1];
    cout << T << endl;
    if (W == 1) {
        cout << 1 << endl;
        return 0;
    }

    const int maximum = 1 << W;
    vector<vector<vector<ll>>> dp(H, vector<vector<ll>>(W, vector<ll>(maximum, 0)));  //dist[i][j]から始まるWマスで正しい値の位置がbit表示でmask
    ll num = 1;
    int mask = maximum - 1;
    dp[0][0][mask] = 1;
    for (int j = W - 1; j >= 0; j--) {
        mask -= (1 << j);
        if (A[0][j] == '?') {
            dp[0][0][mask] = (num * 8) % MOD;
            num = (num * 9) % MOD;
        }
    }
    auto tail = [&](const int i, const int j) {
        return (j == 0 ? A[i][W - 1] : A[i + 1][j - 1]);
    };

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (i == 0 and j == 0) {
                continue;
            }
            if (i == H - 1 and j > 0) {
                break;
            }

            if (j == 1) {
                for (int mask = 0; mask < maximum / 2; mask++) {
                    for (int l = 0; l < 2; l++) {
                        const int MASK = mask;
                        const int PREVMASK = mask * 2 + l;
                        const bool above = (bool)l and fromAbove(i, j);
                        dp[i][j][MASK + (1 << (W - 1))] += (not above ? 0 : prev(dp, i, j)[PREVMASK]);
                        dp[i][j][MASK + (1 << (W - 1))] %= MOD;
                        dp[i][j][MASK] += (not above ? prev(dp, i, j)[PREVMASK] * (tail(i, j) == '?' ? 9 : 1) : prev(dp, i, j)[PREVMASK] * (tail(i, j) == '?' ? 8 : 0));
                        dp[i][j][MASK] %= MOD;
                    }
                }
            } else {
                for (int mask = 0; mask < maximum / 4; mask++) {
                    for (int k = 0; k < 2; k++) {
                        for (int l = 0; l < 2; l++) {
                            const int MASK = mask + k * (1 << (W - 2));
                            const int PREVMASK = mask * 2 + k * (1 << (W - 1)) + l;
                            const bool left = (bool)k and fromLeft(i, j);
                            const bool above = (bool)l and fromAbove(i, j);
                            dp[i][j][MASK + (1 << (W - 1))] += (not left and not above ? 0 : prev(dp, i, j)[PREVMASK]);
                            dp[i][j][MASK + (1 << (W - 1))] %= MOD;
                            dp[i][j][MASK] += (not left and not above ? prev(dp, i, j)[PREVMASK] * (tail(i, j) == '?' ? 9 : 1) : prev(dp, i, j)[PREVMASK] * (tail(i, j) == '?' ? 8 : 0));
                            dp[i][j][MASK] %= MOD;
                        }
                    }
                }
            }
        }
    }

    ll ans = 0;
    for (int i = 0; i < maximum; i++) {
        if (i & (1 << (W - 1))) {
            ans += dp[H - 1][0][i];
            ans %= MOD;
        }
    }
    cout << ans << endl;


    return 0;
}
0