結果

問題 No.1013 〇マス進む
ユーザー ChanyuhChanyuh
提出日時 2020-05-06 21:22:28
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,891 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 105,508 KB
実行使用メモリ 22,180 KB
最終ジャッジ日時 2023-09-16 07:30:26
合計ジャッジ時間 11,234 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,020 KB
testcase_01 AC 6 ms
13,024 KB
testcase_02 AC 5 ms
12,884 KB
testcase_03 AC 6 ms
12,916 KB
testcase_04 AC 6 ms
13,048 KB
testcase_05 AC 7 ms
13,052 KB
testcase_06 WA -
testcase_07 AC 7 ms
13,076 KB
testcase_08 AC 6 ms
13,200 KB
testcase_09 AC 7 ms
13,084 KB
testcase_10 WA -
testcase_11 AC 6 ms
12,924 KB
testcase_12 WA -
testcase_13 AC 11 ms
13,348 KB
testcase_14 AC 94 ms
17,280 KB
testcase_15 AC 155 ms
20,100 KB
testcase_16 AC 140 ms
19,472 KB
testcase_17 AC 79 ms
16,864 KB
testcase_18 AC 102 ms
17,596 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 73 ms
16,520 KB
testcase_22 AC 104 ms
17,892 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 35 ms
14,456 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 63 ms
16,048 KB
testcase_31 AC 118 ms
18,724 KB
testcase_32 AC 89 ms
17,188 KB
testcase_33 AC 89 ms
17,076 KB
testcase_34 AC 144 ms
19,688 KB
testcase_35 AC 140 ms
19,316 KB
testcase_36 AC 14 ms
13,352 KB
testcase_37 AC 13 ms
13,424 KB
testcase_38 AC 161 ms
20,460 KB
testcase_39 AC 51 ms
15,388 KB
testcase_40 AC 145 ms
19,676 KB
testcase_41 AC 37 ms
14,692 KB
testcase_42 AC 86 ms
16,908 KB
testcase_43 AC 57 ms
15,644 KB
testcase_44 AC 90 ms
17,260 KB
testcase_45 AC 80 ms
16,600 KB
testcase_46 AC 40 ms
14,856 KB
testcase_47 AC 88 ms
16,896 KB
testcase_48 AC 101 ms
17,760 KB
testcase_49 AC 144 ms
19,500 KB
testcase_50 AC 122 ms
18,644 KB
testcase_51 AC 110 ms
17,980 KB
testcase_52 AC 24 ms
14,068 KB
testcase_53 AC 33 ms
14,348 KB
testcase_54 AC 131 ms
19,164 KB
testcase_55 AC 42 ms
14,992 KB
testcase_56 AC 172 ms
20,868 KB
testcase_57 AC 117 ms
18,408 KB
testcase_58 AC 205 ms
22,136 KB
testcase_59 AC 205 ms
21,996 KB
testcase_60 AC 206 ms
22,180 KB
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 5 ms
13,088 KB
testcase_64 AC 5 ms
13,156 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];
int 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,int dep,int id){
    if(ans[s]!=-1) return;
    comp_idx[s]=id;
    //cout << s << endl;
    v.push_back(s);
    if(v.size()!=1)depth[s]=dep+p[s];
    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]].back();
        }
    }
    else{
        ans[s]=depth[s]-depth[v[d-k-1]];
    }
    for(int t:R[s]){
        if(isCycle[t]) continue;
        rdfs(t,v,depth[s],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 << i+1+ans[i] << endl;
    }
}

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