結果

問題 No.590 Replacement
ユーザー PulmnPulmn
提出日時 2017-11-04 09:30:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,437 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 89,560 KB
実行使用メモリ 17,180 KB
最終ジャッジ日時 2023-08-14 17:50:15
合計ジャッジ時間 7,183 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 4 ms
4,384 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 34 ms
4,836 KB
testcase_14 AC 56 ms
5,200 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 14 ms
4,376 KB
testcase_17 AC 24 ms
4,376 KB
testcase_18 AC 76 ms
6,936 KB
testcase_19 AC 76 ms
6,468 KB
testcase_20 AC 21 ms
4,380 KB
testcase_21 AC 109 ms
7,324 KB
testcase_22 AC 102 ms
8,056 KB
testcase_23 AC 103 ms
7,904 KB
testcase_24 AC 98 ms
7,320 KB
testcase_25 AC 100 ms
7,432 KB
testcase_26 AC 103 ms
7,328 KB
testcase_27 AC 106 ms
7,776 KB
testcase_28 AC 114 ms
7,332 KB
testcase_29 AC 107 ms
7,164 KB
testcase_30 AC 109 ms
7,292 KB
testcase_31 AC 105 ms
7,032 KB
testcase_32 AC 104 ms
7,800 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 76 ms
8,504 KB
testcase_37 AC 74 ms
8,512 KB
testcase_38 AC 74 ms
7,804 KB
testcase_39 AC 73 ms
8,024 KB
testcase_40 AC 123 ms
12,660 KB
testcase_41 AC 122 ms
12,596 KB
testcase_42 AC 72 ms
7,528 KB
testcase_43 AC 72 ms
7,444 KB
testcase_44 AC 111 ms
17,180 KB
testcase_45 AC 77 ms
8,540 KB
testcase_46 AC 76 ms
8,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> P;
typedef vector<P> vp;

const ll mod=1e9+7;

ll gcd(ll a,ll b){
	if(!b) return a;
	return gcd(b,a%b);
}

ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}

int n;
vi a,b;

vi ac,ad,as,bc,bd,bs;
map<pair<P,int>,vp> m;

void f(vi& A,vi& C,vi& D,vi& S){
	for(int i=0;i<n;i++){
		cin>>A[i];
		A[i]--;
	}
	int cnt=0;
	for(int i=0;i<n;i++) if(C[i]==-1){
		int I=i,num=0;
		do{
			C[I]=cnt;
			D[I]=num;
			num++;
			I=A[I];
		}while(I!=i);
		S.push_back(num);
		cnt++;
	}
}

int main(){
	cin>>n;
	a=b=ad=bd=vi(n);
	ac=bc=vi(n,-1);
	f(a,ac,ad,as);
	f(b,bc,bd,bs);
	for(int i=0;i<n;i++){
		int g=gcd(as[ac[i]],bs[bc[i]]);
		m[{{ac[i],bc[i]},(ad[i]%g-bd[i]%g+g)%g}].push_back({ad[i],bd[i]});
	}
	ll res=0;
	for(auto i=m.begin();i!=m.end();i++){
		P tmp=i->first.first;
		vp v=i->second;
		int A=as[tmp.first],B=bs[tmp.second],S=v.size();
		ll l=lcm(A,B),g=gcd(A,B),P=v[0].first,Q=v[0].second;
		vl u(S+1),w(B/g);
		u[S]=l;
		int I=Q/g;
		for(int i=0;i<B/g;i++){
			(I+=A/g)%=B/g;
			w[I]=B/g-i-1;
		}
		for(int i=1;i<S;i++){
			ll X=v[i].first,Y=v[i].second,num;
			num=(P+A-X)%A;
			(Y+=num)%=B;
			num+=w[Y/g]*A;
			u[i]=num;
		}
		sort(u.begin(),u.end());
		for(int i=0;i<S;i++){
			ll sa=(u[i+1]-u[i])%mod;
			(res+=sa*(sa-1)/2)%=mod;
		}
	}
	cout<<res<<endl;
}
0