#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;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 LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int n,k; int p[100010]; vector G[100010],R[100010]; bool going[100010],backing[100010]; vector cycle[100010]; vector cycle_S[100010]; int comp_idx[100010],cycle_idx[100010]; bool isCycle[100010]; ll ans[100010]; ll depth[100010]; int dfs(int s,vector &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 &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> 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 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 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(); }