結果

問題 No.74 貯金箱の退屈
ユーザー koyumeishikoyumeishi
提出日時 2015-04-24 12:07:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,437 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 70,056 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-18 08:52:07
合計ジャッジ時間 2,483 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <vector>
#include <bitset>
using namespace std;

// A[n*p] * X[p*m] = B[n*m]
template<class T = long long>
vector<T> gaussian_elimination_binary(vector<T> A, vector<T> B, int n, int p, int m){
	vector<T> M(n);
	for(int i=0; i<n; i++){
		M[i] = A[i] << m;
		M[i] |= B[i];
	}

	int rank = 0;
	vector<int> left_most(n,-1);
	for(int row = 0, col = p+m-1; row<n && col>=m; col--){
		if( ((M[row] >> col) & T(1)) == T(0)){
			int pivot = row;
			for(int row__ = row; row__<n; row__++){
				if( ((M[row__] >> col) & T(1)) != T(0) ){
					pivot = row__;
					break;
				}
			}

			if(pivot == row) continue;
			swap(M[row], M[pivot]);
		}

		rank++;
		left_most[row] = col;

		for(int row__=row+1; row__<n; row__++){
			if( ((M[row__] >> col) & T(1)) != T(0) ) M[row__] ^= M[row];
		}

		row++;
	}

	// T = long long 
	//T mask_B = ((T)1<<m) - 1;
	//T mask_A = (((T)1<<(m+p)) - 1) ^ mask_B;

	// T = bitset
	T mask_B( string(m, '1') );
	T mask_A( string(m+p, '1') );
	mask_A ^= mask_B;

	bool has_solution = true;
	for(int row=n-1; row>=0; row--){
		if( (M[row] & mask_B) != T(0) && (M[row] & mask_A) == T(0) ) has_solution = false;
		if(left_most[row] == -1) continue;
		int col = left_most[row];
		for(int row__ = row-1; row__>=0; row__--){
			if( ((M[row__] >> col) & T(1)) != T(0) ) M[row__] ^= M[row];
		}
	}

	return has_solution? M : vector<T>(0);
}

template<class T = long long>
int get_rank(vector<T> A, int n, int m){
	int rank = 0;
	for(int row=0, col=0; col<m && row<n; col++){
		if( ((A[row] >> col)&T(1)) == T(0) ){
			int pivot = row;
			for(int row__ = row; row__<n; row__++){
				if( ((A[row__] >> col) & T(1)) != T(0) ){
					pivot = row__;
					break;
				}
			}

			if(pivot == row) continue;
			swap(A[row], A[pivot]);
		}

		rank++;

		for(int row__=row+1; row__<n; row__++){
			if( ((A[row__] >> col) & T(1)) != T(0) ) A[row__] ^= A[row];
		}
		row++;
	}
	return rank;
}


#include <iostream>

int main(){

	int n;
	cin >> n;
	vector<int> d(n), w(n);
	for(int i=0; i<n; i++) cin >> d[i];
	for(int i=0; i<n; i++) cin >> w[i];

	vector< bitset<200> > A(n);
	for(int i=0; i<n; i++){
		d[i] %= n;
		int right = (i + d[i]) % n;
		int left = (i - d[i] + n) % n;
		A[right].set(i, 1);
		A[left].set(i, 1);
	}

	vector< bitset<200> > B(n);
	for(int i=0; i<n; i++){
		B[i].set(0, 1^w[i]);
	}

	auto v = gaussian_elimination_binary< bitset<200> >(A,B, n,n,1);

	cout << ((v.size() == 0)?"No":"Yes") << endl;

	return 0;
}
0