結果

問題 No.2405 Minimal Matrix Decomposition
ユーザー kotatsugamekotatsugame
提出日時 2023-08-04 22:15:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 4,200 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 79,224 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 20:32:51
合計ジャッジ時間 5,554 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 38 ms
5,376 KB
testcase_05 AC 138 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 72 ms
5,376 KB
testcase_11 AC 198 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 30 ms
5,376 KB
testcase_14 AC 23 ms
5,376 KB
testcase_15 AC 12 ms
5,376 KB
testcase_16 AC 9 ms
5,376 KB
testcase_17 AC 69 ms
5,376 KB
testcase_18 AC 27 ms
5,376 KB
testcase_19 AC 24 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 10 ms
5,376 KB
testcase_22 AC 129 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 51 ms
5,376 KB
testcase_27 AC 28 ms
5,376 KB
testcase_28 AC 43 ms
5,376 KB
testcase_29 AC 13 ms
5,376 KB
testcase_30 AC 61 ms
5,376 KB
testcase_31 AC 26 ms
5,376 KB
testcase_32 AC 6 ms
5,376 KB
testcase_33 AC 85 ms
5,376 KB
testcase_34 AC 17 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 28 ms
5,376 KB
testcase_37 AC 51 ms
5,376 KB
testcase_38 AC 31 ms
5,376 KB
testcase_39 AC 50 ms
5,376 KB
testcase_40 AC 38 ms
5,376 KB
testcase_41 AC 5 ms
5,376 KB
testcase_42 AC 116 ms
5,376 KB
testcase_43 AC 25 ms
5,376 KB
testcase_44 AC 15 ms
5,376 KB
testcase_45 AC 6 ms
5,376 KB
testcase_46 AC 15 ms
5,376 KB
testcase_47 AC 17 ms
5,376 KB
testcase_48 AC 10 ms
5,376 KB
testcase_49 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
#include<atcoder/modint>
using namespace std;
#include<vector>
#include<cassert>
template<typename T>
struct Matrix{
	vector<vector<T> >dat;
	int N,M;//N x M matrix
	Matrix(){}
	Matrix(int N_):Matrix(N_,N_){}
	Matrix(int N_,int M_):N(N_),M(M_),dat(N_,vector<T>(M_)){}
	vector<T>&operator[](int i){return dat[i];}
	const vector<T>&operator[](int i)const{return dat[i];}
	static Matrix eye(int N)
	{
		Matrix res(N);
		for(int i=0;i<N;i++)res[i][i]=1;
		return res;
	}
	Matrix operator+(const Matrix&A)const
	{
		assert(N==A.N&&M==A.M);
		Matrix res(N,M);
		for(int i=0;i<N;i++)for(int j=0;j<M;j++)
			res[i][j]=dat[i][j]+A[i][j];
		return res;
	}
	Matrix operator-(const Matrix&A)const
	{
		assert(N==A.N&&M==A.M);
		Matrix res(N,M);
		for(int i=0;i<N;i++)for(int j=0;j<M;j++)
			res[i][j]=dat[i][j]-A[i][j];
		return res;
	}
	Matrix operator*(const Matrix&A)const
	{
		assert(M==A.N);
		Matrix res(N,A.M);
		for(int i=0;i<N;i++)for(int k=0;k<M;k++)for(int j=0;j<A.M;j++)
			res[i][j]+=dat[i][k]*A[k][j];
		return res;
	}
	Matrix pow(long long n)const
	{
		assert(N==M);
		Matrix a=*this,res=eye(N);
		for(;n;a=a*a,n>>=1)if(n&1)res=res*a;
		return res;
	}
	template<typename U>
	Matrix operator+(const U&A)const
	{
		Matrix res(N,M);
		for(int i=0;i<N;i++)for(int j=0;j<M;j++)
			res[i][j]=dat[i][j]+A;
		return res;
	}
	template<typename U>
	Matrix operator-(const U&A)const
	{
		Matrix res(N,M);
		for(int i=0;i<N;i++)for(int j=0;j<M;j++)
			res[i][j]=dat[i][j]-A;
		return res;
	}
	template<typename U>
	Matrix operator*(const U&A)const
	{
		Matrix res(N,M);
		for(int i=0;i<N;i++)for(int j=0;j<M;j++)
			res[i][j]=dat[i][j]*A;
		return res;
	}
	T det()const
	{
		assert(N==M);
		Matrix A=*this;
		T ret=1;
		for(int j=0;j<N;j++)
		{
			int id=-1;
			for(int i=j;i<N;i++)
			{
				if(A[i][j]!=0)
				{
					id=i;
					break;
				}
			}
			if(id==-1)return T(0);
			if(id!=j)
			{
				A[j].swap(A[id]);
				ret=-ret;
			}
			ret*=A[j][j];
			{
				const T a=1/A[j][j];
				for(int k=j+1;k<N;k++)A[j][k]*=a;
			}
			for(int i=j+1;i<N;i++)
			{
				const T a=A[i][j];
				for(int k=j+1;k<N;k++)A[i][k]-=A[j][k]*a;
			}
		}
		return ret;
	}
	int elimination()
	{
		int ret=0;
		for(int j=0;j<M;j++)
		{
			int id=-1;
			for(int i=ret;i<N;i++)
			{
				if(dat[i][j]!=0)
				{
					id=i;
					break;
				}
			}
			if(id==-1)continue;
			if(id!=ret)dat[ret].swap(dat[id]);
			{
				const T a=1/dat[ret][j];
				for(int k=j;k<M;k++)dat[ret][k]*=a;
			}
			for(int i=0;i<N;i++)
			{
				if(i==ret)continue;
				const T a=dat[i][j];
				for(int k=j;k<M;k++)dat[i][k]-=dat[ret][k]*a;
			}
			ret++;
		}
		return ret;
	}
};
using mint=atcoder::modint;
using mat=Matrix<mint>;
void disp(const mat&A)
{
	for(int i=0;i<A.N;i++)
	{
		for(int j=0;j<A.M;j++)
		{
			cout<<A[i][j].val()<<(j+1==A.M?"\n":" ");
		}
	}
}
int main()
{
	{
		int P;cin>>P;
		mint::set_mod(P);
	}
	int N,M;
	cin>>N>>M;
	mat A(N,M);
	for(int i=0;i<N;i++)for(int j=0;j<M;j++)
	{
		int a;cin>>a;
		A[i][j]=mint::raw(a);
	}
	mat B=A;
	int rk=B.elimination();
	if(N*M<=N*rk+rk*M)
	{
		cout<<"1\n";
		cout<<N<<" "<<M<<"\n";
		disp(A);
		return 0;
	}
	else if(rk==0)
	{
		if(N*M<=N+M)
		{
			cout<<"1\n";
			cout<<N<<" "<<M<<"\n";
			disp(A);
		}
		else
		{
			cout<<"2\n";
			cout<<N<<" 1\n";
			disp(mat(N,1));
			cout<<"1 "<<M<<"\n";
			disp(mat(1,M));
		}
		return 0;
	}
	mat E=mat::eye(N);
	int ret=0;
	for(int j=0;j<M;j++)
	{
		int id=-1;
		for(int i=ret;i<N;i++)
		{
			if(A[i][j]!=0)
			{
				id=i;
				break;
			}
		}
		if(id==-1)continue;
		if(id!=ret)
		{
			A[ret].swap(A[id]);
			for(int i=0;i<N;i++)swap(E[i][ret],E[i][id]);
		}
		{
			const mint b=A[ret][j];
			for(int i=0;i<N;i++)E[i][ret]*=b;
			const mint a=1/A[ret][j];
			for(int k=j;k<M;k++)A[ret][k]*=a;
		}
		for(int i=0;i<N;i++)
		{
			if(i==ret)continue;
			const mint a=A[i][j];
			for(int k=j;k<M;k++)A[i][k]-=A[ret][k]*a;
			for(int k=0;k<N;k++)E[k][ret]+=E[k][i]*a;
		}
		ret++;
	}
	mat X(N,ret),Y(ret,M);
	for(int i=0;i<N;i++)for(int j=0;j<ret;j++)X[i][j]=E[i][j];
	for(int i=0;i<ret;i++)for(int j=0;j<M;j++)Y[i][j]=A[i][j];
	cout<<"2\n";
	cout<<N<<" "<<ret<<"\n";
	disp(X);
	cout<<ret<<" "<<M<<"\n";
	disp(Y);
}
0