結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー minamiminami
提出日時 2019-04-04 00:37:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,529 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 175,928 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 03:37:20
合計ジャッジ時間 3,569 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

// 拡張ユークリッドの互除法
// 一次不定方程式 ax + by = gcd(a, b) を満たす x, y を求める
// ax + by = k * gcd(a, b) は、まず ax + by = gcd(a, b) を解き、解を k 倍する
// 戻り値: gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
	long long g = a; x = 1; y = 0;
	if (b != 0) {
		g = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	return g;
}

// 中国剰余定理
pair<long long, long long> crt(const vector<long long> &a, const vector<long long> &n) {
	assert(a.size() == n.size());
	long long A = 0, N = 1;
	for (int i = 0; i < a.size(); i++) {
		long long u, v;
		long long g = extgcd(N, n[i], u, v);
		if (a[i] % g != A % g)
			return make_pair(-1, -1);
		long long m = n[i] / g;
		long long t = (a[i] - A) / g * u % m;
		if (t < 0)t += m;
		A += N * t;
		N *= m;
	}
	return make_pair(A % N, N);
}

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x / gcd(x, y) * y; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K; cin >> N >> K;
	vector<int> X(K), Y(K); rep(i, 0, K) {
		cin >> X[i] >> Y[i];
		X[i]--, Y[i]--;
	}
	vector<int> v(N); iota(all(v), 0);
	rep(i, 0, K)
		swap(v[X[i]], v[Y[i]]);
	int Q; cin >> Q;
	vector<vector<int>> A(Q, vector<int>(N));
	rep(i, 0, Q) rep(j, 0, N) { cin >> A[i][j]; A[i][j]--; }
	vector<int> g(N);
	vector<vector<int>> r(N, vector<int>(N, -1));
	int l = 1;
	rep(i, 0, N) {
		int c = 1;
		r[i][i] = 0;
		for (int s = v[i]; s != i; s = v[s]) {
			r[i][s] = c;
			c++;
		}
		g[i] = c;
		l = lcm(l, c);
	}
	//dump(r);
	rep(q, 0, Q) {
		vector<long long> a, n;
		rep(i, 0, N) {
			a.push_back(r[i][A[q][i]]);
			n.push_back(g[i]);
		}
		if(count(all(a),-1))
			cout << -1 << endl;
		else {
			auto res = crt(a, n);
			dump(res);
			if(res.first == -1)
				cout << -1 << endl;
			else if(res.first == 0)
				cout << l << endl;
			else
				cout << res.first << endl;
		}
	}
	return 0;
}
0