結果

問題 No.590 Replacement
ユーザー ixmelixmel
提出日時 2017-04-21 16:05:17
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,384 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 109,144 KB
実行使用メモリ 22,640 KB
最終ジャッジ日時 2023-09-27 10:53:57
合計ジャッジ時間 5,894 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 89 ms
14,052 KB
testcase_37 AC 89 ms
13,100 KB
testcase_38 AC 82 ms
12,512 KB
testcase_39 AC 82 ms
12,352 KB
testcase_40 AC 181 ms
16,684 KB
testcase_41 AC 176 ms
16,568 KB
testcase_42 AC 82 ms
12,464 KB
testcase_43 AC 82 ms
11,280 KB
testcase_44 AC 133 ms
22,640 KB
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mt make_tuple
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef tuple<ll,ll,ll> tp;
typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-7;
const int inf=1e8;
const ll INF=1e16;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
ll gcd(ll a,ll b){
	return (b==0?a:gcd(b,a%b));
}
ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
int n,size,co;
vvi in,info,siz;
vi used;
void f(int a,int b){
	if(used[a])return;
	used[a]=true;
	info[b*2][a]=co;
	info[b*2+1][a]=size++;
	f(in[b][a],b);
}
int main(){
	cin>>n;
	in=vvi(2,vi(n));//a,b
	info=vvi(4,vi(n));//numa,in_numa,numb,in_numb
	siz=vvi(2);//num_loop
	rep(i,2)rep(j,n)cin>>in[i][j];
	rep(i,2)rep(j,n)in[i][j]--;
	rep(i,2){
		used=vi(n);
		size=0,co=0;//loop_size,num_loop
		rep(j,n)if(used[j]==0){
			size=0;
			f(j,i);
			siz[i].pb(size);
			co++;
		}
	}
	map<tp,vi>ma;
	rep(i,n){
		ll gc_d=gcd(siz[0][info[0][i]],siz[1][info[2][i]]);
		ll t=(info[1][i]%gc_d-info[3][i]%gc_d+gc_d)%gc_d;
		ma[mt(info[0][i],info[2][i],t)].pb(i);
	}
	ll out=0,MOD=1000000007,divby2=500000004;//divby2=埋め込み
	for(auto it=ma.begin();it!=ma.end();it++){
		ll loop_a,loop_b,gomi;
		vi node=it->second;
		tie(loop_a,loop_b,gomi)=it->first;
		ll siz_a=siz[0][loop_a],siz_b=siz[1][loop_b];
		ll gc_d=gcd(siz_a,siz_b);
		vi point(siz_a/gc_d);
		int now=0;
		rep(i,siz_a/gc_d){
			point[now]=i;
			(now+=siz_b)%=siz_a/gc_d;
		}
		vi co(node.size());
		rep(i,node.size()){
			ll a=info[1][node[i]],b=info[3][node[i]];
			ll t=(a-b+siz_b)%siz_a;
			co[i]=point[t/gc_d]*siz_b+b;
		}
		sort(all(co));
		co.pb(lcm(siz_a,siz_b)+co[0]);
		rep(i,co.size()-1){
			ll sa=co[i+1]-co[i]-1;
			(out+=sa*(sa+1)%MOD*divby2%MOD)%=MOD;
		}
	}
	cout<<out<<endl;
}






0