結果

問題 No.590 Replacement
ユーザー PulmnPulmn
提出日時 2017-04-15 15:16:47
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,721 bytes
コンパイル時間 992 ms
コンパイル使用メモリ 91,128 KB
実行使用メモリ 17,252 KB
最終ジャッジ日時 2023-09-26 15:20:38
合計ジャッジ時間 5,205 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 4 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 29 ms
4,860 KB
testcase_14 AC 47 ms
5,156 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 12 ms
4,380 KB
testcase_17 AC 20 ms
4,380 KB
testcase_18 AC 60 ms
6,872 KB
testcase_19 AC 64 ms
6,656 KB
testcase_20 AC 18 ms
4,384 KB
testcase_21 AC 93 ms
7,428 KB
testcase_22 AC 87 ms
8,216 KB
testcase_23 AC 88 ms
7,980 KB
testcase_24 AC 81 ms
7,436 KB
testcase_25 AC 83 ms
7,084 KB
testcase_26 AC 84 ms
7,268 KB
testcase_27 AC 88 ms
7,804 KB
testcase_28 AC 97 ms
7,060 KB
testcase_29 AC 91 ms
6,968 KB
testcase_30 AC 93 ms
7,312 KB
testcase_31 AC 88 ms
7,292 KB
testcase_32 AC 88 ms
8,088 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 56 ms
7,896 KB
testcase_39 AC 56 ms
7,700 KB
testcase_40 AC 132 ms
12,488 KB
testcase_41 AC 133 ms
12,504 KB
testcase_42 AC 57 ms
7,444 KB
testcase_43 AC 56 ms
7,604 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

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(){
	ios::sync_with_stdio(0);
	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;
	ll flag=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;
		if(S>=5) flag++;
		if(g>=5) flag++;
		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;
		}
	}
	if(flag<2) return 0;
	cout<<res<<endl;
}
0