結果
| 問題 |
No.261 ぐるぐるぐるぐる!あみだくじ!
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2017-05-26 14:34:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,598 bytes |
| コンパイル時間 | 2,184 ms |
| コンパイル使用メモリ | 196,272 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-19 20:06:13 |
| 合計ジャッジ時間 | 3,616 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 3 WA * 34 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
typedef long long ll;
struct CRT {
vector<pair<ll, ll>> v;
void add(ll a, ll m) { v.push_back({ a, m }); } // x = a (mod m)
set<ll> enumpr(ll n) { set<ll> V;
for (ll i = 2; i*i <= n; i++) while (n%i == 0) V.insert(i), n /= i;
if (n>1) V.insert(n); return V; }
ll ext_gcd(ll p, ll q, ll& x, ll& y) {
if (q == 0) return x = 1, y = 0, p; ll g = ext_gcd(q, p%q, y, x); y -= p / q*x; return g; }
ll inv(ll p, ll q) { ll xx, yy, g = ext_gcd(p, q, xx, yy); if (xx<0) xx += q, yy -= p; return xx; }
ll solve(ll mo = 1000000007) {
string s; int N = v.size(); set<ll> P; vector<int> num;
rep(i, 0, N) { set<ll> S = enumpr(v[i].second); for(ll r : S) P.insert(r);}
num = vector<int>(N, 0);
for(ll r : P) { int y = 0;
rep(x, 0, N) { num[x] = 1; int j = v[x].second; while (j%r == 0) j /= r, num[x] *= r;
if (num[y]<num[x]) y = x; }
rep(x, 0, N) if (x != y) {
if (v[x].first%num[x] != v[y].first%num[x]) return -1;
v[x].second /= num[x]; v[x].first %= v[x].second; } }
vector<pair<ll, ll> > V2;
rep(x, 0, N) if (v[x].second>1) V2.emplace_back(v[x].second, v[x].first);
sort(V2.begin(), V2.end()); v = V2; N = v.size();
rep(y, 0, N) rep(x, 0, y) { ll k = v[y].second - v[x].second; if (k<0) k += v[y].first;
v[y].second = k*inv(v[x].first, v[y].first) % v[y].first; }
ll ret = 0; for (int x = N - 1; x >= 0; x--) ret = (ret*v[x].first + v[x].second) % mo;
return ret;
}
};
ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N, K, Q;
int E[101];
//---------------------------------------------------------------------------------------------------
#define INF 101010
int D[101][101];
void dfs(int from, int to, int d = 0) {
if (d == 0) {
dfs(E[from], to, 1);
return;
}
if (1010 < d) return;
D[from][to] = min(D[from][to], d);
dfs(E[from], to, d + 1);
}
void pre() {
rep(i, 0, 101) rep(j, 0, 101) D[i][j] = INF;
rep(i, 1, N + 1) dfs(i, i);
//rep(i, 1, N + 1) rep(j, 1, N + 1) printf("D[%d][%d] = %d\n", i, j, D[i][j]);
}
//---------------------------------------------------------------------------------------------------
int A[101];
int solve() {
CRT crt;
bool mm = false;
set<int> aa;
map<int, int> ma;
rep(i, 1, N + 1) {
int a = D[i][A[i]];
int m = D[A[i]][A[i]];
if (a == INF) return -1;
aa.insert(a);
if (m == INF) {
// mに1つでもINFがあれば、aが全部同じじゃないとダメになる
mm = 1;
}
//printf("(%d %d)\n", a % m, m);
if (ma.count(m)) {
if (ma[m] != a % m) return -1;
}
else {
crt.add(a % m, m);
ma[m] = a % m;
}
}
if (mm) {
if (aa.size() == 1) return *aa.begin();
else return -1;
}
int ans = crt.solve();
int a_max = -1;
rep(i, 1, N + 1) {
int a = D[i][A[i]];
a_max = max(a_max, a);
}
if (ans < a_max) {
int lc = 1;
rep(i, 1, N + 1) {
int m = D[A[i]][A[i]];
lc = lcm(lc, m);
}
int d = a_max - ans;
ans += (d + lc - 1) / lc * lc;
}
return ans;
}
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N >> K;
rep(i, 1, N + 1) E[i] = i;
rep(i, 0, K) {
int x, y; cin >> x >> y;
swap(E[x], E[y]);
}
pre();
cin >> Q;
rep(i, 0, Q) {
rep(i, 1, N + 1) cin >> A[i];
cout << solve() << endl;
}
}
はまやんはまやん