結果
問題 | No.590 Replacement |
ユーザー | ryoissy |
提出日時 | 2017-11-04 00:04:48 |
言語 | C++11 (gcc 11.4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,733 bytes |
コンパイル時間 | 1,565 ms |
コンパイル使用メモリ | 168,208 KB |
実行使用メモリ | 127,904 KB |
最終ジャッジ日時 | 2024-05-02 20:54:49 |
合計ジャッジ時間 | 11,434 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 79 ms
110,592 KB |
testcase_01 | AC | 79 ms
105,344 KB |
testcase_02 | AC | 76 ms
105,216 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 | 78 ms
105,216 KB |
testcase_34 | AC | 77 ms
105,088 KB |
testcase_35 | AC | 78 ms
105,216 KB |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 160 ms
123,300 KB |
testcase_39 | AC | 160 ms
123,300 KB |
testcase_40 | TLE | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
コンパイルメッセージ
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[1000005],b[1000005]; vector<int> G[2][1000005]; int group[2][1000005]; int size[2][1000005]; vector<int> gg[2][1000005]; 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); 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; 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,gb_size/gcdval); di%=ga_size/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; }