結果
問題 | No.590 Replacement |
ユーザー | ryoissy |
提出日時 | 2017-11-03 23:59:36 |
言語 | C++11 (gcc 11.4.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,710 bytes |
コンパイル時間 | 1,587 ms |
コンパイル使用メモリ | 168,000 KB |
実行使用メモリ | 64,544 KB |
最終ジャッジ日時 | 2024-11-23 15:16:24 |
合計ジャッジ時間 | 18,007 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
22,888 KB |
testcase_01 | AC | 6 ms
19,072 KB |
testcase_02 | AC | 6 ms
13,952 KB |
testcase_03 | RE | - |
testcase_04 | WA | - |
testcase_05 | RE | - |
testcase_06 | WA | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | AC | 6 ms
13,696 KB |
testcase_34 | AC | 6 ms
13,824 KB |
testcase_35 | AC | 6 ms
13,696 KB |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 105 ms
31,996 KB |
testcase_39 | AC | 104 ms
31,872 KB |
testcase_40 | TLE | - |
testcase_41 | TLE | - |
testcase_42 | AC | 103 ms
29,952 KB |
testcase_43 | AC | 97 ms
27,864 KB |
testcase_44 | AC | 68 ms
28,544 KB |
testcase_45 | WA | - |
testcase_46 | WA | - |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:53:17: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘ll*’ {aka ‘long long int*’} [-Wformat=] 53 | scanf("%d",&n); | ~^ ~~ | | | | | ll* {aka long long int*} | int* | %lld main.cpp:53:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 53 | scanf("%d",&n); | ~~~~~^~~~~~~~~ main.cpp:55:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 55 | scanf("%lld",&a[i]); | ~~~~~^~~~~~~~~~~~~~ main.cpp:60:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 60 | scanf("%lld",&b[i]); | ~~~~~^~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; ll n; ll a[100005],b[100005]; vector<int> G[2][100005]; int group[2][100005]; int size[2][100005]; vector<int> gg[2][100005]; int dist[2][100005]; bool used[100005]; 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); 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=dista-distb; 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,gb_size/gcdval); di%=ga_size/(ga/gcdval); vs.push_back((ll)dista+di*(ga_size/gcdval)); used[v]=true; }else{ ll di=mod_inverse(diff,gb_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; ans+=diff*(diff+1LL)/2LL; 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; }