結果
| 問題 | No.31 悪のミックスジュース | 
| コンテスト | |
| ユーザー |  tottoripaper | 
| 提出日時 | 2019-01-21 20:43:19 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 68 ms / 5,000 ms | 
| コード長 | 2,348 bytes | 
| コンパイル時間 | 2,054 ms | 
| コンパイル使用メモリ | 176,140 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-09-14 15:55:51 | 
| 合計ジャッジ時間 | 3,045 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 17 | 
ソースコード
#include <bits/stdc++.h>
#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 = std::int64_t;
using P = std::tuple<int,int>;
constexpr ll INF = 1001001001001001001;
template <typename T>
struct Matrix{
    std::vector<std::vector<T>> row;
    
    Matrix() = default;
    Matrix(int n, int m) : row(n, std::vector<T>(m, INF)) {}
    std::vector<T>& operator[](int r){
        return row[r];
    }
    Matrix<T>& operator*=(Matrix<T>& rhs){
        int n = row.size(),
            m = rhs.row.size(),
            l = rhs[0].size();
        Matrix product(n, l);
        for(int i=0;i<n;++i){
            for(int j=0;j<l;++j){
                for(int k=0;k<m;++k){
                    product[i][j] = std::min(product[i][j], row[i][k] + rhs[k][j]);
                }
            }
        }
        *this = std::move(product);
        return *this;
    }
    friend Matrix<T> operator*(Matrix<T> lhs, Matrix<T>& rhs){
        lhs *= rhs;
        return lhs;
    }
    static Matrix<T> identity(int n){
        Matrix<T> I(n, n);
        for(int i=0;i<n;++i){
            I[i][i] = 0;
        }
        return I;
    }
};
template <typename T>
Matrix<T> mat_expt(Matrix<T> a, std::int64_t n){
    Matrix<T> power = std::move(Matrix<T>::identity(a.row.size()));
    
    while(n > 0){
        if(n & 1){power *= a;}
        a *= a;
        n >>= 1;
    }
    return power;
}
int N, V;
ll C[110], D[110];
int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cin >> N >> V;
    for(int i=0;i<N;++i){
        std::cin >> C[i];
    }
    D[0] = C[0];
    for(int i=1;i<N;++i){
        D[i] = C[i] + D[i - 1];
    }
    if(V <= N){
        std::cout << D[N - 1] << std::endl;
        return 0;
    }
    ll minCost = D[N - 1];
    V -= N;
    Matrix<ll> A(N, N), v(N, 1);
    for(int i=0;i+1<N;++i){
        A[i][i+1] = 0;
    }
    for(int i=1;i<N;++i){
        for(int j=0;j<i;++j){
            D[i] = std::min(D[i], D[j] + D[i - j - 1]);
        }
    }
    std::copy(D, D + N, A[N-1].rbegin());
    v[0][0] = 0;
    for(int i=1;i<N;++i){
        v[i][0] = D[i - 1];
    }
    Matrix<ll> w = std::move(mat_expt<ll>(A, V) * v);
    minCost += w[0][0];
    std::cout << minCost << std::endl;
}
            
            
            
        