結果

問題 No.590 Replacement
ユーザー pekempeypekempey
提出日時 2017-11-04 14:16:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 364 ms / 2,000 ms
コード長 3,400 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 116,056 KB
実行使用メモリ 27,588 KB
最終ジャッジ日時 2024-05-02 22:43:16
合計ジャッジ時間 9,708 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 11 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 13 ms
5,376 KB
testcase_10 AC 8 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 13 ms
5,376 KB
testcase_13 AC 108 ms
6,656 KB
testcase_14 AC 120 ms
7,924 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 24 ms
5,376 KB
testcase_17 AC 44 ms
5,376 KB
testcase_18 AC 312 ms
11,888 KB
testcase_19 AC 251 ms
10,628 KB
testcase_20 AC 41 ms
5,376 KB
testcase_21 AC 267 ms
12,256 KB
testcase_22 AC 238 ms
13,796 KB
testcase_23 AC 270 ms
13,264 KB
testcase_24 AC 305 ms
13,056 KB
testcase_25 AC 298 ms
12,692 KB
testcase_26 AC 350 ms
12,500 KB
testcase_27 AC 255 ms
13,200 KB
testcase_28 AC 271 ms
12,036 KB
testcase_29 AC 260 ms
12,096 KB
testcase_30 AC 264 ms
12,056 KB
testcase_31 AC 364 ms
13,060 KB
testcase_32 AC 301 ms
13,036 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 195 ms
14,280 KB
testcase_37 AC 195 ms
14,280 KB
testcase_38 AC 130 ms
14,284 KB
testcase_39 AC 129 ms
14,152 KB
testcase_40 AC 206 ms
18,892 KB
testcase_41 AC 207 ms
18,892 KB
testcase_42 AC 170 ms
14,200 KB
testcase_43 AC 306 ms
13,120 KB
testcase_44 AC 117 ms
27,588 KB
testcase_45 AC 196 ms
14,280 KB
testcase_46 AC 196 ms
14,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>

using namespace std;

const long long mod = 1e9 + 7;

long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}

long long lcm(long long x, long long y) {
  return x / gcd(x, y) * y;
}

vector<vector<int>> cycle(vector<int> a) {
  const int n = a.size();
  vector<vector<int>> ret(n);
  vector<bool> vis(n);
  for (int i = 0; i < n; i++) {
    if (vis[i]) continue;
    for (int k = i; !vis[k]; k = a[k]) {
      ret[i].push_back(k);
      vis[k] = true;
    }
  }
  return ret;
}

long long modinv(long long a, long long m) {
  long long b = m, x = 1, y = 0;
  while (b != 0) {
    long long q = a / b;
    a -= b * q;
    x -= y * q;
    std::swap(a, b);
    std::swap(x, y);
  }
  return x < 0 ? x + m : x;
}

long long crt(long long a1, long long m1, long long a2, long long m2) {
  long long v = (a2 - a1) * modinv(m1, m2) % m2;
  if (v < 0) v += m2;
  return a1 + v * m1;
}

map<long long, int> factors(long long n) {
  map<long long, int> ret;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1) ret[n] = 1;
  return ret;
}

int power(int a, int b) {
  int ret = 1;
  for (int i = 0; i < b; i++) {
    ret *= a;
  }
  return ret;
}

long long crt_g(long long a1, long long m1, long long a2, long long m2) {
  auto f1 = factors(m1);
  auto f2 = factors(m2);
  int M1 = 1;
  int M2 = 1;
  set<int> st;
  for (auto kv : f1) st.insert(kv.first);
  for (auto kv : f2) st.insert(kv.first);
  for (int k : st) {
    if (f1[k] >= f2[k]) {
      M1 *= power(k, f1[k]);
    } else {
      M2 *= power(k, f2[k]);
    }
  }
  return crt(a1 % M1, M1, a2 % M2, M2);
}

int main() {
  int n;
  cin >> n;

  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    b[i]--;
  }
  vector<vector<int>> cycleA = cycle(a);
  vector<vector<int>> cycleB = cycle(b);

  vector<int> rootA(n), rootB(n);
  vector<int> xa(n), xb(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < cycleA[i].size(); j++) {
      xa[cycleA[i][j]] = j;
      rootA[cycleA[i][j]] = i;
    }
    for (int j = 0; j < cycleB[i].size(); j++) {
      xb[cycleB[i][j]] = j;
      rootB[cycleB[i][j]] = i;
    }
  }

  map<pair<int, int>, vector<int>> mp;
  for (int i = 0; i < n; i++) {
    mp[make_pair(rootA[i], rootB[i])].push_back(i);
  }
  long long ans = 0;
  for (auto kv : mp) {
    int A = kv.first.first;
    int B = kv.first.second;
    int lenA = cycleA[A].size();
    int lenB = cycleB[B].size();
    long long G = gcd(lenA, lenB);
    long long L = lcm(lenA, lenB);

    map<int, vector<int>> mp2;
    for (int i : kv.second) {
      int d = (xb[i] - xa[i]) % G;
      if (d < 0) d += G;
      mp2[d].push_back(i);
    }

    for (auto kv2 : mp2) {
      vector<long long> xs;
      for (int i : kv2.second) {
        int x = xa[i];
        int y = xb[i] - kv2.first;
        if (y < 0) y += lenB;
        xs.push_back(crt_g(x, lenA, y, lenB));
      }
      sort(xs.begin(), xs.end());
      xs.push_back(xs[0] + L);
      for (int i = 0; i + 1 < xs.size(); i++) {
        long long d = (xs[i + 1] - xs[i]) % mod;
        ans += d * (d + mod - 1) % mod * ((mod + 1) / 2);
        ans %= mod;
      }
    }
  }

  cout << ans << endl;
}
0