結果

問題 No.2255 Determinant Sum
ユーザー 👑 potato167potato167
提出日時 2023-03-24 21:47:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 3,315 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 213,976 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 20:57:42
合計ジャッジ時間 3,953 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 29 ms
4,348 KB
testcase_07 AC 81 ms
4,348 KB
testcase_08 AC 31 ms
4,348 KB
testcase_09 AC 60 ms
4,348 KB
testcase_10 AC 22 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 12 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 107 ms
4,348 KB
testcase_15 AC 26 ms
4,348 KB
testcase_16 AC 13 ms
4,348 KB
testcase_17 AC 28 ms
4,348 KB
testcase_18 AC 25 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 13 ms
4,348 KB
testcase_21 AC 47 ms
4,348 KB
testcase_22 AC 30 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (ll i=a;i<b;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}

namespace po167{
long long rev(long long a,long long mod){
	a%=mod;
	long long ans=1;
	long long H=mod-2;
	while(H){
		if(H&1) ans=(ans*a)%mod;
		a=(a*a)%mod;
		H>>=1;
	}
	return ans;
}
long long Determinant_Matrix(std::vector<std::vector<long long>> G,long long MOD){
	int N=G.size();
	long long ans=1;
	for(int i=0;i<N;i++){
		for(int j=i;j<N;j++){
			G[j][i]=(G[j][i]%MOD+MOD)%MOD;
			if(G[j][i]!=0){
				if(j!=i){
					for(int k=i;k<N;k++){
						std::swap(G[i][k],G[j][k]);
					}
					ans*=-1;
				}
				break;
			}
		}
		if(G[i][i]==0){
			return 0;
		}
		ans=(ans*G[i][i])%MOD;
		long long R=rev(G[i][i],MOD);
		for(int j=i+1;j<N;j++){
			long long D=(R*G[j][i])%MOD;
			for(int k=i;k<N;k++){
				G[j][k]=(G[j][k]-(D*G[i][k])%MOD+MOD)%MOD;
			}
		}
	}
	return (ans+MOD)%MOD;
}
//行列木定理
long long Kirchhoffs_theorem(std::vector<std::vector<long long>> G,long long MOD){
	int N=G.size();
	std::vector<std::vector<long long>> H(N-1,std::vector<long long>(N-1));
	for(int i=0;i<N-1;i++){
		for(int j=0;j<N;j++){
			if(i==j) continue;
			H[i][i]=(H[i][i]+G[i][j])%MOD;
			if(j!=N-1){
				H[i][j]=(MOD-G[i][j])%MOD;
			}
		}
	}
	return Determinant_Matrix(H,MOD);
}
}
using po167::Determinant_Matrix;
using po167::Kirchhoffs_theorem;


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	cin>>t;
	rep(i,0,t) solve();
}

void solve(){
	int N,P;
	cin>>N>>P;
	vector<vector<ll>> p(N,vector<ll>(N));
	vector<int> X(N),Y(N);
	bool A=0;
	rep(i,0,N) rep(j,0,N){
		cin>>p[i][j];
		if(p[i][j]==-1){
			if(P==2) X[i]++,Y[j]++;
			A=1;
		}
	}
	if(!A) assert(false);
	else if(P!=2) cout<<"0\n";
	else{
		if(vec_max(X)>1||vec_max(Y)>1){
			cout<<"0\n";
			return;
		}
		else{
			rep(i,0,N) rep(j,0,N){
				if(X[i]==1||Y[j]==1){
					if(p[i][j]==-1) p[i][j]=1;
					else p[i][j]=0;
				}
			}
			cout<<Determinant_Matrix(p,P)<<"\n";
		}
	}
}
0