結果

問題 No.1013 〇マス進む
ユーザー ChanyuhChanyuh
提出日時 2020-05-06 21:40:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 4,000 bytes
コンパイル時間 886 ms
コンパイル使用メモリ 106,652 KB
実行使用メモリ 22,680 KB
最終ジャッジ日時 2023-09-16 07:33:43
合計ジャッジ時間 10,320 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,552 KB
testcase_01 AC 5 ms
13,676 KB
testcase_02 AC 5 ms
13,696 KB
testcase_03 AC 5 ms
13,708 KB
testcase_04 AC 5 ms
13,676 KB
testcase_05 AC 6 ms
13,864 KB
testcase_06 AC 5 ms
13,676 KB
testcase_07 AC 6 ms
13,728 KB
testcase_08 AC 5 ms
13,888 KB
testcase_09 AC 7 ms
13,640 KB
testcase_10 AC 6 ms
13,728 KB
testcase_11 AC 6 ms
13,608 KB
testcase_12 AC 7 ms
13,720 KB
testcase_13 AC 11 ms
13,888 KB
testcase_14 AC 92 ms
17,804 KB
testcase_15 AC 157 ms
20,492 KB
testcase_16 AC 139 ms
19,840 KB
testcase_17 AC 80 ms
17,252 KB
testcase_18 AC 100 ms
18,076 KB
testcase_19 AC 59 ms
16,284 KB
testcase_20 AC 187 ms
22,168 KB
testcase_21 AC 73 ms
17,092 KB
testcase_22 AC 107 ms
18,256 KB
testcase_23 AC 34 ms
15,136 KB
testcase_24 AC 203 ms
22,292 KB
testcase_25 AC 196 ms
22,136 KB
testcase_26 AC 34 ms
15,068 KB
testcase_27 AC 72 ms
16,744 KB
testcase_28 AC 36 ms
15,152 KB
testcase_29 AC 189 ms
21,944 KB
testcase_30 AC 62 ms
16,544 KB
testcase_31 AC 121 ms
18,960 KB
testcase_32 AC 90 ms
17,820 KB
testcase_33 AC 88 ms
17,808 KB
testcase_34 AC 147 ms
20,156 KB
testcase_35 AC 136 ms
19,596 KB
testcase_36 AC 13 ms
14,120 KB
testcase_37 AC 13 ms
14,072 KB
testcase_38 AC 164 ms
20,768 KB
testcase_39 AC 51 ms
16,028 KB
testcase_40 AC 147 ms
20,028 KB
testcase_41 AC 36 ms
15,296 KB
testcase_42 AC 83 ms
17,452 KB
testcase_43 AC 54 ms
16,100 KB
testcase_44 AC 89 ms
17,884 KB
testcase_45 AC 80 ms
17,400 KB
testcase_46 AC 39 ms
15,380 KB
testcase_47 AC 90 ms
17,476 KB
testcase_48 AC 100 ms
18,088 KB
testcase_49 AC 146 ms
20,148 KB
testcase_50 AC 122 ms
19,056 KB
testcase_51 AC 109 ms
18,432 KB
testcase_52 AC 24 ms
14,656 KB
testcase_53 AC 33 ms
15,004 KB
testcase_54 AC 135 ms
19,656 KB
testcase_55 AC 41 ms
15,428 KB
testcase_56 AC 169 ms
21,452 KB
testcase_57 AC 116 ms
18,784 KB
testcase_58 AC 209 ms
22,680 KB
testcase_59 AC 207 ms
22,428 KB
testcase_60 AC 204 ms
22,544 KB
testcase_61 AC 205 ms
22,504 KB
testcase_62 AC 207 ms
22,524 KB
testcase_63 AC 5 ms
13,564 KB
testcase_64 AC 5 ms
13,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int n,k;
int p[100010];
vector<int> G[100010],R[100010];
bool going[100010],backing[100010];
vector<int> cycle[100010];
vector<ll> cycle_S[100010];
int comp_idx[100010],cycle_idx[100010];
bool isCycle[100010];
ll ans[100010];
ll depth[100010];

int dfs(int s,vector<int> &hist){
    //cout << s << endl;
    if(going[s]) return -1;
    going[s]=true;
    hist.push_back(s);
    int res=-1;
    for(int t:G[s]){
        if(backing[t])continue;
        if(going[t]){
            return t;
        }
        res=dfs(t,hist);
        if(res!=-1) return res;
    }
    hist.pop_back();
    backing[s]=true;
    return -1;
}

void rdfs(int s,vector<int> &v,ll dep,int id){
    if(ans[s]!=-1) return;
    comp_idx[s]=id;
    //cout << s << endl;
    v.push_back(s);
    depth[s]=dep;
    int d=v.size()-1;
    int siz=cycle[comp_idx[s]].size();
    if(d-k-1<0){
        ans[s]=depth[s];
        ll m=k-d;
        int sta=cycle_idx[v[0]];
        ll gol=sta+m;
        //cout << sta << " " << gol << endl;
        if(gol<siz){
            ans[s]+=cycle_S[comp_idx[s]][gol]-cycle_S[comp_idx[s]][sta];
        }
        else{
            ans[s]+=cycle_S[comp_idx[s]][siz]-cycle_S[comp_idx[s]][sta]+cycle_S[comp_idx[s]][gol%siz]+(ll)(gol/siz-1)*cycle_S[comp_idx[s]][siz];
        }
    }
    else{
        //cout << s << " " << v[d-k] << endl;
        ans[s]=depth[s]-depth[v[d-k]];
    }
    for(int t:R[s]){
        if(isCycle[t]) continue;
        rdfs(t,v,dep+p[t],id);
    }
    v.pop_back();
}

void solve(){
    cin >> n >> k;
    rep(i,n){
        cin >> p[i];
        G[i].push_back((i+p[i])%n);
        R[(i+p[i])%n].push_back(i);
        comp_idx[i]=-1;
        cycle_idx[i]=-1;
        ans[i]=-1;
    }
    int id=0;
    rep(i,n){
        if(backing[i]) continue;
        vector<int> hist={};
        int t=dfs(i,hist);
        int l=0;
        bool cy=false;
        if(t!=-1){
            cycle_S[id].push_back(0);
            for(int c:hist){
                if(!cy && c==t){
                    cy=true;
                }
                else if(!cy){
                    continue;
                }
                //cout << c << " ";
                cycle[id].push_back(c);
                cycle_S[id].push_back(cycle_S[id].back()+p[c]);
                isCycle[c]=true;
                cycle_idx[c]=l;
                comp_idx[c]=id;
                l++;
            }
            // cout << "" << endl;
            // for(int s:cycle_S[id]){
            //     cout << s << " ";
            // }
            // cout << "" << endl;
            id++;
        }
    }
    // rep(i,n){
    //     cout << i << " " << comp_idx[i] << " " << cycle_idx[i] << endl;
    // }
    rep(i,n){
        vector<int> v(0);
        if(isCycle[i]) rdfs(i,v,0,comp_idx[i]);
    }
    // rep(i,n){
    //     cout << depth[i] << " ";
    // }
    // cout << "" << endl;
    rep(i,n){
        cout << i+1+ans[i] << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0