結果
問題 |
No.1013 〇マス進む
|
ユーザー |
![]() |
提出日時 | 2019-11-09 14:07:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 206 ms / 2,000 ms |
コード長 | 5,411 bytes |
コンパイル時間 | 2,428 ms |
コンパイル使用メモリ | 195,048 KB |
実行使用メモリ | 14,096 KB |
最終ジャッジ日時 | 2024-09-15 04:25:26 |
合計ジャッジ時間 | 11,416 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 62 |
ソースコード
/*under Implementation K回の試行で閉じたサイクルにたどり着かない場合があり、これをO(N)で計算する実装ができてない */ #include<bits/stdc++.h> #include<unistd.h> #define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i) #define rep(i,a,b) for(int i=int(a);i<int(b);++i) #define All(x) (x).begin(),(x).end() #define DEBUG(x) cerr<<(x)<<endl using namespace std; typedef long long ll; typedef pair<ll,ll> P; class node_type { public: int v; bool s; node_type(const int v, const bool s) : v(v), s(s) {} }; ll N,K; vector<ll> p; vector<P> incycle;//(何番目の閉じたサイクル内にいるか,サイクルの何処に位置するか) vector<P> tocycle;//(閉じたサイクルのどの数字に最初に当たるか、そこまでの長さ) vector<int> tocnt;//閉じたサイクルに行くまでの手数 vector<vector<P>> cycle;//(サイクルのk番目のP上での位置、スタート地点から何個P同士の境界を越えたか) vector<bool> outcycle; vector<P> ans; vector<vector<ll>> tree; std::vector<int> LA; std::vector<int> path; std::vector<node_type> stuck; void rsz(){ p.resize(N); incycle.resize(N); tocycle.resize(N); tocnt.resize(N); outcycle.resize(N,false); ans.resize(N); tree.resize(N); LA.resize(N,-1); stuck.reserve(N); } void cycle_detection(){ vector<bool> used(N,false); vector<int> prev(N,-1); int k=1; REP(i,N){ if(used[i]||incycle[i].first!=-1||prev[i]!=-1) continue; int j=i,pre=-1; bool out=false; while(!used[j]){ used[j]=true; prev[j]=pre; pre=j; j+=p[j]; j%=N; if(incycle[j].first!=-1||outcycle[j]){ out=true; break; } } if(out) {//サイクルに合流する数字は別に計算する for(int v=pre;v!=-1;v=prev[v]){ outcycle[v]=true; } continue; } cycle.resize(k+1); int u = pre, t = 0, num = 0; while(u!=j){ u=prev[u]; } for(int v=prev[u];v!=-1;v=prev[v]){ outcycle[v]=true; } do{ cycle[k].push_back(P(u,t)); incycle[u] = P(k,num); u+=p[u]; if(u>=N){ u-=N; t++; } num++; }while(u!=j); k++; } } P outcycle_calculate(int i){ if(incycle[i].first!=-1){ return P(i,0); } if(tocycle[i].first!=-1){ return tocycle[i]; } P res=outcycle_calculate((i+p[i])%N); tocnt[i]=tocnt[(i+p[i])%N]+1; tree[(i+p[i])%N].push_back(i); return tocycle[i]=P(res.first,res.second+p[i]); } P Ans_calculate(int start){ ll res=start; ll remain=K; if(incycle[res].first==-1){ remain-=tocnt[res]; res+=tocycle[res].second; } if(remain>0){ int C = incycle[res%N].first, num=incycle[res%N].second; ll LtoF = (cycle[C].back().first>=cycle[C][0].first); ll clen = N*(cycle[C].back().second + LtoF ); ll csz = cycle[C].size(); res += clen*(remain/csz); remain%=csz; int last=(num+remain)%csz; if(last<num){ ll border = cycle[C].back().second - cycle[C][num].second -1 + LtoF + cycle[C][last].second; if(border!=-1){ res += N - cycle[C][num].first + N*border + cycle[C][last].first; } else{ res += cycle[C][last].first - cycle[C][num].first; } } else{ ll border = cycle[C][last].second - cycle[C][num].second -1; if(border!=-1){ res += N - cycle[C][num].first + N*border + cycle[C][last].first; } else{ res += cycle[C][last].first - cycle[C][num].first; } } remain=0; } return P(res+1,remain); } void makeLA(int root){ stuck.emplace_back(root, true); while (!stuck.empty()) { if (stuck.back().s) { stuck.back().s = false; const int v = stuck.back().v; path.push_back(v); if(path.size()>K) LA[v] = path[(int)path.size()-K-1]; for (const int e : tree[v]) { stuck.emplace_back(e, true); } } else { stuck.pop_back(); path.pop_back(); } } return ; } int main(){ cin>>N>>K; rsz(); REP(i,N){ cin>>p[i]; incycle[i] = P(-1,-1); tocycle[i] = P(-1,-1); if(p[i]==N){ incycle[i] = P(0,0); cycle.push_back({P(i,0)}); } } cycle_detection(); //REP(i,cycle.size()) REP(j,cycle[i].size()) cerr<<cycle[i][j].first<<" \n"[j==cycle[i].size()-1]; REP(i,N){ outcycle_calculate(i); //cout<<i<<endl<<tocycle[i].first<<" "<<tocycle[i].second<<endl; } REP(i,N) ans[i]=Ans_calculate(i); REP(i,N){ if(ans[i].second!=0){ if(LA[i]==-1) makeLA(tocycle[i].first); cout<<i+1+tocycle[i].second - tocycle[LA[i]].second<<endl; }else{ cout<<ans[i].first<<endl; } } }