結果

問題 No.1175 Simultaneous Equations
ユーザー ytftytft
提出日時 2021-05-11 14:35:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 5,399 bytes
コンパイル時間 7,840 ms
コンパイル使用メモリ 374,316 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 18:30:34
合計ジャッジ時間 8,706 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 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,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 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{
public:
    function<type(type,type)> add=[](double A,double B){
        return (A+B);
    };
    function<type(type,type)> product=[](double A,double B){
        return (A*B);
    };
    function<bool(type)> in=[this](type a){
        int comp;
        return true;
    };//引数が体の要素か否か
    function<type(type)> add_inverse=[this](type a){
        return -a;
    };
    function<type(type)> product_inverse=[this](type a){
        return 1.0/a;
    };
    double one=1.0,zero=0;
    field(string s="real",int param=1000000007){
        if(s=="residue"){
            int MOD=param;
            add=[MOD](int a,int b){
                long long A=a,B=b;
                return (A+B)%MOD;
            };
            product=[MOD](int a,int b){
                long long A=a,B=b;
                return (A*B)%MOD;
            };
            in=[MOD](type a){
                int comp;
                return typeid(comp)==typeid(a) && 0<=a && a<MOD;
            };//引数が体の要素か否か
            add_inverse=[MOD](int a){
                return (-a+MOD)%MOD;
            };
            product_inverse=[MOD](int a){
                long long temp=a;
                long long ans=1;
                int p=MOD-2;
                while(p>0){
                    if(p%2){
                        ans=(ans*temp)%MOD;
                    }
                    temp=(temp*temp)%MOD;
                    p/=2;
                }
                int l=ans;
                return l;
            };
            one=1,zero=0;
        }else if(s=="real"){
        }
    }
};

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;
    }
    matrix<type> reduct(){//行簡約化
        vector<vector<type>> ret=value;
        int N=ret.size(),M=ret[0].size();
        int determined=0;//定められた行の個数
        for(int i=0;i<M;++i){
            for(int j=determined;j<N;++j){
                if(ret[j][i]!=f.zero){
                    for(int k=0;k<M;++k){
                        swap(ret[determined][k],ret[j][k]);
                    }
                    type a=f.product_inverse(ret[determined][i]);
                    for(int k=0;k<M;++k){
                        ret[determined][k]=f.product(ret[determined][k],a);
                    }
                    
                    for(int k=0;k<N;++k){
                        if(k==determined){
                            continue;
                        }
                        type c=f.add_inverse(ret[k][i]);
                        for(int l=i;l<M;++l){
                            ret[k][l]=f.add(ret[k][l],f.product(ret[determined][l],c));
                        }
                    }
                    ++determined;
                    break;
                }
            }
        }
        matrix<type> R(ret,f);
        return R;
    }
    matrix<type> inv(){
        int N=value.size();
        int M=value[0].size();
        vector<vector<type>> memo(N,vector<type>(M*2));
        vector<vector<type>> ret(N,vector<type>(M));
        for(int i=0;i<N;++i){
            for(int j=0;j<M;++j){
                memo[i][j]=value[i][j];
            }
            for(int j=M;j<M*2;++j){
                memo[i][j]=(j-M==i?1:0);
            }
        }
        memo=matrix<type>(memo,f).reduct().value;
        for(int i=0;i<N;++i){
            for(int j=0;j<M;++j){
                ret[i][j]=memo[i][j+M];
            }
        }
        matrix<type> R(ret,f);
        return R;
    }
};

int main(){
    double a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    field<double> W("real");
    vector<vector<double>> v={{a,b},{d,e}};
    vector<vector<double>> r={{c},{f}};
    matrix<double> awt(v,W);
    matrix<double> bawta(r,W);
    matrix<double> cela=awt.inv()*bawta;
    cout<<cela.value[0][0]<<' '<<cela.value[1][0]<<endl;
}
0