結果

問題 No.1105 Many Triplets
ユーザー ytftytft
提出日時 2021-05-06 23:07:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,879 bytes
コンパイル時間 8,974 ms
コンパイル使用メモリ 381,956 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 15:31:18
合計ジャッジ時間 10,489 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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]<<' ';
    }
    
}
0