結果

問題 No.74 貯金箱の退屈
ユーザー koyumeishikoyumeishi
提出日時 2015-04-17 04:00:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 2,570 bytes
コンパイル時間 842 ms
コンパイル使用メモリ 81,784 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 21:27:45
合計ジャッジ時間 2,433 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

long long extgcd(long long a, long long b, long long &x, long long &y){
	long long d=a;
	if(b!=0){
		d = extgcd(b, a%b, y, x);
		y -= (a/b) * x;
	}else{
		x = 1;
		y = 0;
	}
	return d;
}

long long mod_inverse(long long a, long long m){
	long long x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}


// A[n*p] * X[p*m] = B[n*m]
template<class T = int>
int gaussian_elimination_with_mod(vector<vector<T>>& A, vector<vector<T>>& B, int n, int p, int m, const T mod){
	int R = max(n,p); int C = p+m;
	vector<vector<T>> V( R, vector<T>(C, 0) );
	for(int i=0; i<n; i++)
		for(int j=0; j<p; j++) V[i][j] = A[i][j];
	for(int i=0; i<n; i++)
		for(int j=0; j<m; j++) V[i][j+p] = B[i][j];


	int rank = 0;
	int row = 0;

	vector<int> left(R, -1);

	//foward
	for(int col=0; col<C && row<R; col++){
		//pivot
		T val = abs( V[row][col] );
		int pivot = row;
		for(int j=row+1; j<R; j++){
			if(val < abs( V[j][col] )){
				val = abs( V[j][col] );
				pivot = j;
			}
		}
		if(pivot != row) swap(V[row], V[pivot]);

		if(val == 0) continue;

		T inv = mod_inverse(val, mod);
		for(int j=row+1; j<R; j++){
			T c = (V[j][col] * inv + mod) % mod;
			for(int k=col; k<p+m; k++) V[j][k] = ((V[j][k] - V[row][k] * c) % mod + mod) % mod;
		}

		left[row] = col;
		row++;
		rank++;
	}

	//backward
	for(int i=R-1; i>=0; i--){
		bool zero = true;
		bool valid = true;
		for(int col=0; col<p; col++) if(V[i][col] != 0) zero = false;
		for(int col = p; zero && col<C; col++) if(V[i][col] != 0) valid = false;

		if(valid == false) return -1;	//no solution

		if(left[i] == -1) continue;

		T inv = mod_inverse(V[i][ left[i] ], mod);
		for(int j=left[i]; j<C; j++) V[i][j] = (V[i][j] * inv) % mod;

		for(int j=i-1; j>=0; j--){
			for(int k=0; k<C; k++){
				V[j][k] = ( (V[j][k] - V[j][i] * V[i][k]) % mod + mod ) % mod;
			}
		}
	}
	//return V;
	return rank;
}

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<vector<int>> A(n, vector<int>(n, 0));
	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][i] = 1;
		A[left][i] = 1;
	}

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

	int rank = gaussian_elimination_with_mod<int>(A,B, n,n,1, 2);

	cout << ((rank == -1)?"No":"Yes") << endl;
	return 0;
}
0