fork download
  1. #include <iostream>
  2. #define endl '\n'
  3. typedef long long ll;
  4. using namespace std;
  5. const int MAX = 200000;
  6. int idx, n, a[MAX], trace[MAX], d, tIdx;
  7. ll k;
  8.  
  9. int main()
  10. {
  11. ios::sync_with_stdio(false);
  12. cin.tie(NULL);
  13. cin >> n >> k;
  14. for (int i = 0; i < n; ++i)
  15. cin >> a[i];
  16.  
  17. int rptFound = 0;
  18. for (ll i = 0; i < k; i++)
  19. {
  20. d = a[idx];
  21. if (i > 0 && rptFound == 0)
  22. {
  23. for (ll j = 0; j < tIdx; j++)
  24. {
  25. if (d == trace[j])
  26. {
  27. rptFound = 1;
  28. ll rpt = i - j;
  29. i = (k - 1) - ((k - 1 - i) % rpt);
  30. break;
  31. }
  32. }
  33. }
  34. if (!rptFound)
  35. trace[tIdx++] = d;
  36. idx = d - 1;
  37. }
  38. cout << d << endl;
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 4496KB
stdin
5 7
4 1 2 5 1
stdout
4