結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 159 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 83 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 4 ms
5,376 KB
testcase_13 WA -
testcase_14 AC 27 ms
5,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 30 ms
5,376 KB
testcase_19 AC 29 ms
5,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 3 ms
5,376 KB
testcase_24 WA -
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 60 ms
5,376 KB
testcase_27 WA -
testcase_28 AC 51 ms
5,376 KB
testcase_29 AC 16 ms
5,376 KB
testcase_30 AC 71 ms
5,376 KB
testcase_31 AC 30 ms
5,376 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 2 ms
5,376 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 35 ms
5,376 KB
testcase_39 AC 57 ms
5,376 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 18 ms
5,376 KB
testcase_45 AC 8 ms
5,376 KB
testcase_46 AC 16 ms
5,376 KB
testcase_47 AC 20 ms
5,376 KB
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

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<<N*ret+ret*M<<"\n";
	cout<<N<<" "<<ret<<"\n";
	disp(X);
	cout<<ret<<" "<<M<<"\n";
	disp(Y);
}
0