結果

問題 No.1779 Magical Swap
ユーザー chocoruskchocorusk
提出日時 2021-12-10 23:00:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 2,082 bytes
コンパイル時間 3,444 ms
コンパイル使用メモリ 192,336 KB
実行使用メモリ 10,388 KB
最終ジャッジ日時 2023-09-25 21:49:14
合計ジャッジ時間 7,131 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 10 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 72 ms
9,404 KB
testcase_05 AC 31 ms
6,020 KB
testcase_06 AC 36 ms
6,368 KB
testcase_07 AC 63 ms
8,652 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 22 ms
4,932 KB
testcase_10 AC 88 ms
9,716 KB
testcase_11 AC 46 ms
6,784 KB
testcase_12 AC 16 ms
4,376 KB
testcase_13 AC 69 ms
8,480 KB
testcase_14 AC 77 ms
10,308 KB
testcase_15 AC 206 ms
4,376 KB
testcase_16 AC 84 ms
10,388 KB
testcase_17 AC 85 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
using mint=modint998244353;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int main()
{
    int t;cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n+1), b(n+1);
        for(int i=1; i<=n; i++){
            cin>>a[i];
        }
        for(int i=1; i<=n; i++){
            cin>>b[i];
        }
        unionfind uf(n+1);
        for(int k=2; k<=n; k++){
            for(int i=k; i<=n-k; i+=k){
                uf.unite(i, i+k);
            }
        }
        vector<vector<int>> va(n+1), vb(n+1);
        for(int i=1; i<=n; i++){
            int r=uf.find(i);
            va[r].push_back(a[i]);
            vb[r].push_back(b[i]);
        }
        bool ok=1;
        for(int i=1; i<=n; i++){
            if(va[i].empty()) continue;
            sort(va[i].begin(), va[i].end());
            sort(vb[i].begin(), vb[i].end());
            if(va[i]!=vb[i]){
                ok=0;break;
            }
        }
        cout<<(ok?"Yes":"No")<<endl;
    }
    return 0;
}
0