Problem:
環状すごろく
ユキさんは、子供会の催しでみんなで遊べるようにすごろくを作りました。このすごろくでは、環状にマスが並んでいて、それぞれのマスには１以上の整数が書いてあります。
プレイヤーは出発点としてどこかのマスを選んで自分の駒を置きます。そのマスに書いてある数だけ、時計回りに駒を進めます。止まったマスに書いてある数だけ、再び時計回りに駒を進めます。これを繰り返して、出発点に選んだマスの上で駒が止まったら「あがり」です。
実際には、マスの選び方によっては絶対に「あがり」にならない場合もあります。ユキさんは、このすごろくで「あがり」にたどり着けるマスの個数を数えようとしています。
すごろくの情報を入力とし、「あがり」にたどり着けるマスの個数を報告するプログラムを作成せよ。
Input
入力は以下の形式で与えられる。
N
a
1
a
2
...
a
N
１行目にすごろくに含まれるすべてのマスの個数
N
(1 ≤
N
≤ 100000) が与えられる。２行目に、それぞれのマスに書かれた数
a
i
(1 ≤
a
i
≤ 10
9
) が、時計回りに順番に与えられる。
Output
「あがり」にたどり着けるマスの個数を１行に出力する。
Sample Input 1
3
1 1 1
Sample Output 1
3
Sample Input 2
3
1 1 2
Sample Output 2
2
Sample Input 3
8
2 3 7 3 3 3 4 4
Sample Output 3
6

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define ld long double
#define vec vector
#define vi vec<int>
#define vll vec<ll>
#define vld vec<ld>
#define pq priority_queue
#define rep(i, l, r) for(int i = (int)(l); i < (int)(r); i++)

template<class T>
bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}

template<class T>
bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

signed main() {
	int n; cin >> n;
	vi a(n); rep(i, 0, n) cin >> a[i];
	vi to(n); rep(i, 0, n) to[i] = (i + a[i]) % n;
	vi ok(n, -1);
	vec<bool> vis(n, false);
	rep(i, 0, n) {
		if(vis[i]) continue;
		int pos = i;
		while(true) {
			ok[pos]++;
			if(ok[pos] == 2) {ok[pos] = 1; break;}
			pos = to[pos];
			if(vis[pos]) break;
		}
		pos = i;
		while(true) {
			vis[pos] = true;
			pos = to[pos];
			if(vis[pos]) break;
		}
	}
	int ans = 0;
	rep(i, 0, n) if(ok[i] > 0) ans++;
	cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5276
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int N;
    cin>>N;
    vector<int> P(N);
    for(int i=0;i<N;i++){
        int a;
        cin>>a;
        P[i] = (i+a)%N;
    }
    for(int i=0;i<20;i++){
        vector<int> A(N);
        for(int j=0;j<N;j++) A[j] = P[P[j]];
        P = A;
    }
    set<int> s;
    for(int x:P) s.insert(x);
    cout<<s.size()<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 8316
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int N;
	cin >> N;
	vector<int> A(N);
	rep(i, 0, N) {
		cin >> A[i];
		A[i] %= N;
	}
	
	vector<int> res(N, -1);
	rep(i, 0, N) if (res[i] == -1) {
		vector<int> vis;
		set<int> S;
		int cur = i;
		vis.push_back(i);
		S.insert(i);
		
		while (true) {
			int next = (cur + A[cur]) % N;
			
			if (S.count(next)) {
				bool flag = false;
				for (int v : vis) {
					if (v == next) flag = true;
					if (flag) res[v] = 1;
					else res[v] = 0;
				}
				break;
			}
			
			if (res[next] != -1) {
				for (int v : vis) res[v] = 0;
				break;
			}
			
			vis.push_back(next);
			S.insert(next);
			cur = next;
		}
	}
	
	int ans = 0;
	rep(i, 0, N) if (res[i] == 1) ++ans;
	cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 8788
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

int main() {
    int N;
    cin >> N;
    vector<int> a(N);
    for(int i=0; i<N; ++i) {
        cin >> a[i];
    }
    vector<int> cnt(N, -1);
    vector<int> id(N, -1);
    int res = 0;
    for(int i=0; i<N; ++i) {
        if(cnt[i] == -1) {
            int sum = 0;
            int now = i;
            while(cnt[now] == -1) {
                cnt[now] = sum++;
                id[now] = i;
                (now += a[now]) %= N;
            }
            if(id[now] == i) {
                res += sum - cnt[now];
            }
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4172
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int n,ind = 0;
    cin >> n;
    vector<vector<int>> vec(n,vector<int>(3,-1));
    for(int i = 0; i < n; i++) vec[i][2] = 0;
    for(int i = 0; i < n; i++) cin >> vec[i][0];
    
    for(int i = 0; i < n; i++){
        if(vec[i][1] >= 0) continue;
        
        int p = i;
        bool isEnd = false;
        while(true){
            if(vec[p][1] == ind){
                vec[p][2] = 1;
                break;
            }else if(vec[p][1] >= 0){
                isEnd = true;
                break;
            }
            vec[p][1] = ind;
            p = (vec[p][0] % n + p) % n;
        }
        ind++;
        if(isEnd) continue;
        while(true){
            p = (vec[p][0] % n + p) % n;
            if(vec[p][2] == 0) vec[p][2] = 1;
            else break;
        }
    }
    int count = 0;
    for(int i = 0; i < n; i++) if(vec[i][2]) count++;
    cout << count << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 8616
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n, a, to[100010];
    bool alr[100010];
    bool ok[100010]={};
    queue<int> que;
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a;
        to[i] = (i+a)%n;
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(ok[i]) continue;
        int now=i;
        memset(alr, false, sizeof(alr));
        while(!alr[now]){
            que.push(now);
            alr[now] = true;
            now = to[now];
            if(ok[now]){
                while(!que.empty()) que.pop();
                alr[now] = true;
            }
        }
        while(!que.empty() && que.front()!=now) que.pop();
        while(!que.empty()){
            ok[que.front()] = true;
            que.pop();
            ans++;
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 280
memory_kb: 3992
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int a;
    cin>>a;
    long b[a];
    long c[a];
    for(int i=0;i<a;i+=1){
        cin>>b[i];
        c[i]=-1;
    }
    map<int,int> e;
    for(int i=0;i<a;i+=1){
        long t=i;
        if(!e.count(t)){
        while(!e.count(t)){
            e[t]=1;
            t=(b[t]+t)%a;
        }
        while(c[t]==-1){
            c[t]=1;
            t=(b[t]+t)%a;
        }
        t=i;
        while(c[t]==-1){
            c[t]=0;
            t=(b[t]+t)%a;
        }
        }
    }
    int ans=0;
    for(int i=0;i<a;i+=1){
        ans+=c[i];
        //cout<<c[i]<<endl;
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 9576
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll=long long;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep2(i,s,n) for(int i=int(s);i<int(n);i++)

template<typename T>
bool chmax(T &a,T b){
    return (a<b ? a=b,true:false);
}


int main(){
    cout << fixed << setprecision(20);
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n)cin >> a[i],a[i]%=n;
    vector<int> al(n,-1);
    int ans=0;
    rep(i,n)if(al[i]==-1){
        al[i]=0;
        int j=i;
        queue<int> seen_now;
        while(true){
            seen_now.emplace(j);
            if(al[(j+a[j])%n]==-2)break;
            else if(al[(j+a[j])%n]==-1){
                al[(j+a[j])%n]=al[j]+1;
                j+=a[j];
                j%=n;
                continue;
            }
            else{
                //数字が書いてある！
                ans+=al[j]-al[(j+a[j])%n]+1;
                break;
            }
        }
        while(!seen_now.empty()){
            al[seen_now.front()]=-2;
            seen_now.pop();
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4364
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class SCC{
private:
    vector<vector<int>> G;
    vector<vector<int>> rG;
    vector<bool> visited;
    vector<int> vs;
    vector<int> cmp;

    void dfs1(int v){
        visited[v] = true;
        for(auto &nex: G[v]){
            if(visited[nex]) continue;
            dfs1(nex);
        }
        vs.emplace_back(v);
    }

    void dfs2(int v, int k){
        visited[v] = true;
        cmp[v] = k;
        for(auto &nex: rG[v]){
            if(visited[nex]) continue;
            dfs2(nex, k);
        }
    }
public:
    SCC(int n):
    G(vector<vector<int>>(n)), rG(vector<vector<int>>(n)),
    visited(vector<bool>(n, false)), cmp(vector<int>(n)) {}

    void add_edge(int from, int to){
        G[from].emplace_back(to);
        rG[to].emplace_back(from);
    }

    vector<vector<int>> scc(){
        for(int i = 0; i < G.size(); i++){
            if(!visited[i]) dfs1(i);
        }

        fill(visited.begin(), visited.end(), false);
        reverse(vs.begin(), vs.end());
        int k = 0;
        for(auto &it: vs){
            if(visited[it]) continue;
            dfs2(it, k);
            k++;
        }

        vector<vector<int>> res(k);
        for(int i = 0; i < G.size(); i++) res[cmp[i]].emplace_back(i);
        return res;
    }
};

int main(){
    int n; cin >> n;
    vector<int> a(n);
    for(auto &it: a) cin >> it;
    SCC G(n);
    for(int i = 0; i < n; i++){
        G.add_edge(i, (i+a[i])%n);
    }

    auto res = G.scc();
    int ans = 0;
    for(auto vec: res){
        if(vec.size() > 1) ans += vec.size();
    }
    for(auto &it: a){
        if(it%n == 0) ans++;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 22124
final_rank: 19

code_snippet: #ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define unless(x) if(!(x))
#define until(x) while(!(x))
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)

/*---------type/const---------*/
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
    constexpr operator int(){return -int(1e9)-10;}
    constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
    constexpr operator int(){return int(1e9)+10;}
    constexpr operator ll(){return ll(1e18)+10;}
    constexpr auto operator -(){return neginf;}
}inf;

/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif

/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}



int main(){
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);
    std::cout.precision(10);
/*------------------------------------*/
    
    int n;
    cin >> n;
    vector<int> g(n);
    rep(i,n){
        int a;
        cin >> a;
        g[i]=(i+a)%n;
    }
    int ans=0;
    vector<int> d(n);
    //0:まだ 1:調査中 2:済み
    vector<int> seen(n);
    auto dfs=[&](int v,auto &dfs)->void{
        if(seen[g[v]]==1){
            ans+=d[v]-d[g[v]]+1;
        }
        else if(seen[g[v]]==0){
            seen[v]=1;
            d[g[v]]=d[v]+1;
            dfs(g[v],dfs);
        }
        seen[v]=2;
        return;
    };
    rep(i,n)if(seen[i]==0){
        dfs(i,dfs);
    }
    cout << ans << newl;
}
/*
無向グラフに直して閉路検出すれば良い dfsだと楽
*/
accuracy: 1
time_ms: 20
memory_kb: 7268
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

int s[100005];
int to[100005];
bool c[100005];
int dfs(int u){
  if(s[u]==2)return 2;
  if(s[u]==1){s[u]=2; return 3;}
  s[u]=1;
  int res=dfs(to[u]);
  if(res==2){s[u]=2; return 2;}
  if(res==3){
    c[u]=true;
    if(s[u]==2)return 2;
    else {
      s[u]=2;
      return 3;
    }
  }
}

int main(){
  int n; cin>>n;
  vector<int> a(n); for(int i=0; i<n; i++) cin>>a[i], to[i]=(i+a[i])%n;
  int ans=0;
  for(int i=0; i<n; i++) {
    dfs(i);
    // cout<<c[i]<<" ";
    ans+=c[i];
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5544
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define repp(i,n,m) for (int i = m; i < int(n); ++i)
#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)
#define fi first
#define se second
using namespace std;

const string alp = "abcdefghijklmnopqrstuvwxyz";
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<long double, long double>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
const int INF = 1001001007;
const long long mod1 = 1000000007LL;
const long long mod2 = 998244353LL;
const ll inf = 2e18;
const ld pi = 3.14159265358979323;
const ld eps = 1e-7;
template<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}
template<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}
template<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
void dout(ld h=-1.23456789){cout<<setprecision(20);if(abs(h+1.23456789)>eps)cout<<h<<endl;}
template<typename T,typename U> void pout(pair<T,U> p){cout<<p.fi<<" "<<p.se<<endl;}

struct vs{
    vector<int> to;
};

int main(){
    int n; cin >> n;
    vector<int> ar(n);
    rep(i,n) cin >> ar[i], ar[i] %= n;
    int ans = 0;
    map<int,int> mp;
    set<int> st;
    int ti = 0;
    rep(i,n) st.insert(i);
    while (!st.empty()){
        int p = *st.begin();
        st.erase(p);
        mp[p] = ti;
        int ima = p;
        while (true){
            int nex = (ima + ar[ima]) % n;
            ti++;
            if (mp.find(nex) == mp.end()){
                mp[nex] = ti;
                st.erase(nex);
                ima = nex;
            }
            else {
                if (mp[nex] != -1){
                    ans += ti - mp[nex];
                }
                ima = p;
                mp[p] = -1;
                while (true){
                    int tug = (ima + ar[ima]) % n;
                    if (mp[tug] != -1) mp[tug] = -1, ima = tug;
                    else break;
                }
                break;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 8136
final_rank: 15

code_snippet: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }
template<typename T=ll,size_t N>
array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

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

	auto N=in();
	vll V=vecin(N);
	vll F(N,-1);
	REP(i,N){
		if(F[i]>=0) continue;

		set<ll> check;
		vll history;

		check.emplace(i);
		history.push_back(i);
		ll pos = i;
		while(true){
			auto d = (pos+V[pos])%N;
			if(F[d]>=0){
				for(auto& x:history){
					F[x] = 0;
				}
				break;
			}

			if(check.find(d)==check.end()){
				check.emplace(d);
				history.push_back(d);
			}
			else{
				if(d==i){
					for(auto& x:history){
						F[x] = 1;
					}
				}
				else{
					ll failed = 0;
					REP(j,history.size()){
						failed = j;
						if(history[j]==d) break;
						F[history[j]] = 0;
					}
					for(int j=failed;j<history.size();j++){
						F[history[j]] = 1;
					}
				}
				break;
			}
			pos = d;
		}
	}
	out(count(ALL(F),1));
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10136
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n, 0);
    for(int i=0; i<n; i++){
        cin >> a[i];
        a[i] = (a[i]+i) %n;
        b[a[i]]++;
    }
    vector<int> wait;
    for(int i=0; i<n; i++){
        if(b[i] == 0){
            wait.push_back(i);
        }
    }
    int ans = n;
    while(!wait.empty()){
        int c = wait.back();
        wait.pop_back();
        ans--;
        b[a[c]]--;
        if(b[a[c]] == 0){
            wait.push_back(a[c]);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4056
final_rank: 4

code_snippet: #include<iostream>
using namespace std;
int N,A[1<<17];
bool vis[1<<17],sed[1<<17];
main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		A[i]=(i+A[i])%N;
	}
	int ans=0;
	for(int i=0;i<N;i++)if(!vis[i])
	{
		int t=i;
		do{
			vis[t]=true;
			t=A[t];
		}while(!vis[t]);
		if(!sed[t])
		{
			int u=t;
			do{
				sed[u]=true;
				u=A[u];
				ans++;
			}while(u!=t);
		}
		int u=i;
		while(!sed[u])
		{
			sed[u]=true;
			u=A[u];
		}
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3684
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=1e5;
const int inf=1e9;
int g[siz];
int visit[siz] = {};
int ans = 0;
void dfs(int v){
	if(visit[g[v]] == visit[v]){
        int u = v;
        do{
            u = g[u];
            ans++;
        }while(u != v);
		return;
	}
	if(visit[g[v]] == 0){
        visit[g[v]] = visit[v];
        dfs(g[v]);
    }
}
int main(){
	int N; cin>>N;
	rep(i,0,N){
        cin>>g[i];
        g[i] = (i + g[i]) % N;
    }
	rep(i,0,N)if(!visit[i]){ visit[i] = i+1; dfs(i); }
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3868
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N;
vector<int> A;
vector<int> F;

int main() {
 cin>>N;
 A.resize(N);
 rep(i,N){ cin>>A[i]; A[i] = (i+A[i])%N; }

 F.assign(N,-1);
 rep(s,N){
  if(F[s]!=-1) continue;
  int p=s;
  while(F[p]==-1){ F[p]=s; p=A[p]; }
  if(F[p]!=s) continue;
  while(F[p]!=N){ F[p]=N; p=A[p]; }
 }

 int ans=0;
 rep(i,N) if(F[i]==N) ans++;
 cout<<ans<<endl;
 return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3640
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
vector<int> a[100005];
int dfs(int d,int c){
  int u=0;
  for(int i=0;i<a[d].size();i++){
    if(a[d][i]==c){
      return 1;
    }
    else{
      u=dfs(a[d][i],c);
      if(u==1) break;
    }
  }
  return u;
}
signed main(){
  int n,b,c=0,sum=0;
  map<int,int> d; 
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>b;
    if((b+i)%n!=i) a[(b+i)%n].push_back(i);
    else sum++;
    d[(b+i)%n]++;
  }
  if(d.size()==n) sum=n;
  else{
    for(int i=0;i<n;i++){
      sum+=dfs(i,i);
    }
  }
  cout<<sum<<"\n";
  return 0;
}
accuracy: 1
time_ms: 590
memory_kb: 13220
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

int N;
int DataInfo[100000];
int User[100000];

int solve() {
	int curPos, tmp, sum;

	sum = 0;
	for (int i = 0; i < N; ++i) {
		if (User[i] != 0) {
			continue;
		}
		curPos = i;
		while (User[curPos] == 0) {
			User[curPos] = i + 1;
			curPos = (curPos + DataInfo[curPos]) % N;
		}
		if (User[curPos] != i + 1) {
			continue;
		}
		tmp = curPos;
		do {
			++sum;
			curPos = (curPos + DataInfo[curPos]) % N;
		} while (curPos != tmp);
	}

	return sum;
}

int main() {
	
	cin >> N;
	for (int i = 0; i < N; ++i) {
		cin >> DataInfo[i];
	}
	cout << solve() << endl;

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3860
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

struct SCC {
    vector<vector<int>> G, G_r, G_out;
    vector<int> vs, used, cmp, sz;
    SCC() = default;
    explicit SCC(int n) : G(n), G_r(n), G_out(n), used(n), cmp(n), sz(n) {}

    void add_edge(int a, int b){
        G[a].emplace_back(b);
        G_r[b].emplace_back(a);
    }

    void dfs(int v){
        used[v] = 1;
        for (auto &&u : G[v]) if(!used[u]) dfs(u);
        vs.emplace_back(v);
    }

    void dfs_r(int v, int k){
        used[v] = 1;
        cmp[v] = k;
        sz[k]++;
        for (auto &&u : G_r[v]) if(!used[u]) dfs_r(u, k);
    }

    int build() {
        int n = G.size();
        for (int i = 0; i < n; ++i) if(!used[i]) dfs(i);
        fill(used.begin(),used.end(), 0);
        int k = 0;
        for (int i = n - 1; i >= 0; --i) {
            if(!used[vs[i]]){
                dfs_r(vs[i], k++);
            }
        }
        G_out.resize(k);
        sz.resize(k);
        for (int i = 0; i < n; ++i) {
            for (auto &&j : G[i]) {
                if(cmp[i] != cmp[j]){
                    G_out[cmp[i]].emplace_back(cmp[j]);
                }
            }
        }
        for (auto &&l : G_out) {
            sort(l.begin(), l.end());
            l.erase(unique(l.begin(), l.end()), l.end());
        }
        return k;
    }

    int operator[](int k) const { return cmp[k]; }
};


int main() {
    int n;
    cin >> n;
    SCC G(n);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if(x%n == 0) ans++;
        else G.add_edge(i, (i+x)%n);
    }
    G.build();

    for (auto &&j : G.sz) {
        if(j != 1) ans += j;
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 20332
final_rank: 18