結果

問題 No.1013 〇マス進む
ユーザー ChanyuhChanyuh
提出日時 2020-05-06 21:25:45
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,889 bytes
コンパイル時間 919 ms
コンパイル使用メモリ 106,268 KB
実行使用メモリ 22,580 KB
最終ジャッジ日時 2023-09-16 07:31:45
合計ジャッジ時間 9,943 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,700 KB
testcase_01 AC 5 ms
13,732 KB
testcase_02 AC 5 ms
13,676 KB
testcase_03 AC 6 ms
13,600 KB
testcase_04 AC 5 ms
13,624 KB
testcase_05 AC 6 ms
13,664 KB
testcase_06 WA -
testcase_07 AC 7 ms
13,856 KB
testcase_08 AC 6 ms
13,596 KB
testcase_09 AC 6 ms
13,760 KB
testcase_10 WA -
testcase_11 AC 6 ms
13,644 KB
testcase_12 WA -
testcase_13 AC 12 ms
14,000 KB
testcase_14 AC 95 ms
17,788 KB
testcase_15 AC 155 ms
20,628 KB
testcase_16 AC 143 ms
19,756 KB
testcase_17 AC 80 ms
17,316 KB
testcase_18 AC 99 ms
18,236 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 73 ms
16,924 KB
testcase_22 AC 101 ms
18,352 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 34 ms
15,176 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 62 ms
16,528 KB
testcase_31 AC 123 ms
19,048 KB
testcase_32 AC 89 ms
17,624 KB
testcase_33 AC 91 ms
17,648 KB
testcase_34 AC 148 ms
19,988 KB
testcase_35 AC 146 ms
19,656 KB
testcase_36 AC 13 ms
14,100 KB
testcase_37 AC 13 ms
14,068 KB
testcase_38 AC 165 ms
20,680 KB
testcase_39 AC 51 ms
16,100 KB
testcase_40 AC 149 ms
19,952 KB
testcase_41 AC 36 ms
15,160 KB
testcase_42 AC 88 ms
17,580 KB
testcase_43 AC 56 ms
16,100 KB
testcase_44 AC 94 ms
17,688 KB
testcase_45 AC 81 ms
17,368 KB
testcase_46 AC 39 ms
15,340 KB
testcase_47 AC 87 ms
17,388 KB
testcase_48 AC 105 ms
18,072 KB
testcase_49 AC 150 ms
20,104 KB
testcase_50 AC 131 ms
19,004 KB
testcase_51 AC 114 ms
18,496 KB
testcase_52 AC 25 ms
14,504 KB
testcase_53 AC 33 ms
14,956 KB
testcase_54 AC 138 ms
19,696 KB
testcase_55 AC 40 ms
15,424 KB
testcase_56 AC 179 ms
21,388 KB
testcase_57 AC 119 ms
18,848 KB
testcase_58 AC 207 ms
22,504 KB
testcase_59 AC 208 ms
22,504 KB
testcase_60 AC 199 ms
22,408 KB
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 5 ms
13,664 KB
testcase_64 AC 5 ms
13,668 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);
    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