結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー koba-e964koba-e964
提出日時 2017-01-18 23:45:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,101 bytes
コンパイル時間 3,007 ms
コンパイル使用メモリ 85,912 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-24 17:10:04
合計ジャッジ時間 2,595 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#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;
const ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
  return b == 0 ? a : gcd(b, a % b);
}

pair<ll, ll> arith_prog_intersect(pair<ll, ll> p1, pair<ll, ll> p2) {
  ll g = p1.second / gcd(p1.second, p2.second) * p2.second;
  ll x = 0;
  for (ll i = 0; i < g / p1.second; ++i) {
    x = p1.first + i * p1.second;
    if (x >= p2.first && (x - p2.first) % p2.second == 0) {
      return pair<ll, ll>(x, g);
    }
  }
  return pair<ll, ll>(-1, -1);
}

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 > n) {
	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;
  }
  
}
0