結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー pekempeypekempey
提出日時 2015-11-12 21:43:54
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,517 bytes
コンパイル時間 2,186 ms
コンパイル使用メモリ 158,332 KB
実行使用メモリ 4,536 KB
最終ジャッジ日時 2023-10-11 15:44:41
合計ジャッジ時間 3,358 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

template<class T>
ostream &operator <<(ostream &os, const vector<T> &v) {
	rep (i, v.size()) {
		if (i) os << " ";
		os << v[i];
	}
	return os;
}

ll modulo(ll a, ll mod) {
	a %= mod; a += mod; a %= mod;
	return a;
}

ll modpow(ll a, ll b, ll mod) {
	ll res = 1;
	while (b) {
		if (b & 1) (res *= a) %= mod;
		(a *= a) %= mod;
		b /= 2;
	}
	return res;
}

ll modinv(ll a, ll mod) {
	return modpow(a, mod - 2, mod);
}

ll garner(vector<pair<ll, ll>> eq, ll mod) {
	eq.emplace_back(0, mod);
	int n = eq.size();
	vector<ll> coef(n, 1), sum(n, 0);
	rep (i, n - 1) {
		ll v = (eq[i].first - sum[i]) * modinv(coef[i], eq[i].second) % eq[i].second;
		if (v < 0) v += eq[i].second;
		rep (j, i + 1, n) {
			(sum[j] += coef[j] * v) %= eq[j].second;
			(coef[j] *= eq[i].second) %= eq[j].second;
		}
	}
	return sum.back();
}

int main() {
	int N, K;
	cin >> N >> K;
	vector<int> s(N);
	rep (i, N) s[i] = i;
	rep (i, K) {
		int X, Y;
		cin >> X >> Y;
		X--; Y--;
		swap(s[X], s[Y]);
	}

	vector<int> id(N, -1), index(N, -1);
	vector<vector<int>> orbit(N);
	rep (i, N) if (id[i] == -1) {
		int curr = i;
		int j = 0;
		while (id[curr] == -1) {
			id[curr] = i;
			index[curr] = j;
			orbit[i].push_back(curr);
			curr = s[curr];
			j++;
		}
	}

	int Q;
	cin >> Q;
	rep (i_, Q) {
		vector<int> A(N);
		rep (i, N) {
			cin >> A[i];
			A[i]--;
		}
		bool bad = false;
		vector<int> dist(N, -1);
		rep (i, N) {
			if (id[i] != id[A[i]]) bad = true;
			else {
				int d = modulo(index[A[i]] - index[i], orbit[id[i]].size());
				if (dist[id[i]] == -1) {
					dist[id[i]] = modulo(d, orbit[id[i]].size());
				} else {
					if (dist[id[i]] != d) bad = true;
				}
			}
		}
		if (bad) {
			cout << -1 << endl;
			continue;
		}

		vector<pair<ll, ll>> eq;
		ll prod = 1;
		rep (i, N) if (dist[i] != -1) {
			eq.emplace_back(dist[i], orbit[id[i]].size());
			prod *= orbit[id[i]].size();
		}
		const ll mod = 1e18;
		ll ans = garner(eq, mod);
		if (ans == 0) ans += prod;
		cout << ans << endl;
	}
	return 0;
}
0