結果

問題 No.590 Replacement
ユーザー PulmnPulmn
提出日時 2017-04-15 19:35:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 1,623 bytes
コンパイル時間 3,802 ms
コンパイル使用メモリ 89,864 KB
実行使用メモリ 17,196 KB
最終ジャッジ日時 2023-09-26 15:23:21
合計ジャッジ時間 6,228 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 9 ms
4,384 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 3 ms
4,384 KB
testcase_12 AC 9 ms
4,384 KB
testcase_13 AC 40 ms
4,800 KB
testcase_14 AC 64 ms
5,096 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 15 ms
4,384 KB
testcase_17 AC 26 ms
4,380 KB
testcase_18 AC 86 ms
6,952 KB
testcase_19 AC 89 ms
6,496 KB
testcase_20 AC 24 ms
4,384 KB
testcase_21 AC 124 ms
7,476 KB
testcase_22 AC 116 ms
8,060 KB
testcase_23 AC 120 ms
7,956 KB
testcase_24 AC 113 ms
7,324 KB
testcase_25 AC 115 ms
7,252 KB
testcase_26 AC 117 ms
7,464 KB
testcase_27 AC 120 ms
7,756 KB
testcase_28 AC 130 ms
7,084 KB
testcase_29 AC 122 ms
7,100 KB
testcase_30 AC 125 ms
7,052 KB
testcase_31 AC 119 ms
7,132 KB
testcase_32 AC 118 ms
7,796 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 91 ms
8,484 KB
testcase_37 AC 91 ms
8,536 KB
testcase_38 AC 87 ms
7,860 KB
testcase_39 AC 88 ms
7,816 KB
testcase_40 AC 156 ms
12,616 KB
testcase_41 AC 158 ms
12,600 KB
testcase_42 AC 88 ms
7,464 KB
testcase_43 AC 87 ms
7,524 KB
testcase_44 AC 127 ms
17,196 KB
testcase_45 AC 91 ms
8,420 KB
testcase_46 AC 91 ms
8,756 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;
}

ll Pow_mod(ll n,ll p){
	ll r=1;
	for(;p>0;p>>=1){
		if(p&1) r=(r*n)%mod;
		n=(n*n)%mod;
	}
	return r;
}

ll Division_mod(ll n,ll m){
	return n*Pow_mod(m,mod-2)%mod;
}

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+=Division_mod(sa*(sa-1)%mod,2))%=mod;
		}
	}
	cout<<res<<endl;
}
0