結果

問題 No.572 妖精の演奏
ユーザー tottoripapertottoripaper
提出日時 2017-10-09 20:44:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 2,258 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 166,540 KB
実行使用メモリ 4,436 KB
最終ジャッジ日時 2023-08-10 21:18:10
合計ジャッジ時間 2,659 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

ll N, M;
int A[50][50];
ll dp[40][50][50], _dp[50][50], __dp[50][50];

// merge dp1 and dp2 -> dp3
void merge(const ll (&dp1)[50][50], const ll (&dp2)[50][50], ll (&dp3)[50][50]){
    memset(dp3, 0, sizeof(dp3));
    
    for(int i=0;i<M;++i){
        for(int j=0;j<M;++j){
            for(int k=0;k<M;++k){
                for(int l=0;l<M;++l){
                    dp3[i][j] = max(dp3[i][j], dp1[i][k] + A[k][l] + dp2[l][j]);
                }
            }
        }
    }
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N >> M;

    for(int i=0;i<M;++i){
        for(int j=0;j<M;++j){
            std::cin >> A[i][j];            
        }
    }

    for(int i=0;i<M;++i){
        for(int j=0;j<M;++j){
            if(i == j){continue;}
            dp[0][i][j] = -1001001001001001001ll;
        }
    }

    for(int i=0;i<40;++i){
        if(N >> i & 1){
            if((N & ((1ll << i) - 1)) == 0){
                for(int j=0;j<M;++j){
                    for(int k=0;k<M;++k){
                        _dp[j][k] = dp[i][j][k];
                    }
                }
            }else{
                merge(dp[i], _dp, __dp);
                for(int j=0;j<M;++j){
                    for(int k=0;k<M;++k){
                        _dp[j][k] = __dp[j][k];
                    }
                }
            }
                
            // std::cout << i << std::endl;
            // for(int i=0;i<M;++i){
            //     for(int j=0;j<M;++j){
            //         std::cout << _dp[i][j] << " \n"[j + 1 == M];
            //     }
            // }
            // std::cout << "----------" << std::endl;
        }

        if(i + 1 < 40){
            merge(dp[i], dp[i], dp[i+1]);
        }
    }

    ll res = 0ll;
    for(int i=0;i<M;++i){
        for(int j=0;j<M;++j){
            res = max(res, _dp[i][j]);
        }
    }

    std::cout << res << std::endl;
}
0