結果

問題 No.590 Replacement
ユーザー 👑 kmjpkmjp
提出日時 2017-11-03 23:36:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 2,710 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 173,488 KB
実行使用メモリ 29,688 KB
最終ジャッジ日時 2023-08-15 09:05:11
合計ジャッジ時間 6,136 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,340 KB
testcase_01 AC 8 ms
12,400 KB
testcase_02 AC 9 ms
12,344 KB
testcase_03 AC 9 ms
12,352 KB
testcase_04 AC 8 ms
12,252 KB
testcase_05 AC 8 ms
12,112 KB
testcase_06 AC 8 ms
12,256 KB
testcase_07 AC 12 ms
12,648 KB
testcase_08 AC 10 ms
12,500 KB
testcase_09 AC 12 ms
12,556 KB
testcase_10 AC 11 ms
12,532 KB
testcase_11 AC 9 ms
12,272 KB
testcase_12 AC 13 ms
12,608 KB
testcase_13 AC 32 ms
13,296 KB
testcase_14 AC 45 ms
13,460 KB
testcase_15 AC 10 ms
12,504 KB
testcase_16 AC 18 ms
12,836 KB
testcase_17 AC 24 ms
12,876 KB
testcase_18 AC 67 ms
15,180 KB
testcase_19 AC 66 ms
14,716 KB
testcase_20 AC 22 ms
13,096 KB
testcase_21 AC 82 ms
14,976 KB
testcase_22 AC 79 ms
16,576 KB
testcase_23 AC 85 ms
16,080 KB
testcase_24 AC 84 ms
15,172 KB
testcase_25 AC 87 ms
14,848 KB
testcase_26 AC 86 ms
15,024 KB
testcase_27 AC 85 ms
15,464 KB
testcase_28 AC 85 ms
14,484 KB
testcase_29 AC 86 ms
14,352 KB
testcase_30 AC 87 ms
14,516 KB
testcase_31 AC 86 ms
15,276 KB
testcase_32 AC 89 ms
15,840 KB
testcase_33 AC 8 ms
12,332 KB
testcase_34 AC 9 ms
12,276 KB
testcase_35 AC 8 ms
12,112 KB
testcase_36 AC 70 ms
16,868 KB
testcase_37 AC 70 ms
17,020 KB
testcase_38 AC 70 ms
16,728 KB
testcase_39 AC 69 ms
16,760 KB
testcase_40 AC 141 ms
21,524 KB
testcase_41 AC 146 ms
21,316 KB
testcase_42 AC 70 ms
16,768 KB
testcase_43 AC 69 ms
15,504 KB
testcase_44 AC 113 ms
29,688 KB
testcase_45 AC 69 ms
16,808 KB
testcase_46 AC 69 ms
16,732 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;
template<int um> class UF {
	public:
	vector<int> par,rank;
	UF() {rank=vector<int>(um,0); for(int i=0;i<um;i++) par.push_back(i);}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<200000> uf[2];


int A[2][101010];
int id[2][101010];
vector<int> V[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,A[j][i]);
		}
		FOR(i,N) if(uf[j][i]==i) {
			V[j][i].push_back(i);
			while(A[j][V[j][i].back()]!=i) {
				id[j][A[j][V[j][i].back()]]=V[j][i].size();
				V[j][i].push_back(A[j][V[j][i].back()]);
			}
		}
	}
	FOR(i,N) M[{uf[0][i],uf[1][i]}].push_back(i);
	ll ret=0;
	FORR(r,M) {
		(ret+=hoge(V[0][r.first.first].size(),V[1][r.first.second].size(),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