結果

問題 No.590 Replacement
ユーザー 👑 kmjpkmjp
提出日時 2017-11-03 23:30:45
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,340 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 189,668 KB
実行使用メモリ 20,092 KB
最終ジャッジ日時 2024-05-02 20:38:19
合計ジャッジ時間 6,197 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
11,268 KB
testcase_01 AC 8 ms
11,132 KB
testcase_02 AC 8 ms
11,140 KB
testcase_03 AC 8 ms
11,132 KB
testcase_04 AC 8 ms
11,132 KB
testcase_05 AC 8 ms
11,132 KB
testcase_06 AC 8 ms
11,136 KB
testcase_07 AC 54 ms
11,776 KB
testcase_08 AC 38 ms
11,516 KB
testcase_09 AC 90 ms
11,772 KB
testcase_10 AC 33 ms
11,648 KB
testcase_11 AC 14 ms
11,392 KB
testcase_12 AC 112 ms
11,780 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
権限があれば一括ダウンロードができます

ソースコード

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 hoge(vector<int> X,vector<int> Y,vector<int> R) {
	map<int,int> rev[2];
	int i;
	FOR(i,X.size()) rev[0][X[i]]=i;
	FOR(i,Y.size()) rev[1][Y[i]]=i;
	int g=__gcd(X.size(),Y.size());
	
	
	map<int,vector<ll>> V;
	FORR(r,R) {
		int a=id[0][r];
		int b=id[1][r];
		int dif=(a-b+Y.size())%g;
		a=(a+X.size()-dif)%X.size();
		ll tmp;
		for(tmp=b;;tmp+=Y.size()) {
			if(tmp%X.size()==a) break;
		}
		V[dif].push_back(tmp);
	}
	ll ret=0;
	FORR(r,V) {
		vector<ll> vec=r.second;
		sort(ALL(vec));
		vec.push_back(vec[0]+1LL*X.size()*Y.size()/g);
		FOR(i,vec.size()-1) {
			ll dif=vec[i+1]-vec[i];
			(ret+=dif*(dif-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],V[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