結果
問題 | No.261 ぐるぐるぐるぐる!あみだくじ! |
ユーザー | koba-e964 |
提出日時 | 2017-01-19 02:00:36 |
言語 | C++11 (gcc 11.4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,431 bytes |
コンパイル時間 | 570 ms |
コンパイル使用メモリ | 63,192 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-02 06:57:35 |
合計ジャッジ時間 | 1,518 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | WA | - |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
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 | - |
ソースコード
#include <cassert> #include <iostream> #include <list> #include <vector> #define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++) using namespace std; typedef long long int ll; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PI; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll ex_gcd(ll x, ll y, ll &a, ll &b) { if (y == 0) { a = 1; b = 0; return x; } ll q = x / y; ll r = x % y; ll res = ex_gcd(y, r, a, b); ll tmp = a - q * b; a = b; b = tmp; return res; } /* * Calculates the intersection of two arithmetic progressions, * x[n] = a + b * n and y[n] = c + d * n (n >= 0). * p1 = (a, b), p2 = (c, d) */ pair<ll, ll> arith_prog_intersect(const pair<ll, ll> &p1, const pair<ll, ll> &p2) { if (p1.first > p2.first) { return arith_prog_intersect(p2, p1); } ll u, v; ll g = ex_gcd(p1.second, p2.second, u, v); ll lcm = p1.second / g * p2.second; if ((p1.first - p2.first) % g != 0) { return pair<ll, ll>(-1, -1); } ll diff = (p2.first - p1.first) / g; diff *= v % (p1.second / g); diff %= p1.second / g; if (diff < 0) { diff += p1.second / g; } ll x = p2.first + diff * p2.second; return pair<ll, ll>(x, lcm); } int main(void){ int n, k; cin >> n >> k; int a[100] = {}; int dp[100] = {}; REP(i, 0, n) { a[i] = i; dp[i] = -1; } REP(i, 0, k) { int x, y; cin >> x >> y; x--, y--; swap(a[x], a[y]); } REP(i, 0, n) { if (dp[i] > 0) { continue; } int c = 1; int v = a[i]; while (v != i) { v = a[v]; c++; } while (dp[v] < 0) { dp[v] = c; v = a[v]; } } int q; cin >> q; REP(loop_cnt, 0, q) { VI b(n); REP(i, 0, n) { cin >> b[i]; b[i]--; } bool ok = true; pair<ll, ll> coef(0, 1); REP(i, 0, n) { int c = 0; int v = i; int goal = b[i]; while (c <= dp[i]) { if (goal == v) { break; } v = a[v]; c++; } if (c > dp[i]) { ok = false; break; } if (0) { cerr << "y(" << i << ")= " << c << " + " << dp[i] << " * x" << endl; } coef = arith_prog_intersect(coef, pair<ll, ll>(c, dp[i])); if (coef.first < 0) { ok = false; break; } } if (not ok) { cout << -1 << endl; continue; } if (coef.first == 0) { coef.first = coef.second; } cout << coef.first << endl; } }