結果

問題 No.590 Replacement
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-11-03 23:40:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,475 bytes
コンパイル時間 2,573 ms
コンパイル使用メモリ 192,736 KB
実行使用メモリ 21,256 KB
最終ジャッジ日時 2024-05-02 20:43:44
合計ジャッジ時間 5,114 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,192 KB
testcase_01 AC 6 ms
8,192 KB
testcase_02 AC 6 ms
8,192 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
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 AC 6 ms
8,192 KB
testcase_34 AC 6 ms
8,064 KB
testcase_35 AC 6 ms
8,192 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 33 ms
10,512 KB
testcase_39 AC 33 ms
10,504 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 33 ms
10,512 KB
testcase_43 AC 34 ms
10,512 KB
testcase_44 AC 63 ms
21,256 KB
testcase_45 AC 33 ms
10,548 KB
testcase_46 AC 34 ms
10,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
struct UnionFind {vector<int> par; // uf(x,y)->y
    UnionFind(int NV) { par.clear(); rep(i, 0, NV) par.push_back(i); }
    void reset() { rep(i, 0, par.size()) par[i] = i; }
    int operator[](int x) { return par[x] == x ? x : par[x] = operator[](par[x]); }
    void operator()(int x, int y) {x = operator[](x); y = operator[](y);if (x != y) par[x] = y;}};
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
typedef long long ll;
ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; }
// なるべく上限付きLCM使おう
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/



int N, A[101010], B[101010];
//---------------------------------------------------------------------------------------------------
int vis[1010][1010];
int f(int a, int b) {
    if (vis[a][b]) return -101010;
    vis[a][b] = 1;
    if (a == b) return 0;
    return f(A[a], B[b]) + 1;
}
//---------------------------------------------------------------------------------------------------
set<int> E[101010];
int CA[101010], CB[101010];
map<pair<int, int>, int> CC;
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> A[i], A[i]--;
    rep(i, 0, N) cin >> B[i], B[i]--;

    UnionFind AU(N), BU(N);
    rep(i, 0, N) CA[i] = CB[i] = 1;
    rep(i, 0, N) {
        if (AU[i] != AU[A[i]]) {
            int c = CA[AU[i]] + CA[AU[A[i]]];
            AU(i, A[i]);
            CA[AU[i]] = c;
        }
    }
    rep(i, 0, N) {
        if (BU[i] != BU[B[i]]) {
            int c = CB[BU[i]] + CB[BU[B[i]]];
            BU(i, B[i]);
            CB[BU[i]] = c;
        }
    }

    mint ans = 0;
    rep(i, 0, N) {
        E[AU[i]].insert(BU[i]);
        CC[{AU[i], BU[i]}]++;
    }
    rep(i, 0, N) if (AU[i] == i) {
        fore(b, E[i]) if(CA[i] != CB[b]){
            ll lc = lcm(CA[i], CB[b]);
            int c = CC[{AU[i], BU[i]}];
            
            if (max(CA[i], CB[b]) % min(CA[i], CB[b]) == 0 and 2 <= min(CA[i], CB[b])) c++;
            if (min(CA[i], CB[b]) == 1) c = 1;

            mint d = (mint(lc % 1000000007 - c + 1)) * (mint(lc % 1000000007) - c) / 2;
            ans += d;

            

            //printf("[%d(%d) %d(%d)] -> %d\n", i + 1, CA[i], b + 1, CB[b], d.get());
        }
    }
    cout << ans << endl;
    return;
    
    printf("CHECK!\n");
    int ans2 = 0;
    rep(i, 0, N) if(AU[i] == i) fore(b, E[i]) if(CA[i] != CB[b]) {
        vector<int> va, vb;
        rep(j, 0, N) if (AU[j] == AU[i]) va.push_back(j);
        rep(j, 0, N) if (BU[j] == BU[b]) vb.push_back(j);

        int sm = 0;
        vector<int> v;
        fore(aa, va) fore(bb, vb) {
            rep(j, 0, N) rep(k, 0, N) vis[j][k] = 0;
            int x = f(aa, bb);
            if (x < 0) continue;
            sm += x;
            v.push_back(x);
        }
        sort(v.begin(), v.end());
        printf("[%d(%d) %d(%d)] -> %d (", i + 1, CA[i], b + 1, CB[b], sm);
        fore(j, v) printf("%d ", j);
        printf(")\n");
        ans2 += sm;
    }
    printf("%d\n", ans2);
}
0