結果

問題 No.2255 Determinant Sum
ユーザー gucci0512gucci0512
提出日時 2023-03-25 00:28:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 4,051 bytes
コンパイル時間 4,070 ms
コンパイル使用メモリ 231,724 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 21:49:47
合計ジャッジ時間 5,485 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 126 ms
4,348 KB
testcase_07 AC 47 ms
4,348 KB
testcase_08 AC 23 ms
4,348 KB
testcase_09 AC 33 ms
4,348 KB
testcase_10 AC 30 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 10 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 63 ms
4,348 KB
testcase_15 AC 20 ms
4,348 KB
testcase_16 AC 19 ms
4,348 KB
testcase_17 AC 35 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 11 ms
4,348 KB
testcase_21 AC 18 ms
4,348 KB
testcase_22 AC 10 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
// url 
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=b-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x);
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double lld;
using namespace std;
using namespace atcoder;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const string zton="0123456789";
const string atoz="abcdefghijklmnopqrstuvwxyz";
const string ATOZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll inf=(1ll<<60);
// const int inf=(1<<30);
lld dist(lld x1,lld x2,lld y1,lld y2){
    lld res=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    res=sqrt(abs(res));
    return res;
}
lld arg(lld x,lld y){
    const lld eps=1e-8;
    lld res=0;
    if(abs(x)+abs(y)<=eps)return 0.0;
    else if(abs(x)<=eps){
        if(y>=0.0)return (M_PI/2);
        else return (M_PI/2+M_PI);
    }
    else if(abs(y)<=eps){
        if(x>=0.0)return 0.0;
        else return M_PI;
    }
    res=atan2(abs(y),abs(x));
    if(x<=0&&y>=0)res=(M_PI-res);
    else if(x<=0&&y<=0)res+=(M_PI);
    else if(x>=0&&y<=0)res=(M_PI*2-res);
    return res;
}
ll gcd(ll a,ll b){
    if(a==0||b==0)return a+b;
    ll r;
    r=a%b;
    if(r==0){
        return b;
    }
    else{
        return gcd(b,r);
    }
}

ll solve(){
    int N;cin >> N;
    ll mod;cin >> mod;
    ll sum=mod*(mod-1);
    sum/=2;
    sum%=mod;
    ll A[N][N];
    rep(i,0,N){
        rep(j,0,N){
            cin >> A[i][j];
        }
    }
    ll B[N][N];
    rep(j,0,N){
        int cnt1=0;
        rep(i,0,N){
            if(A[i][j]==-1)cnt1++;
        }
        if(cnt1==0){
            rep(i,0,N){
                B[i][j]=A[i][j];
            }
        }
        else if(cnt1==1){
            rep(i,0,N){
                if(A[i][j]==-1){
                    B[i][j]=sum;
                }
                else B[i][j]=0;
            }
        }
        else{
            return 0;
        }
    }
    // rep(i,0,N){
    //     if(B[i][i]!=0){
    //         continue;
    //     }
    //     int k=0;
    //     while(k<N){
    //         if(B[k][i]!=0)break;
    //         k++;
    //     }
    //     if(k==N){
    //         return 0;
    //     }

    //     rep(j,0,N){
    //         B[i][j]+=B[k][j];
    //         B[i][j]%=mod;
    //     }
    // }
    rep(i,0,N){
        if(B[i][i]==0){
            int k=i+1;
            while(k<N){
                if(B[k][i]!=0)break;
                k++;
            }
            if(k==N){
                return 0;
            }
            rep(j,0,N){
                swap(B[i][j],B[k][j]);
            }
        }
        rep(k,i+1,N){
            if(B[k][i]==0)continue;
            ll c=B[k][i];
            c*=inv_mod(B[i][i],mod);
            c%=mod;
            rep(j,i,N){
                ll tt=c*B[i][j];
                tt%=mod;
                B[k][j]-=tt;
                B[k][j]+=mod;
                B[k][j]%=mod;
            }
        }
    }

    // rep(i,0,N){
    //     rep(j,0,N){
    //         if(i==j){
    //             continue;
    //         }
    //         ll c=B[j][i];
    //         c*=inv_mod(B[i][i],mod);
    //         c%=mod;
    //         rep(k,0,N){
    //             ll tt=c*B[i][k];
    //             tt%=mod;
    //             // cout << i  << " " << j << " " << k << " " << tt << endl;
    //             B[j][k]-=tt;
    //             B[j][k]+=mod;
    //             B[j][k]%=mod;
    //         }
    //     }
    // }
    // rep(i,0,N){
    //     rep(j,0,N){
    //         cout << B[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    ll res=1;
    rep(i,0,N){
        res*=B[i][i];
        res%=mod;
    }
    return res;
}

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<ll> ans;
    int t;cin >> t;
    rep(i,0,t){
        ans.pb(solve());
    }
    rep(i,0,t){
        cout << ans[i] << endl;
    }

}
0