結果
| 問題 | No.1105 Many Triplets | 
| コンテスト | |
| ユーザー |  ytft | 
| 提出日時 | 2021-05-06 23:07:11 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 2,879 bytes | 
| コンパイル時間 | 5,107 ms | 
| コンパイル使用メモリ | 356,924 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-09-14 14:17:08 | 
| 合計ジャッジ時間 | 6,210 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 25 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
namespace mp=boost::multiprecision;
template<typename type>
class field{
    
    int MOD=1000000007;
public:
    function<type(type,type)> add=[this](int a,int b){
        long long A=a,B=b;
        return (A+B)%MOD;
    };
    function<type(type,type)> product=[this](int a,int b){
        long long A=a,B=b;
        return (A*B)%MOD;
    };
    function<bool(type)> in=[this](type a){
        int comp;
        return typeid(comp)==typeid(a) && 0<=a && a<MOD;
    };//引数が体の要素か否か
    function<type(type)> add_inverse=[this](int a){
        return (-a+MOD)%MOD;
    };
    function<type(type)> product_inverse=[this](int a){
        return pow(a,MOD-2);
    };
    type one=1,zero=0;
    field(){
    }
};
template<typename type>
class matrix{
public:
    vector<vector<type>> value;
    field<type> f;
    matrix(vector<vector<type>> value,field<type> f){
        this->value=value;
        this->f=f;
    }
    matrix<type> operator + (matrix a){
        vector<vector<type>> ret(value.size(),vector<type>(a.value[0].size()));
        type temp;
        for(int i=value.size()-1;i>=0;--i){
            for(int j=value[0].size()-1;j>=0;--j){
                ret[i][j]=f.add(value[i][j],a.value[i][j]);
            }
        }
        return matrix(ret,f);
    }
    matrix<type> operator * (matrix a){
        vector<vector<type>> ret(value.size(),vector<type>(a.value[0].size()));
        type temp;
        for(int i=value.size()-1;i>=0;i--){
            for(int j=a.value[0].size()-1;j>=0;j--){
                temp=f.zero;
                for(int k=a.value.size()-1;k>=0;k--){
                    temp=f.add(temp,f.product(value[i][k],a.value[k][j]));
                }
                ret[i][j]=temp;
            }
        }
        return matrix(ret,f);
    }
    matrix<type> pow(long long p){
        matrix temp(value,f);
        matrix ans=temp;
        bool flg=false;
        while(p>0){
            if(p%2){
                if(flg){
                    ans=ans*temp;
                }else{
                    flg=true;
                    ans=temp;
                }
            }
            p=p/2;
            temp=temp*temp;
        }
        return ans;
    }
};
int main(){
    long long N;
    cin>>N;
    field<int> f;
    vector<vector<int>> a(3,vector<int>(3,0));
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if(i==j){
                a[i][j]=1;
            }
            if((j-i+2)%3==0){
                a[i][j]=1000000006;
            }
        }
    }
    vector<vector<int>> b(3,vector<int>(1,0));
    for(int i=0;i<3;i++){
        cin>>b[i][0];
    }
    matrix<int> m(a,f);
    matrix<int> init(b,f);
    matrix<int> ans=m.pow(N-1)*init;
    for(int i=0;i<3;i++){
        cout<<ans.value[i][0]<<' ';
    }
    
}
            
            
            
        