malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   const int maxn = 1e5 + 100;   const double eps = 1e-7;   const int INF = 1e9;   using namespace std;   bool f[maxn];   int a[maxn];   struct Node {     int i, num;     bool operator<(const Node &rhs) const {       return i < rhs.i || (i == rhs.i && num < rhs.num);     }   };   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       f[i] = true;       a[i] = i;     }     set<Node> S1, S2;     set<Node>::iterator it, it1;     int cnt = n;     char key[10];     int id;     for (int i = 1; i <= m; i++) {       scanf( %s %d , key, &id);       if (key[0] ==  + ) {         int d = S2.erase((Node){a[id], id});         if (S2.size() && d == 0) f[id] = false;         if (S1.size()) f[id] = false;         a[id] = ++cnt;         S1.insert((Node){a[id], id});         for (it = S2.begin(); it != S2.end(); it++) {           f[it->num] = false;         }         S2.clear();       } else {         it1 = it = S1.upper_bound((Node){a[id], id});         if (it != S1.end()) {           f[id] = false;           f[it->num] = false;         }         S1.erase((Node){a[id], id});         for (it = S2.begin(); it != S2.end(); it++) {           f[it->num] = false;         }         S2.clear();         S2.insert((Node){a[id], id});       }     }     int num = 0;     for (int i = 1; i <= n; i++)       if (f[i]) num++;     cout << num << endl;     for (int i = 1; i <= n; i++)       if (f[i]) cout << i <<    ;     cout << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e6 + 2;   bool isPrime[maxn];   vector<int> primeNumbers;   int smallestPrimeDiv[maxn];   const int maxn2 = 1e5 + 2;   int a[maxn2];   int b[maxn2];   bool primeUsed[maxn];   void sieve(int n) {     for (int i = 1; i <= n; ++i) {       isPrime[i] = 1;       smallestPrimeDiv[i] = i;     }     for (int i = 2; i * i <= n; ++i) {       if (isPrime[i]) {         for (int j = i * i; j <= n; j += i) {           isPrime[j] = 0;           smallestPrimeDiv[j] = i;         }       }     }     for (int i = 2; i < n; ++i) {       if (isPrime[i]) primeNumbers.push_back(i);     }   }   set<int> primeFactorsOf(int x) {     set<int> ans;     while (x > 1 && (x % smallestPrimeDiv[x] == 0)) {       ans.insert(smallestPrimeDiv[x]);       x /= smallestPrimeDiv[x];     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     sieve(maxn - 2);     int n;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     int i;     for (i = 0; i < n; ++i) {       set<int> pf = primeFactorsOf(a[i]);       bool pos = 1;       for (int pi : pf) {         if (primeUsed[pi]) {           pos = 0;           break;         }       }       if (!pos)         break;       else {         for (int pi : pf) {           primeUsed[pi] = 1;         }         b[i] = a[i];       }     }     if (i < n) {       int x = a[i];       while (1) {         x++;         set<int> pf = primeFactorsOf(x);         bool pos = 1;         for (int pi : pf) {           if (primeUsed[pi]) {             pos = 0;             break;           }         }         if (!pos) continue;         b[i] = x;         for (int pi : pf) {           primeUsed[pi] = 1;         }         break;       }     }     i++;     for (int j = 0; i < n; ++i) {       while (primeUsed[primeNumbers[j]]) j++;       b[i] = primeNumbers[j];       primeUsed[primeNumbers[j]]++;     }     for (int i = 0; i < n; ++i) cout << b[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a;   int b = 1;   bool check(long long x) {     if (x < 0) x = -x;     while (x) {       if (x % 10 == 8) return 1;       x /= 10;     }     return 0;   }   int main() {     scanf( %lld , &a);     while (!check(a + b)) ++b;     printf( %d , b);   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000005;   const int inf = 0x3f3f3f3f;   long long a[maxn];   int q[maxn];   int main() {     int n;     cin >> n;     long long p = 0;     long long pans = 0;     for (int i = (1); i < (n + 1); i++) {       cin >> a[i];       pans += abs(a[i] - i);       if (i < a[i]) {         p++;         q[a[i] - i]--;       }     }     long long ans1 = pans, ans2 = 0;     int cur = n;     for (int i = (1); i < (n + 1); i++) {       pans -= p;       pans += n - p - 1;       pans = pans - abs(a[cur] - n) + abs(a[cur] - 1);       if (ans1 > pans) ans1 = pans, ans2 = i;       p += q[i];       if (1 < a[cur]) {         p++;         q[a[cur] - 1 + i]--;       }       cur--;     }     cout << ans1 <<     << ans2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 7;   const long long mod = 1e9 + 7;   const long long INF = 1e9 + 7;   const long long mlog = 22;   const long long SQ = 400;   long long T[maxn * 4];   long long a[maxn], b[maxn];   long long x[maxn], y[maxn], k[maxn];   void add(long long L, long long R, long long col, long long l, long long r,            long long id) {     if (r == R && l == L) {       T[id] = col;       return;     }     long long mid = (l + r) / 2;     if (L < mid) add(L, min(mid, R), col, l, mid, 2 * id + 0);     if (R > mid) add(max(L, mid), R, col, mid, r, 2 * id + 1);   }   long long get(long long idx, long long l, long long r, long long id) {     if (r - l <= 1) return T[id];     long long mid = (l + r) / 2;     if (idx < mid)       return max(T[id], get(idx, l, mid, 2 * id + 0));     else       return max(T[id], get(idx, mid, r, 2 * id + 1));   }   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(), cout.tie();     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= n; i++) cin >> a[i];     for (long long i = 1; i <= n; i++) cin >> b[i];     long long t;     for (long long i = 1; i <= m; i++) {       cin >> t;       if (t == 1) {         cin >> x[i] >> y[i] >> k[i];         add(y[i], y[i] + k[i], i, 1, n + 1, 1);       } else {         long long index;         cin >> index;         long long next = get(index, 1, n + 1, 1);         if (next == 0)           cout << b[index] <<   n ;         else           cout << a[x[next] + index - y[next]] <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, w, h;   set<int> Set[2];   set<int>::iterator iter;   priority_queue<int> qu[2];   int vis[2][200010], point[2][200010];   char s[10];   int main() {     int i, j, k, l, r, p, a, b;     long long ans;     scanf( %d%d%d , &w, &h, &k);     Set[0].insert(0);     Set[0].insert(h);     Set[1].insert(0);     Set[1].insert(w);     point[0][h] = 0;     point[1][w] = 0;     qu[0].push(h);     qu[1].push(w);     for (i = 1; i <= k; i++) {       scanf( %s%d , s, &p);       if (s[0] ==  H ) {         iter = Set[0].lower_bound(p);         r = *iter;         l = point[0][r];         vis[0][r - l]++;         qu[0].push(r - p);         qu[0].push(p - l);         point[0][r] = p;         point[0][p] = l;         Set[0].insert(p);       } else {         iter = Set[1].lower_bound(p);         r = *iter;         l = point[1][r];         vis[1][r - l]++;         qu[1].push(r - p);         qu[1].push(p - l);         point[1][r] = p;         point[1][p] = l;         Set[1].insert(p);       }       while (true) {         a = qu[0].top();         if (vis[0][a] == 0) break;         vis[0][a]--;         qu[0].pop();       }       while (true) {         b = qu[1].top();         if (vis[1][b] == 0) break;         vis[1][b]--;         qu[1].pop();       }       ans = (long long)a * (long long)b;       printf( %I64d n , ans);     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   const int mod = 1e9 + 7;   vector<int> vec[350];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     int k = 1;     for (int i = 1; i <= n; ++i) {       if (i & 1) {         for (int j = 1; j <= n; ++j) vec[j].push_back(k++);       } else {         for (int j = n; j >= 1; j--) vec[j].push_back(k++);       }     }     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) printf( %d%c , vec[i][j - 1],    n [j == n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Tp>   inline void outarr(Tp _begin, Tp _end, const char* _delim =    ) {     for (Tp current = _begin; current != _end; ++current) {       std::cout << *current << _delim;     }     std::cout <<   n ;   }   using ll = long long;   using pii = std::pair<int, int>;   constexpr int INF = 0x3f3f3f3f;   constexpr int MOD = static_cast<const int>(1e9 + 7);   constexpr int g_mod = 998244353;   int arr[200000];   int fact[200001];   int ifact[200001];   int Add(int x, int y) {     x += y;     return x < g_mod ? x : x - g_mod;   }   int Sub(int x, int y) {     x -= y;     return x >= 0 ? x : x + g_mod;   }   int Mul(int x, int y) { return int(1LL * x * y % g_mod); }   int FastPow(int x, int p) {     int res = 1;     while (p > 0) {       if (p & 1) {         res = Mul(res, x);       }       p >>= 1;       x = Mul(x, x);     }     return res;   }   int Inv(int x) { return FastPow(x, g_mod - 2); }   void Init(int n) {     fact[0] = 1;     for (int i = 1; i <= n; ++i) {       fact[i] = Mul(i, fact[i - 1]);     }     ifact[n] = Inv(fact[n]);     for (int i = n - 1; i >= 0; --i) {       ifact[i] = Mul(i + 1, ifact[i + 1]);     }   }   int C(int k, int n) { return Mul(fact[n], Mul(ifact[k], ifact[n - k])); }   int Func(int len) {     if (len & 1) {       return 0;     }     return C(len >> 1, len);   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     int n, k;     cin >> n >> k;     Init(n);     for (int i = 0; i < (n); ++i) {       cin >> arr[i];     }     int len = distance(arr, unique(arr, arr + n));     if (arr[0] == arr[len - 1]) {       --len;     }     if (len == 0) {       return cout << (0) <<   n , 0;       ;     }     const int inv2 = Inv(2);     int k2 = (k - 2) % g_mod;     int ans = 0;     for (int i = 0; i < (len); ++i) {       int cur =           Mul(Sub(FastPow(2, len - i - 1), Mul(Func(len - i), inv2)), C(i, len));       ans = Add(ans, Mul(FastPow(k2, i), cur));     }     ans = Mul(ans, FastPow(k % g_mod, n - len));     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long fastxp(long long b, long long e) {     if (e == 1)       return b;     else {       long long ans = fastxp(b, e / 2);       ans = (ans * ans) % 998244353;       if (e % 2 == 1) {         ans = (ans * b) % 998244353;       }       return ans;     }   }   int main() {     ios::sync_with_stdio(false);     long long i, j, l, r;     cin >> i >> j >> l >> r;     long long v = fastxp(r - l + 1, i * j);     if ((i * j) % 2 == 1) {       cout << v << endl;     } else {       if ((r - l) % 2 == 1) {         cout << (v * fastxp(2, 998244353 - 2)) % 998244353 << endl;       } else {         cout << ((v + 1) * fastxp(2, 998244353 - 2)) % 998244353 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());   const int N = 3e5 + 7;   const int LG = 19;   int go[N][LG];   int a[N];   int last[LG];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, q;     cin >> n >> q;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int j = 0; j < LG; j++) {       go[n][j] = n;       last[j] = n;     }     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j < LG; j++) {         go[i][j] = n;       }       for (int j = 0; j < LG; j++) {         if ((a[i] >> j) & 1) {           for (int k = 0; k < LG; k++) {             go[i][k] = min(go[i][k], go[last[j]][k]);           }           last[j] = i;           go[i][j] = i;         }       }     }     for (int i = 0; i < q; i++) {       int x, y;       cin >> x >> y;       x--, y--;       bool good = false;       for (int j = 0; j < LG; j++) {         good |= (((a[y] >> j) & 1) && go[x][j] <= y);       }       cout << (good ?  Shi  :  Fou ) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int m, n, k, l = 9999, j = 9999, s, i;     cin >> n >> m >> k;     int a[n + 3];     for (i = 1; i <= n; i++) cin >> a[i];     for (i = m + 1; i <= n; i++) {       if (a[i] != 0 && a[i] <= k) {         l = (i - m);         break;       }     }     for (i = m - 1; i >= 1; i--) {       if (a[i] != 0 && a[i] <= k) {         j = (m - i);         break;       }     }     s = min(j, l);     cout << s * 10;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 9;   const long long maxn = 3e2 + 9;   long long a[maxn][maxn];   pair<long long, long long> q[N];   long long front, rear;   long long dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   bool vis[maxn][maxn];   void solve() {     memset(vis, 0, sizeof(vis));     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= m; j++) {         cin >> a[i][j];       }     }     if (a[1][1] > 2 || a[n][m] > 2 || a[1][m] > 2 || a[n][1] > 2) {       cout <<  NO  << endl;       return;     }     a[1][1] = a[n][m] = a[1][m] = a[n][1] = 2;     for (long long i = 2; i <= n - 1; i++) {       if (a[i][1] > 3 || a[i][m] > 3) {         cout <<  NO  << endl;         return;       } else {         a[i][1] = a[i][m] = 3;       }     }     for (long long i = 2; i <= m - 1; i++) {       if (a[1][i] > 3 || a[n][i] > 3) {         cout <<  NO  << endl;         return;       } else {         a[1][i] = a[n][i] = 3;       }     }     for (long long i = 2; i <= n - 1; i++) {       for (long long j = 2; j <= m - 1; j++) {         if (a[i][j] > 4) {           cout <<  NO  << endl;           return;         } else {           a[i][j] = 4;         }       }     }     cout <<  YES  << endl;     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= m; j++) {         cout << a[i][j] <<    ;       }       cout << endl;     }   }   signed main() {     long long _;     cin >> _;     while (_--) solve();   }
#include <bits/stdc++.h>   using namespace std;   map<long long, long long> m;   map<long long, long long>::iterator it;   long long ans[200500];   int main() {     long long n, i, x, y, pr = -1, cur = 0;     cin >> n;     for (i = 0; i < n; i++) {       cin >> x >> y;       m[x]++;       m[y + 1]--;     }     for (it = m.begin(); it != m.end(); it++) {       if (it->second < 0) {         ans[cur] += (it->first - pr);       } else {         if (cur != 0) {           ans[cur] += (it->first - pr);         }       }       cur += it->second;       pr = it->first;     }     for (i = 1; i <= n; i++) {       cout << ans[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int dp[maxn][maxn];   int n, ans;   int tim[maxn];   int dfs(int i, int j) {     if (dp[i][j]) return dp[i][j];     int ti = tim[i], tj = tim[j], tk = tim[j + 1];     if (j == n + 1) return dp[i][j] = ti;     if (j == n) return dp[i][j] = max(ti, tj);     int choseij = max(ti, tj) + dfs(j + 1, j + 2),         choseik = max(ti, tk) + dfs(j, j + 2),         chosejk = max(tj, tk) + dfs(i, j + 2);     dp[i][j] = min(min(choseij, choseik), chosejk);     return dp[i][j];   }   void print_ans(int i, int j) {     int ti = tim[i], tj = tim[j], tk = tim[j + 1];     if (j == n + 1) {       cout << i << endl;       return;     }     if (j == n) {       cout << i <<     << j << endl;       return;     }     int choseij = max(ti, tj) + dp[j + 1][j + 2],         choseik = max(ti, tk) + dp[j][j + 2],         chosejk = max(tj, tk) + dp[i][j + 2];     if (dp[i][j] == choseij) {       cout << i <<     << j << endl;       print_ans(j + 1, j + 2);       return;     }     if (dp[i][j] == choseik) {       cout << i <<     << j + 1 << endl;       print_ans(j, j + 2);       return;     }     if (dp[i][j] == chosejk) {       cout << j <<     << j + 1 << endl;       print_ans(i, j + 2);       return;     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> tim[i];     ans = dfs(1, 2);     cout << ans << endl;     print_ans(1, 2);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   long long A[N];   int n;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> A[i];     int ans = 1;     int len = 1;     for (int i = 1; i < n; i++) {       if (A[i] <= 2 * A[i - 1])         len++, ans = max(len, ans);       else         len = 1;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char a[3][3];     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++) cin >> a[i][j];     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++)         if (a[i][j] != a[2 - i][2 - j]) {           cout <<  NO ;           return 0;         }     cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 205;   long long dp[maxn][maxn][maxn];   long long n;   string s, t;   long long recur(long long pos, long long changes, long long sum) {     if (pos == n) return 0;     if (dp[pos][changes][sum] != -1) return dp[pos][changes][sum];     long long res = recur(pos + 1, changes, sum);     if (s[pos] == t[0]) res = max(res, recur(pos + 1, changes, sum + 1));     if (s[pos] == t[1]) res = max(res, recur(pos + 1, changes, sum) + sum);     if (s[pos] == t[0] && s[pos] == t[1])       res = max(res, recur(pos + 1, changes, sum + 1) + sum);     if (changes > 0) {       res = max(res, recur(pos + 1, changes - 1, sum + 1));       res = max(res, recur(pos + 1, changes - 1, sum) + sum);       if (t[0] == t[1])         res = max(res, recur(pos + 1, changes - 1, sum + 1) + sum);     }     dp[pos][changes][sum] = res;     return res;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (long long i = 0; i < maxn; i++)       for (long long j = 0; j < maxn; j++)         for (long long k = 0; k < maxn; k++) dp[i][j][k] = -1;     long long k;     cin >> n >> k >> s >> t;     long long res = recur(0, k, 0);     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     double x, y, t, val;   } pnt[2050];   struct Edge {     int u, v, next;   } edge[2050 * 2050];   int head[2050], tot = 0;   void add(int u, int v) {     edge[tot].u = u;     edge[tot].v = v;     edge[tot].next = head[u];     head[u] = tot++;   }   double Dist(node x, node y) {     return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));   }   int ru[2050];   double Max(double x, double y) {     if (x > y) return x;     return y;   }   int mark[2050];   int n;   double dis[2050];   double spfa(int x) {     for (int i = 1; i <= n; i++) {       mark[i] = false;       dis[i] = 0;     }     queue<int> Q;     Q.push(x);     dis[x] = pnt[x].val;     mark[x] = true;     while (!Q.empty()) {       int k = Q.front();       Q.pop();       mark[k] = false;       for (int i = head[k]; i != -1; i = edge[i].next) {         if (dis[edge[i].v] < dis[k] + pnt[edge[i].v].val) {           dis[edge[i].v] = dis[k] + pnt[edge[i].v].val;           if (mark[edge[i].v] == false) {             Q.push(edge[i].v);             mark[edge[i].v] = true;           }         }       }     }     double maxn = dis[x];     for (int i = 1; i <= n; i++) {       if (dis[i] > maxn) {         maxn = dis[i];       }     }     return maxn;   }   int main() {     int i, j;     while (scanf( %d , &n) != EOF) {       for (i = 1; i <= n; i++) {         scanf( %lf%lf%lf%lf , &pnt[i].x, &pnt[i].y, &pnt[i].t, &pnt[i].val);       }       memset(head, -1, sizeof(head));       memset(ru, 0, sizeof(ru));       tot = 0;       for (i = 1; i <= n; i++) {         for (j = 1; j <= n; j++) {           if (i == j) continue;           if (Dist(pnt[i], pnt[j]) + pnt[i].t <= pnt[j].t) {             add(i, j);             ru[j]++;           }         }       }       double maxn = 0;       for (i = 1; i <= n; i++) {         if (ru[i] == 0) {           maxn = Max(maxn, spfa(i));         }       }       printf( %.7f n , maxn);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool sorting(const pair<long long int, long long int>& a,                const pair<long long int, long long int>& b) {     if (a.first != b.first) {       return (a.first > b.first);     } else       return (!(a.second >= b.second));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, k;     cin >> n >> k;     pair<long long int, long long int> v[n];     for (int i = 0; i < n; ++i) {       cin >> v[i].first >> v[i].second;     }     sort(v, v + n, sorting);     long long int ans = 0;     for (int i = 0; i < n; ++i) {       if (v[k - 1].first == v[i].first && v[k - 1].second == v[i].second) {         ans++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int x1, y1;   } ar[500101];   bool cmp(node &b1, node &b2) {     if (b1.x1 == b2.x1) return b1.y1 < b2.y1;     return b1.x1 < b2.x1;   }   int main() {     long long q, i, j, k, v, c = 0, p1, p2, t, n, x, y, p;     cin >> n;     for (i = 0; i < n; i++) {       cin >> ar[i].x1 >> ar[i].y1;     }     sort(ar, ar + n, cmp);     for (i = 0; i < n; i++) {       if (i == 0) {         p = min(ar[i].x1, ar[i].y1);       } else {         p1 = min(ar[i].x1, ar[i].y1);         if (p1 >= p) {           p = p1;         } else if (ar[i].x1 > ar[i].y1) {           p = ar[i].x1;         } else {           p = ar[i].y1;         }       }     }     cout << p << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int k, res;   string s;   int cs(int x) {     int dem(0);     while (x != 0) {       x /= 10;       dem++;     }     return dem;   }   int main() {     int t;     cin >> t;     while (t--) {       cin >> k;       res = 0;       int ii;       for (int i = 1; i <= 100000000; ++i) {         ii = i;         res += cs(i);         if (k <= res) break;         k -= res;       }       res = 0;       for (int i = 1; i <= ii; ++i) {         res += cs(i);         if (res >= k) {           res -= cs(i);           ii = i;           break;         }       }       ostringstream convert;       convert.clear();       convert << ii;       s = convert.str();       cout << s[k - res - 1] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int nmax = 100005;   int n, cnt, one, two1, two2, p[nmax];   vector<int> v[nmax];   bitset<nmax> viz;   int main() {     cin.sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++)       if (!viz[i]) {         cnt++;         for (int j = i; !viz[j]; j = p[j]) {           viz[j] = 1;           v[cnt].push_back(j);         }       }     for (int i = 1; i <= n; i++)       if (v[i].size() == 1) {         one = v[i][0];         break;       }     if (one) {       printf( YES n );       for (int i = 1; i <= n; i++)         if (i != one) printf( %d %d n , i, one);       return 0;     }     for (int i = 1; i <= n; i++)       if (v[i].size() == 2) {         two1 = v[i][0];         two2 = v[i][1];         break;       }     if (two1) {       for (int i = 1; i <= cnt; i++)         if (v[i].size() & 1) {           printf( NO n );           return 0;         }       printf( YES n );       printf( %d %d n , two1, two2);       for (int i = 1; i <= cnt; i++)         if (v[i][0] != two1)           for (int j = 0; j < (int)v[i].size(); j++) {             if (j & 1)               printf( %d %d n , two1, v[i][j]);             else               printf( %d %d n , two2, v[i][j]);           }       return 0;     }     printf( NO n );     return 0;   }