結果

問題 No.1013 〇マス進む
ユーザー otamay6otamay6
提出日時 2019-11-09 13:32:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,385 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 193,328 KB
実行使用メモリ 14,636 KB
最終ジャッジ日時 2023-10-13 07:10:37
合計ジャッジ時間 11,362 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 WA -
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,356 KB
testcase_12 WA -
testcase_13 AC 8 ms
4,348 KB
testcase_14 AC 91 ms
8,556 KB
testcase_15 AC 146 ms
12,040 KB
testcase_16 AC 135 ms
11,076 KB
testcase_17 AC 79 ms
7,780 KB
testcase_18 AC 98 ms
8,896 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 73 ms
7,480 KB
testcase_22 AC 100 ms
8,968 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 33 ms
5,084 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 59 ms
6,676 KB
testcase_31 AC 116 ms
10,108 KB
testcase_32 AC 87 ms
8,340 KB
testcase_33 AC 89 ms
8,352 KB
testcase_34 AC 141 ms
11,208 KB
testcase_35 AC 130 ms
10,772 KB
testcase_36 AC 10 ms
4,352 KB
testcase_37 AC 10 ms
4,348 KB
testcase_38 AC 152 ms
12,136 KB
testcase_39 AC 49 ms
6,012 KB
testcase_40 AC 139 ms
11,268 KB
testcase_41 AC 34 ms
5,148 KB
testcase_42 AC 84 ms
8,124 KB
testcase_43 AC 55 ms
6,328 KB
testcase_44 AC 89 ms
8,364 KB
testcase_45 AC 79 ms
7,760 KB
testcase_46 AC 38 ms
5,548 KB
testcase_47 AC 83 ms
7,964 KB
testcase_48 AC 101 ms
9,044 KB
testcase_49 AC 138 ms
11,120 KB
testcase_50 AC 119 ms
9,936 KB
testcase_51 AC 106 ms
9,360 KB
testcase_52 AC 21 ms
4,412 KB
testcase_53 AC 31 ms
5,120 KB
testcase_54 AC 133 ms
10,772 KB
testcase_55 AC 38 ms
5,396 KB
testcase_56 AC 168 ms
12,764 KB
testcase_57 AC 113 ms
9,648 KB
testcase_58 AC 198 ms
14,228 KB
testcase_59 AC 194 ms
14,328 KB
testcase_60 AC 193 ms
14,192 KB
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*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);
    path.resize(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);
            LA[v] = path[K];
            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<<tocycle[i].second - tocycle[LA[i]].second<<endl;
        }else{
            cout<<ans[i].first<<endl;
        }
    }
}
0