結果

問題 No.572 妖精の演奏
ユーザー PachicobuePachicobue
提出日時 2017-10-07 04:49:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 2,356 bytes
コンパイル時間 2,168 ms
コンパイル使用メモリ 179,852 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 12:14:32
合計ジャッジ時間 3,147 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 7 ms
5,376 KB
testcase_10 AC 34 ms
5,376 KB
testcase_11 AC 34 ms
5,376 KB
testcase_12 AC 36 ms
5,376 KB
testcase_13 AC 41 ms
5,376 KB
testcase_14 AC 32 ms
5,376 KB
testcase_15 AC 29 ms
5,376 KB
testcase_16 AC 31 ms
5,376 KB
testcase_17 AC 32 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

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 = 1e9 + 7;

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

void concat(vector<vector<ll>>& table, const vector<vector<ll>>& f, const vector<vector<ll>>& l, const vector<vector<ll>>& A)
{
    const int m = f.size();
    for (int m1 = 0; m1 < m; m1++) {
        for (int m2 = 0; m2 < m; m2++) {
            for (int m3 = 0; m3 < m; m3++) {
                for (int m4 = 0; m4 < m; m4++) {
                    table[m1][m4] = max(table[m1][m4], f[m1][m2] + l[m3][m4] + A[m2][m3]);
                }
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> A(m, vector<ll>(m, 0));
    for (ll i = 0; i < m; i++) {
        for (ll j = 0; j < m; j++) {
            cin >> A[i][j];
        }
    }
    constexpr int MAX = 35;
    vector<vector<vector<ll>>> dp(MAX, vector<vector<ll>>(m, vector<ll>(m, -INF<ll>)));  //dp[i][m1][m2] = 長さ2^iでm1から始まりm2で終わる譜面のmax
    for (int i = 0; i < m; i++) {
        dp[0][i][i] = 0;
    }
    for (int i = 1; i < MAX; i++) {
        concat(dp[i], dp[i - 1], dp[i - 1], A);
    }

    vector<int> len;
    for (int i = 0; i < MAX; i++) {
        if (n & (1LL << i)) {
            len.push_back(i);
        }
    }
    vector<vector<ll>> table(m, vector<ll>(m));
    for (int i = 0; i < len.size(); i++) {
        if (i == 0) {
            table = dp[len[i]];
        } else {
            vector<vector<ll>> emp(m, vector<ll>(m, 0));
            concat(emp, table, dp[len[i]], A);
            table = emp;
        }
    }
    ll maxi = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
            maxi = max(maxi, table[i][j]);
        }
    }
    cout << maxi << endl;


    return 0;
}
0