結果

問題 No.590 Replacement
ユーザー 👑 kmjpkmjp
提出日時 2017-11-03 23:42:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 2,306 bytes
コンパイル時間 3,006 ms
コンパイル使用メモリ 169,700 KB
実行使用メモリ 16,700 KB
最終ジャッジ日時 2023-08-15 09:07:47
合計ジャッジ時間 6,935 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 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,376 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 AC 23 ms
4,480 KB
testcase_14 AC 34 ms
5,216 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 15 ms
4,408 KB
testcase_18 AC 53 ms
7,016 KB
testcase_19 AC 53 ms
6,264 KB
testcase_20 AC 14 ms
4,376 KB
testcase_21 AC 65 ms
6,860 KB
testcase_22 AC 63 ms
7,956 KB
testcase_23 AC 68 ms
7,744 KB
testcase_24 AC 68 ms
7,420 KB
testcase_25 AC 70 ms
7,164 KB
testcase_26 AC 69 ms
6,864 KB
testcase_27 AC 68 ms
7,492 KB
testcase_28 AC 67 ms
6,548 KB
testcase_29 AC 69 ms
6,356 KB
testcase_30 AC 71 ms
6,476 KB
testcase_31 AC 69 ms
7,336 KB
testcase_32 AC 72 ms
7,880 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 55 ms
8,488 KB
testcase_37 AC 54 ms
8,500 KB
testcase_38 AC 54 ms
8,604 KB
testcase_39 AC 54 ms
8,488 KB
testcase_40 AC 120 ms
13,428 KB
testcase_41 AC 113 ms
13,344 KB
testcase_42 AC 55 ms
8,096 KB
testcase_43 AC 55 ms
7,548 KB
testcase_44 AC 95 ms
16,700 KB
testcase_45 AC 55 ms
8,616 KB
testcase_46 AC 55 ms
8,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;

int A[2][101010];
int uf[2][101010];
int id[2][101010];
int size[2][101010];
ll mo=1000000007;

map<pair<int,int>,vector<int>> M;

ll ext_gcd(ll p,ll q,ll& x, ll& y) { // get px+qy=gcd(p,q)
	if(q==0) return x=1,y=0,p;
	ll g=ext_gcd(q,p%q,y,x);
	y-=p/q*x;
	return g;
}

pair<ll,ll> crt(ll a1,ll mo1,ll a2,ll mo2) { // return (x,y) y=lcm(a1,a2),x%mo1=a1,x%mo2=a2
	ll g,x,y,z;
	g=ext_gcd(mo1,mo2,x,y);
	a1=(a1%mo1+mo1)%mo1;a2=(a2%mo2+mo2)%mo2;
	if(a1%g != a2%g) return pair<ll,ll>(-1,0); // N/A
	ll lcm=mo1*(mo2/g);
	if(lcm<mo1) return pair<ll,ll>(-2,0); // overflow
	ll v=a1+((a2-a1)%lcm+lcm)*x%lcm*(mo1/g);
	return make_pair(((v%lcm)+lcm) % lcm,lcm);
}

ll hoge(int X,int Y,vector<int> R) {
	int i;
	int g=__gcd(X,Y);
	
	map<int,vector<ll>> V;
	FORR(r,R) {
		int a=id[0][r];
		int b=id[1][r];
		int dif=(a-b+Y)%g;
		a=(a+X-dif)%X;
		pair<ll,ll> tmp=crt(a,X,b,Y);
		V[dif].push_back(tmp.first);
	}
	ll ret=0;
	FORR(r,V) {
		vector<ll> vec=r.second;
		sort(ALL(vec));
		vec.push_back(vec[0]+1LL*X*Y/g);
		FOR(i,vec.size()-1) {
			ll dif=vec[i+1]-vec[i];
			(ret+=(dif%mo)*((dif+mo-1)%mo)%mo*((mo+1)/2))%=mo;
		}
	}
	return ret;
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(j,2) {
		FOR(i,N) {
			cin>>A[j][i], A[j][i]--;
			uf[j][i]=-1;
		}
		FOR(i,N) if(uf[j][i]==-1) {
			int pre=i;
			size[j][i]=1;
			uf[j][i]=i;
			while(A[j][pre]!=i) {
				pre=A[j][pre];
				uf[j][pre]=i;
				id[j][pre]=size[j][i];
				size[j][i]++;
			}
		}
	}
	FOR(i,N) M[{uf[0][i],uf[1][i]}].push_back(i);
	ll ret=0;
	FORR(r,M) {
		(ret+=hoge(size[0][r.first.first],size[1][r.first.second],r.second))%=mo;
	}
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0