結果

問題 No.74 貯金箱の退屈
ユーザー furafura
提出日時 2020-07-26 01:00:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,918 bytes
コンパイル時間 2,290 ms
コンパイル使用メモリ 207,868 KB
実行使用メモリ 4,512 KB
最終ジャッジ日時 2023-09-10 02:24:32
合計ジャッジ時間 7,764 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class F2{
	bool x;
public:
	F2():x(false){}
	F2(long long n){ assert(n==0||n==1); x=(n==1); }
	F2& operator+=(const F2& a){ x=(x!=a.x); return *this; }
	F2& operator-=(const F2& a){ return (*this)+=a; }
	F2& operator*=(const F2& a){ x=(x&&a.x); return *this; }
	F2& operator/=(const F2& a){ assert(a.x); return *this; }
	F2 operator+(const F2& a)const{ return F2(*this)+=a; }
	F2 operator-(const F2& a)const{ return F2(*this)-=a; }
	F2 operator*(const F2& a)const{ return F2(*this)*=a; }
	F2 operator/(const F2& a)const{ return F2(*this)/=a; }
	bool operator==(const F2& a)const{ return x==a.x; }
	bool operator!=(const F2& a)const{ return !((*this)==a); }
	int to_int()const{ return x; }
};

template<class R>
class matrix{
	vector<vector<R>> a;
public:
	matrix(int n):a(n,vector<R>(n)){}
	matrix(int m,int n):a(m,vector<R>(n)){}

	matrix& operator+=(const matrix& A){
		assert(h()==A.h() && w()==A.w());
		int m=h(),n=w();
		rep(i,m) rep(j,n) (*this)[i][j]+=A[i][j];
		return *this;
	}
	matrix& operator-=(const matrix& A){
		assert(h()==A.h() && w()==A.w());
		int m=h(),n=w();
		rep(i,m) rep(j,n) (*this)[i][j]-=A[i][j];
		return *this;
	}
	matrix& operator*=(const matrix& A){
		assert(w()==A.h());
		int m=h(),n=w(),l=A.w();
		matrix B(m,l);
		rep(i,m) rep(j,l) rep(k,n) B[i][j]+=(*this)[i][k]*A[k][j];
		swap(*this,B);
		return *this;
	}
	matrix operator+(const matrix& A)const{ return matrix(*this)+=A; }
	matrix operator-(const matrix& A)const{ return matrix(*this)-=A; }
	matrix operator*(const matrix& A)const{ return matrix(*this)*=A; }
	const vector<R>& operator[](int i)const{ return a[i]; }
	vector<R>& operator[](int i){ return a[i]; }

	vector<R> operator*(const vector<R>& v)const{
		assert(w()==v.size());
		int m=h(),n=w();
		vector<R> res(m);
		rep(i,m) rep(j,n) res[i]+=(*this)[i][j]*v[j];
		return res;
	}

	int h()const{ return a.size(); }
	int w()const{ return a.empty()?0:a[0].size(); }

	static matrix identity(int n){
		matrix I(n);
		rep(i,n) I[i][i]=R{1};
		return I;
	}
};


int matrix_rank(matrix<F2> A){
	int m=A.h(),n=A.w(),r=0;
	rep(j,n){
		int piv;
		for(piv=r;piv<m;piv++) if(A[piv][j]==1) break;
		if(piv==m) continue;
		if(piv!=r){
			rep(k,n) swap(A[piv][k],A[r][k]);
		}
		for(int i=r+1;i<m;i++) if(A[i][j]==1) {
			for(int k=j;k<n;k++) A[i][k]-=A[r][k];
		}
		r++;
	}
	return r;
}

bool is_solvable(matrix<F2> A,vector<F2> b){
	int h=A.h(),w=A.w();
	assert(h==b.size());
	matrix<F2> A2(h,w+1);
	rep(i,h){
		rep(j,w) A2[i][j]=A[i][j];
		A2[i][w]=b[i];
	}
	return matrix_rank(A)==matrix_rank(A2);
}

int main(){
	int n; scanf("%d",&n);
	vector<int> d(n),w(n);
	rep(i,n) scanf("%d",&d[i]);
	rep(i,n) scanf("%d",&w[i]);

	matrix<F2> A(n);
	rep(i,n){
		A[(i-d[i]+n)%n][i]=1;
		A[(i+d[i])%n][i]=1;
	}
	vector<F2> b(n);
	rep(i,n) b[i]=1-w[i];

	puts(is_solvable(A,b)?"Yes":"No");

	return 0;
}
0