結果

問題 No.590 Replacement
ユーザー chocoruskchocorusk
提出日時 2020-04-13 15:56:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,292 bytes
コンパイル時間 1,881 ms
コンパイル使用メモリ 153,368 KB
実行使用メモリ 27,852 KB
最終ジャッジ日時 2023-10-25 06:57:39
合計ジャッジ時間 8,461 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,784 KB
testcase_01 AC 5 ms
9,784 KB
testcase_02 AC 4 ms
9,784 KB
testcase_03 AC 4 ms
9,784 KB
testcase_04 AC 5 ms
9,788 KB
testcase_05 AC 5 ms
9,784 KB
testcase_06 AC 4 ms
9,788 KB
testcase_07 AC 10 ms
10,044 KB
testcase_08 AC 8 ms
9,916 KB
testcase_09 AC 10 ms
10,068 KB
testcase_10 AC 7 ms
9,936 KB
testcase_11 AC 6 ms
9,844 KB
testcase_12 AC 10 ms
10,084 KB
testcase_13 AC 36 ms
10,952 KB
testcase_14 AC 52 ms
11,096 KB
testcase_15 AC 7 ms
9,948 KB
testcase_16 AC 16 ms
10,292 KB
testcase_17 AC 24 ms
10,504 KB
testcase_18 AC 81 ms
13,684 KB
testcase_19 AC 79 ms
12,448 KB
testcase_20 AC 23 ms
10,436 KB
testcase_21 AC 97 ms
13,112 KB
testcase_22 AC 97 ms
14,488 KB
testcase_23 AC 103 ms
14,276 KB
testcase_24 AC 102 ms
13,180 KB
testcase_25 AC 103 ms
13,032 KB
testcase_26 AC 104 ms
12,988 KB
testcase_27 AC 103 ms
13,744 KB
testcase_28 AC 102 ms
12,292 KB
testcase_29 AC 102 ms
12,252 KB
testcase_30 AC 104 ms
12,112 KB
testcase_31 AC 103 ms
13,488 KB
testcase_32 AC 107 ms
14,176 KB
testcase_33 AC 5 ms
9,784 KB
testcase_34 AC 4 ms
9,784 KB
testcase_35 AC 4 ms
9,784 KB
testcase_36 AC 91 ms
15,444 KB
testcase_37 AC 90 ms
15,444 KB
testcase_38 AC 88 ms
15,444 KB
testcase_39 AC 88 ms
15,444 KB
testcase_40 AC 158 ms
19,292 KB
testcase_41 AC 162 ms
19,292 KB
testcase_42 AC 89 ms
14,652 KB
testcase_43 AC 87 ms
13,912 KB
testcase_44 AC 149 ms
27,852 KB
testcase_45 AC 89 ms
15,444 KB
testcase_46 AC 89 ms
15,444 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>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll inv(ll a, ll m){
    ll b=m, x=1, y=0;
    while(b>0){
        ll t=a/b;
        swap(a-=t*b, b);
        swap(x-=t*y, y);
    }
    return (x%m+m)%m;
}
int main()
{
    int n;
	cin>>n;
	int a[100010], b[100010];
	for(int i=0; i<n; i++){
		cin>>a[i];
		a[i]--;
	}
	for(int i=0; i<n; i++){
		cin>>b[i];
		b[i]--;
	}
	int p[100010], q[100010];
	fill(p, p+n, -1); fill(q, q+n, -1);
	vector<int> va[100010], vb[100010];
	int idx=0;
	for(int i=0; i<n; i++){
		if(p[i]!=-1) continue;
		int x=i;
		do{
			p[x]=idx;
			va[idx].push_back(x);
			x=a[x];
		}while(x!=i);
		idx++;
	}
	idx=0;
	for(int i=0; i<n; i++){
		if(q[i]!=-1) continue;
		int x=i;
		do{
			q[x]=idx;
			vb[idx].push_back(x);
			x=b[x];
		}while(x!=i);
		idx++;
	}
	int ra[100010], rb[100010];
	for(int i=0; i<n; i++){
		for(int j=0; j<va[i].size(); j++){
			ra[va[i][j]]=j;
		}
		for(int j=0; j<vb[i].size(); j++){
			rb[vb[i][j]]=j;
		}
	}
	map<P, vector<int>> mp;
	for(int i=0; i<n; i++){
		mp[{p[i], q[i]}].push_back(i);
	}
	auto sum=[](ll m){
		if(m%2==0) return m/2%MOD*((m-1)%MOD)%MOD;
		else return (m-1)/2%MOD*(m%MOD)%MOD;
	};
	ll ans=0;
	for(auto pr:mp){
		auto v=pr.second;
		int p1=pr.first.first, q1=pr.first.second;
		ll cp=va[p1].size(), cq=vb[q1].size();
		ll g=gcd(cp, cq);
		map<ll, vector<ll>> mp1;
		for(auto i:v){
			ll rp=ra[i], rq=rb[i];
			ll s=(rq-rp%g+g)%g;
			rq=(rq-s+cq)%cq;
			ll x=inv(cp/g, cq/g)*(((rq-rp)/g)%(cq/g)+cq/g)%(cq/g);
			mp1[s].push_back(x*cp+rp);
		}
		for(auto pr2:mp1){
			auto w=pr2.second;
			sort(w.begin(), w.end());
			w.push_back(w[0]+cp*cq/g);
			for(int i=0; i<w.size()-1; i++){
				ll m=w[i+1]-w[i];
              //cout<<m<<" "<<sum(m)<<endl;
				(ans+=sum(m))%=MOD;
			}
		}
	}
	cout<<ans<<endl;
    return 0;
}
0