結果

問題 No.590 Replacement
ユーザー ryoissyryoissy
提出日時 2017-11-04 00:18:05
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,862 bytes
コンパイル時間 1,679 ms
コンパイル使用メモリ 169,708 KB
実行使用メモリ 138,480 KB
最終ジャッジ日時 2024-05-02 20:58:23
合計ジャッジ時間 12,996 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
115,200 KB
testcase_01 AC 85 ms
109,952 KB
testcase_02 AC 85 ms
109,824 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
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 86 ms
109,696 KB
testcase_34 AC 86 ms
109,696 KB
testcase_35 AC 86 ms
109,952 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 205 ms
134,240 KB
testcase_39 AC 207 ms
134,244 KB
testcase_40 TLE -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:55:17: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘ll*’ {aka ‘long long int*’} [-Wformat=]
   55 |         scanf("%d",&n);
      |                ~^  ~~
      |                 |  |
      |                 |  ll* {aka long long int*}
      |                 int*
      |                %lld
main.cpp:55:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   55 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
main.cpp:57:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   57 |                 scanf("%lld",&a[i]);
      |                 ~~~~~^~~~~~~~~~~~~~
main.cpp:62:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |                 scanf("%lld",&b[i]);
      |                 ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

ll n;
ll a[1000005],b[1000005];
vector<int> G[2][1000005];
int group[2][1000005];
int size[2][1000005];
vector<int> gg[2][1000005];
list<int> lis[2][100005];
int dist[2][1000005];
bool used[1000005];

ll extgcd(ll a,ll b,ll& x,ll& y){
	ll d=a;
	if(b!=0LL){
		d=extgcd(b,a%b,y,x);
		y-=(a/b)*x;
	}else{
		x=1;
		y=0;
	}
	return d;
}

ll mod_inverse(ll a,ll m){
	ll x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}

ll gcd(ll a,ll b){
	if(b==0LL)return a;
	return gcd(b,a%b);
}

void dfs(int v,int div,int s){
	group[div][v]=s;
	size[div][s]++;
	gg[div][s].push_back(v);
	lis[div][s].push_back(v);
	for(int i=0;i<G[div][v].size();i++){
		int nv=G[div][v][i];
		if(group[div][nv]==-1){
			dist[div][nv]=dist[div][v]+1;
			dfs(nv,div,s);
		}
	}
}

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		a[i]--;
		G[0][i].push_back(a[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%lld",&b[i]);
		b[i]--;
		G[1][i].push_back(b[i]);
	}
	int cnt=0;
	memset(group,-1,sizeof(group));
	for(int i=0;i<n;i++){
		if(group[0][i]==-1){
			dfs(i,0,cnt++);
		}
	}
	cnt=0;
	for(int i=0;i<n;i++){
		if(group[1][i]==-1){
			dfs(i,1,cnt++);
		}
	}
	ll ans=0;
	for(int i=0;i<n;i++){
		if(used[i])continue;
		int ga=group[0][i];
		used[i]=true;
		int gb=group[1][i];
		int ga_size=gg[0][ga].size();
		int gb_size=gg[1][gb].size();
		vector<ll> vs;
		int gcdval=gcd(ga_size,gb_size);
		if(gcdval==0)while(1);
		ll all=(ll)ga_size*(gb_size/gcdval);
		vs.push_back(0);
		for(int j=0;j<gg[0][ga].size();j++){
			int v=gg[0][ga][j];
			if(used[v])continue;
			if(gb==group[1][v]){
				int dista=dist[0][i]-dist[0][v];
				int distb=dist[1][i]-dist[1][v];
				if(dista<0)dista+=ga_size;
				if(distb<0)distb+=gb_size;
				//printf("%d %d %d %d\n",i,v,dista,distb);
				if(dista==distb){
					used[v]=true;
					vs.push_back(dista);
				}else{
					if(ga_size%gb_size==0)continue;
					int diff=distb-dista;
					diff%=gb_size;
					if(diff<0)diff+=gb_size;
					if(gcd(ga_size,gb_size)!=1){
						if(diff%gcdval!=0)continue;
						diff/=gcdval;
						ll di=mod_inverse(diff,ga_size/gcdval);
						di%=ga_size/gcdval;
						if(di<0LL)di+=ga_size/gcdval;
						vs.push_back((ll)dista+di*(ga_size/gcdval));
						used[v]=true;
					}else{
						ll di=mod_inverse(diff,ga_size);
						di%=ga_size;
						if(di<0LL)di+=ga_size;
						vs.push_back((ll)dista+di*ga_size);
						used[v]=true;
					}
				}
			}
		}
		vs.push_back(all);
		sort(vs.begin(),vs.end());
		for(int j=0;j<(int)vs.size()-1;j++){
			//printf("%d ",vs[j]);
			ll diff=vs[j+1]-vs[j]-1LL;
			ll vac=(diff*(diff+1LL)%MOD)*mod_inverse(2,MOD)%MOD;
			ans+=vac;
			if(ans>=MOD)ans%=MOD;
		}
		//printf("%d\n",vs[vs.size()-1]);
		//printf("get%d %lld\n",i,ans);
	}
	printf("%lld\n",ans);
	return 0;
}
0