結果
| 問題 | No.572 妖精の演奏 | 
| コンテスト | |
| ユーザー |  はまやんはまやん | 
| 提出日時 | 2017-10-06 23:57:52 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,104 ms / 2,000 ms | 
| コード長 | 2,608 bytes | 
| コンパイル時間 | 1,855 ms | 
| コンパイル使用メモリ | 168,596 KB | 
| 実行使用メモリ | 10,496 KB | 
| 最終ジャッジ日時 | 2024-11-17 02:18:16 | 
| 合計ジャッジ時間 | 8,464 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 20 | 
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/
typedef long long ll;
ll N; int M;
// dp[len][s][t] := max
ll dp[1010][30][30];
ll A[30][30];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> M;
    rep(y, 0, M) rep(x, 0, M) cin >> A[y][x];
    rep(s, 0, M) rep(t, 0, M) dp[2][s][t] = A[s][t];
    rep(le, 2, 1000) rep(s, 0, M) rep(t, 0, M) {
        rep(to, 0, M) dp[le + 1][s][to] = max(dp[le + 1][s][to], dp[le][s][t] + A[t][to]);
    }
    ll ans = 0;
    if (N <= 100) {
        rep(s, 0, M) rep(t, 0, M) ans = max(ans, dp[N][s][t]);
    } else {
        rep(s, 0, M) rep(c, 0, M) rep(t, 0, M) rep(len1, 1, 32) rep(len2, 3, 32) {
            if (len1 == 1 && s != c) continue;
            ll _d = N - len1 + 1;
            int m = _d % (len2 - 1);
            for (int len3 = len2 - 1 - m; len3 < 32; len3 += len2 - 1) {
                if (len3 == 1 && c != t) continue;
                ll d = N - len1 - len3 + 1;
                ll cc = dp[len1][s][c] + dp[len3][c][t];
                //assert(d % (len2 - 1) == 0);
                cc += dp[len2][c][c] * (d / (len2 - 1));
                if (ans < cc) {
                    ans = cc;
                }
            }
        }
    }
    cout << ans << endl;
    
    return;
    string st = "ABCDEFGHIJKL";
    int ss = 0, tt = 0;
    rep(s, 0, M) rep(t, 0, M) if (ans == dp[N][s][t]) {
        ss = s;
        tt = t;
    }
    string res = "";
    res += st[tt];
    while (N) {
        rep(t, 0, M) if (dp[N][ss][tt] == dp[N - 1][ss][t] + A[t][tt]) {
            res = st[t] + res;
            tt = t;
            N--;
            break;
        }
    }
    cout << res << endl;
}
            
            
            
        