結果

問題 No.1779 Magical Swap
ユーザー 👑 emthrmemthrm
提出日時 2021-12-08 00:04:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 2,132 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 209,316 KB
実行使用メモリ 16,240 KB
最終ジャッジ日時 2023-09-23 07:46:50
合計ジャッジ時間 3,769 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 5 ms
4,384 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 26 ms
6,708 KB
testcase_05 AC 12 ms
4,860 KB
testcase_06 AC 14 ms
4,952 KB
testcase_07 AC 22 ms
6,212 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 20 ms
6,244 KB
testcase_10 AC 94 ms
14,600 KB
testcase_11 AC 42 ms
9,200 KB
testcase_12 AC 14 ms
5,164 KB
testcase_13 AC 67 ms
12,028 KB
testcase_14 AC 105 ms
16,240 KB
testcase_15 AC 52 ms
4,376 KB
testcase_16 AC 29 ms
7,216 KB
testcase_17 AC 49 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct UnionFind {
  UnionFind(const int n) : data(n, -1) {}

  int root(const int ver) {
    return data[ver] < 0 ? ver : data[ver] = root(data[ver]);
  }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) {
      std::swap(u, v);
    }
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool is_same(const int u, const int v) {
    return root(u) == root(v);
  }

  int size(const int ver) {
    return -data[root(ver)];
  }

private:
  std::vector<int> data;
};

void solve() {
  int n; cin >> n;
  vector<int> a(n + 1, 0), b(n + 1, 0);
  FOR(i, 1, n + 1) cin >> a[i];
  FOR(i, 1, n + 1) cin >> b[i];
  UnionFind union_find(n + 1);
  FOR(k, 2, n + 1) {
    for (int i = 2; k * i <= n; ++i) {
      union_find.unite((i - 1) * k, i * k);
    }
  }
  vector<vector<int>> rt(n + 1);
  REP(i, n + 1) rt[union_find.root(i)].emplace_back(i);
  REP(i, n + 1) {
    if (rt[i].empty()) continue;
    multiset<int> a_k, b_k;
    for (int idx : rt[i]) {
      a_k.emplace(a[idx]);
      b_k.emplace(b[idx]);
    }
    if (a_k != b_k) {
      cout << "No\n";
      return;
    }
  }
  cout << "Yes\n";
}

int main() {
  int t; cin >> t;
  while (t--) solve();
  return 0;
}
0