結果

問題 No.3075 Mex Recurrence Formula
ユーザー 👑 bo9chan
提出日時 2025-03-28 21:53:50
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 841 ms / 2,000 ms
コード長 34,064 bytes
コンパイル時間 2,924 ms
コンパイル使用メモリ 234,272 KB
実行使用メモリ 31,360 KB
最終ジャッジ日時 2025-03-28 21:54:16
合計ジャッジ時間 21,230 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#ifdef __LOCAL
#include <mytemplate.hpp> // ~/local/include/mytemplate.hpp.gch
#else
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#endif
// MARK: ALIASES
using namespace std;
// #pragma GCC target "no-avx" // gcc12.2boost使
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
template <class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
constexpr ll INF = LONG_LONG_MAX / 2 - 10000LL; // 4,611,686,018,427,377,903 ~= 4.6e+18, 19
const double PI = acos(-1);
// MARK: MACRO
#define REP1(i, n) REP3(i, 0, n, 1)
#define REP2(i, s, n) REP3(i, s, n, 1)
#define REP3(i, s, n, d) for (ll i = (ll)(s); i < (ll)(n); i += (d))
#define REP_OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME
#define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__)
#define DEP1(i, n) DEP3(i, n, -1, 1)
#define DEP2(i, n, s) DEP3(i, n, s, 1)
#define DEP3(i, n, s, d) for (ll i = (ll)(n); (ll)(s) < i; i -= (d))
#define DEP_OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME
#define dep(...) DEP_OVERLOAD(__VA_ARGS__, DEP3, DEP2, DEP1)(__VA_ARGS__)
#define fore(e, a) for (auto&& e : (a))
#define len(a) (ll)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define th third
#define fo fourth
// MARK: INT128
using int128 = __int128_t;
// 128bit. LONG_LONG_MAX/LONG_LONG_MINOK.
istream& operator>>(istream& stream, int128& val) {
string s;
stream >> s;
assert(s.size() > 0);
val = 0;
bool is_minus = false;
for (int i = 0; i < (int)s.size(); i++) {
if (i == 0 && s[i] == '-') {
assert(s.size() > 1);
is_minus = true;
} else {
assert('0' <= s[i] && s[i] <= '9');
val *= 10;
val += s[i] - '0';
}
}
if (is_minus) val *= -1;
return stream;
}
// 128bit.
ostream& operator<<(ostream& stream, const int128& val_) {
auto val(val_);
if (val == 0) return stream << 0;
if (val < 0) {
stream << '-';
val *= -1;
}
string s;
while (val > 0) {
s += (val % 10) + '0';
val /= 10;
}
reverse(s.begin(), s.end());
return stream << s;
}
// MARK: PAIR
/* + - */
template <class T, class U> pair<T, U> operator+(pair<T, U> a) {
return a;
}
template <class T, class U> pair<T, U> operator-(pair<T, U> a) {
return a * (-1);
}
/* (pair) */
template <class T, class U> pair<T, U>& operator+=(pair<T, U>& a, const pair<T, U> b) {
a.first += b.first;
a.second += b.second;
return a;
}
template <class T, class U> pair<T, U>& operator-=(pair<T, U>& a, const pair<T, U> b) {
a.first -= b.first;
a.second -= b.second;
return a;
}
template <class T, class U> pair<T, U>& operator*=(pair<T, U>& a, const pair<T, U> b) {
a.first *= b.first;
a.second *= b.second;
return a;
}
template <class T, class U> pair<T, U>& operator/=(pair<T, U>& a, const pair<T, U> b) {
a.first /= b.first;
a.second /= b.second;
return a;
}
template <class T, class U> pair<T, U>& operator%=(pair<T, U>& a, const pair<T, U> b) {
a.first %= b.first;
a.second %= b.second;
return a;
}
template <class T, class U, class V> pair<T, U>& operator+=(pair<T, U>& a, const V b) {
a.first += b;
a.second += b;
return a;
}
template <class T, class U, class V> pair<T, U>& operator-=(pair<T, U>& a, const V b) {
a.first -= b;
a.second -= b;
return a;
}
template <class T, class U, class V> pair<T, U>& operator*=(pair<T, U>& a, const V b) {
a.first *= b;
a.second *= b;
return a;
}
template <class T, class U, class V> pair<T, U>& operator/=(pair<T, U>& a, const V b) {
a.first /= b;
a.second /= b;
return a;
}
template <class T, class U, class V> pair<T, U>& operator%=(pair<T, U>& a, const V b) {
a.first %= b;
a.second %= b;
return a;
}
/* (pair) */
template <class T, class U, class V> pair<T, U> operator+(pair<T, U> a, const V& b) {
return a += b;
}
template <class T, class U, class V> pair<T, U> operator-(pair<T, U> a, const V& b) {
return a -= b;
}
template <class T, class U, class V> pair<T, U> operator*(pair<T, U> a, const V& b) {
return a *= b;
}
template <class T, class U, class V> pair<T, U> operator/(pair<T, U> a, const V& b) {
return a /= b;
}
template <class T, class U, class V> pair<T, U> operator%(pair<T, U> a, const V& b) {
return a %= b;
}
//
template <class T, class U> istream& operator>>(istream& stream, pair<T, U>& a) {
stream >> a.first >> a.second;
return stream;
}
template <class T, class U> ostream& operator<<(ostream& stream, const pair<T, U>& a) {
stream << a.first << " " << a.second;
return stream;
}
// MARK: VECTOR
/* + - */
template <class T> vector<T> operator+(vector<T> a) {
return a;
}
template <class T> vector<T> operator-(vector<T> a) {
return a * (-1);
}
/* (vector) */
template <class T> vector<T>& operator+=(vector<T>& a, const vector<T> b) {
assert(a.size() == b.size());
for (int i = 0; i < (int)a.size(); i++) a[i] += b[i];
return a;
}
template <class T> vector<T>& operator-=(vector<T>& a, const vector<T> b) {
assert(a.size() == b.size());
for (int i = 0; i < (int)a.size(); i++) a[i] -= b[i];
return a;
}
template <class T> vector<T>& operator*=(vector<T>& a, const vector<T> b) {
assert(a.size() == b.size());
for (int i = 0; i < (int)a.size(); i++) a[i] *= b[i];
return a;
}
template <class T> vector<T>& operator/=(vector<T>& a, const vector<T> b) {
assert(a.size() == b.size());
for (int i = 0; i < (int)a.size(); i++) a[i] /= b[i];
return a;
}
template <class T> vector<T>& operator%=(vector<T>& a, const vector<T> b) {
assert(a.size() == b.size());
for (int i = 0; i < (int)a.size(); i++) a[i] %= b[i];
return a;
}
template <class T, class U> vector<T>& operator+=(vector<T>& a, const U b) {
for (auto&& e : a) e += b;
return a;
}
template <class T, class U> vector<T>& operator-=(vector<T>& a, const U b) {
for (auto&& e : a) e -= b;
return a;
}
template <class T, class U> vector<T>& operator*=(vector<T>& a, const U b) {
for (auto&& e : a) e *= b;
return a;
}
template <class T, class U> vector<T>& operator/=(vector<T>& a, const U b) {
for (auto&& e : a) e /= b;
return a;
}
template <class T, class U> vector<T>& operator%=(vector<T>& a, const U b) {
for (auto&& e : a) e %= b;
return a;
}
/* (vector) */
template <class T, class U> vector<T> operator+(vector<T> a, const U& b) {
return a += b;
}
template <class T, class U> vector<T> operator-(vector<T> a, const U& b) {
return a -= b;
}
template <class T, class U> vector<T> operator*(vector<T> a, const U& b) {
return a *= b;
}
template <class T, class U> vector<T> operator/(vector<T> a, const U& b) {
return a /= b;
}
template <class T, class U> vector<T> operator%(vector<T> a, const U& b) {
return a %= b;
}
/* (vector) */
//
template <class T> istream& operator>>(istream& stream, vector<T>& a) {
for (auto&& e : a) stream >> e;
return stream;
}
//
template <class T> ostream& operator<<(ostream& stream, const vector<T>& v) {
if (v.size()) {
stream << v[0];
for (int i = 1; i < (int)v.size(); i++) cout << " " << v[i];
}
return stream;
}
// 2/
template <class T> ostream& operator<<(ostream& stream, const vector<vector<T>>& vv) {
if (vv.size()) {
stream << vv[0];
for (int i = 1; i < (int)vv.size(); i++) cout << '\n' << vv[i];
}
return stream;
}
/* (vector) */
// v.size() == 0 T()
template <class T> T Sum(const vector<T>& v) {
return reduce(v.begin(), v.end());
}
template <class T> T Max(const vector<T>& v) {
assert(v.size());
return *max_element(v.begin(), v.end());
}
template <class T> T Min(const vector<T>& v) {
assert(v.size());
return *min_element(v.begin(), v.end());
}
// 0-origin, O(N).
template <class T> ll Argmax(const vector<T>& v) {
assert(v.size());
return max_element(v.begin(), v.end()) - v.begin();
}
// 0-origin, O(N).
template <class T> ll Argmin(const vector<T>& v) {
assert(v.size());
return min_element(v.begin(), v.end()) - v.begin();
}
// `a``v`1
template <class T, class U> bool Contains(const vector<T>& v, const U& a) {
return find(v.begin(), v.end(), a) != v.end();
}
// inplace, O(NlogN).
template <class T> void Unique(vector<T>& v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
// `a``v``bool`, O(N).
template <class T, class U> bool Erase(vector<T>& v, const U& a) {
auto iter = find(v.begin(), v.end(), a);
if (iter == v.end()) return false;
v.erase(iter);
return true;
}
/* (vector) */
template <class T, class U> vector<T> make_vector(int n, U v) {
return vector<T>(n, v);
}
template <class T, class... Args> auto make_vector(int n, Args... args) {
auto val = make_vector<T>(args...);
return make_vector<decltype(val)>(n, move(val));
}
// MARK: SET
//
template <class T> istream& operator>>(istream& stream, set<T>& st) {
T e;
stream >> e;
st.insert(e);
return stream;
}
//
template <class T> ostream& operator<<(ostream& stream, const set<T>& st) {
if (st.size()) {
auto it = st.begin();
stream << *it++;
for (; it != st.end(); it++) cout << " " << *it;
}
return stream;
}
template <class T> T Max(const set<T>& st) {
assert(st.size());
return *prev(st.end());
}
template <class T> T Min(const set<T>& st) {
assert(st.size());
return *st.begin();
}
// MARK: MULTISET
//
template <class T> istream& operator>>(istream& stream, multiset<T>& st) {
T e;
stream >> e;
st.insert(e);
return stream;
}
//
template <class T> ostream& operator<<(ostream& stream, const multiset<T>& st) {
if (st.size()) {
auto it = st.begin();
stream << *it++;
for (; it != st.end(); it++) cout << " " << *it;
}
return stream;
}
template <class T> T Max(const multiset<T>& st) {
assert(st.size());
return *prev(st.end());
}
template <class T> T Min(const multiset<T>& st) {
assert(st.size());
return *st.begin();
}
// x1. true.
template <class T> bool EraseOne(multiset<T>& st, const T& x) {
auto it = st.find(x);
if (it != st.end()) {
st.erase(it);
return true;
} else return false;
}
// MARK: CHAR
//
bool IsUpper(const char& c) {
return isupper(c) > 0;
}
//
bool IsLower(const char& c) {
return islower(c) > 0;
}
//
char ToUpper(const char& c) {
string s{c}, t;
t.resize(s.size());
std::transform(s.begin(), s.end(), t.begin(), ::toupper);
return t[0];
}
//
char ToLower(const char& c) {
string s{c}, t;
t.resize(s.size());
std::transform(s.begin(), s.end(), t.begin(), ::tolower);
return t[0];
}
// MARK: STRING
//
string ToUpper(const string& s) {
string t;
t.resize(s.size());
std::transform(s.begin(), s.end(), t.begin(), ::toupper);
return t;
}
//
string ToLower(const string& s) {
string t;
t.resize(s.size());
std::transform(s.begin(), s.end(), t.begin(), ::tolower);
return t;
}
// , O(N). True.
bool IsPalindrome(const string& S) {
ll N = S.size();
for (ll i = 0; 2 * i < N; i++)
if (S[i] != S[N - i - 1]) return false;
return true;
}
// MARK: TRIO
template <class T1, class T2, class T3> struct trio {
T1 first;
T2 second;
T3 third;
trio() {
first = T1();
second = T2();
third = T3();
}
trio(const T1& x) : first(x), second(x), third(x) {}
trio(const T1& x, const T2& y, const T3& z) : first(x), second(y), third(z) {}
trio(const trio& t) {
first = t.first;
second = t.second;
third = t.third;
}
trio<T1, T2, T3>& operator=(const trio& t) {
if (this != &t) {
first = t.first;
second = t.second;
third = t.third;
}
return *this;
}
// (trio)
bool operator<(const trio& t) const {
return tie(first, second, third) < tie(t.first, t.second, t.third);
}
bool operator==(const trio& t) const {
return tie(first, second, third) == tie(t.first, t.second, t.third);
}
bool operator!=(const trio& t) const {
return !(*this == t);
}
bool operator>(const trio& t) const {
return t < *this;
}
bool operator<=(const trio& t) const {
return !(*this > t);
}
bool operator>=(const trio& t) const {
return !(*this < t);
}
// + - (trio)
trio<T1, T2, T3> operator+() const {
return *this;
}
trio<T1, T2, T3> operator-() const {
return (*this) * (-1);
}
//
trio& operator+=(const trio& t) {
first += t.first;
second += t.second;
third += t.third;
return *this;
}
trio& operator-=(const trio& t) {
first -= t.first;
second -= t.second;
third -= t.third;
return *this;
}
trio& operator*=(const trio& t) {
first *= t.first;
second *= t.second;
third *= t.third;
return *this;
}
trio& operator/=(const trio& t) {
first /= t.first;
second /= t.second;
third /= t.third;
return *this;
}
trio& operator%=(const trio& t) {
first %= t.first;
second %= t.second;
third %= t.third;
return *this;
}
//
friend trio operator+(const trio& lhs, const trio& rhs) {
return trio(lhs) += rhs;
}
friend trio operator-(const trio& lhs, const trio& rhs) {
return trio(lhs) -= rhs;
}
friend trio operator*(const trio& lhs, const trio& rhs) {
return trio(lhs) *= rhs;
}
friend trio operator/(const trio& lhs, const trio& rhs) {
return trio(lhs) /= rhs;
}
friend trio operator%(const trio& lhs, const trio& rhs) {
return trio(lhs) %= rhs;
}
// (trio)
friend istream& operator>>(istream& stream, trio<T1, T2, T3>& t) {
return stream >> t.first >> t.second >> t.third;
}
friend ostream& operator<<(ostream& stream, const trio<T1, T2, T3>& t) {
return stream << t.first << " " << t.second << " " << t.third;
}
};
using tll = trio<ll, ll, ll>;
// MARK: QUARTET
template <class T1, class T2, class T3, class T4> struct quartet {
T1 first;
T2 second;
T3 third;
T4 fourth;
//
quartet() {
first = T1();
second = T2();
third = T3();
fourth = T4();
}
quartet(const T1& x) : first(x), second(x), third(x), fourth(x) {}
quartet(const T1& x, const T2& y, const T3& z, const T4& w) : first(x), second(y), third(z), fourth(w) {}
quartet(const quartet& t) {
first = t.first;
second = t.second;
third = t.third;
fourth = t.fourth;
}
quartet<T1, T2, T3, T4>& operator=(const quartet& t) {
if (this != &t) {
first = t.first;
second = t.second;
third = t.third;
fourth = t.fourth;
}
return *this;
}
// (quartet)
bool operator<(const quartet& t) const {
return tie(first, second, third, fourth) < tie(t.first, t.second, t.third, t.fourth);
}
bool operator==(const quartet& t) const {
return tie(first, second, third, fourth) == tie(t.first, t.second, t.third, t.fourth);
}
bool operator!=(const quartet& t) const {
return !(*this == t);
}
bool operator>(const quartet& t) const {
return t < *this;
}
bool operator<=(const quartet& t) const {
return !(*this > t);
}
bool operator>=(const quartet& t) const {
return !(*this < t);
}
// + - (quartet)
quartet<T1, T2, T3, T4> operator+() const {
return *this;
}
quartet<T1, T2, T3, T4> operator-() const {
return (*this) * (-1);
}
//
quartet& operator+=(const quartet& t) {
first += t.first;
second += t.second;
third += t.third;
fourth += t.fourth;
return *this;
}
quartet& operator-=(const quartet& t) {
first -= t.first;
second -= t.second;
third -= t.third;
fourth -= t.fourth;
return *this;
}
quartet& operator*=(const quartet& t) {
first *= t.first;
second *= t.second;
third *= t.third;
fourth *= t.fourth;
return *this;
}
quartet& operator/=(const quartet& t) {
first /= t.first;
second /= t.second;
third /= t.third;
fourth /= t.fourth;
return *this;
}
quartet& operator%=(const quartet& t) {
first %= t.first;
second %= t.second;
third %= t.third;
fourth %= t.fourth;
return *this;
}
//
friend quartet operator+(const quartet& lhs, const quartet& rhs) {
return quartet(lhs) += rhs;
}
friend quartet operator-(const quartet& lhs, const quartet& rhs) {
return quartet(lhs) -= rhs;
}
friend quartet operator*(const quartet& lhs, const quartet& rhs) {
return quartet(lhs) *= rhs;
}
friend quartet operator/(const quartet& lhs, const quartet& rhs) {
return quartet(lhs) /= rhs;
}
friend quartet operator%(const quartet& lhs, const quartet& rhs) {
return quartet(lhs) %= rhs;
}
// (quartet)
friend istream& operator>>(istream& stream, quartet<T1, T2, T3, T4>& t) {
return stream >> t.first >> t.second >> t.third >> t.fourth;
}
friend ostream& operator<<(ostream& stream, const quartet<T1, T2, T3, T4>& t) {
return stream << t.first << " " << t.second << " " << t.third << " " << t.fourth;
}
};
using qll = quartet<ll, ll, ll, ll>;
// MARK: RANDOM
struct Random {
mt19937_64 rnd;
// : seed
Random() {
random_device seed_gen;
rnd.seed(seed_gen());
}
// : seed
Random(ll seed) {
rnd.seed(seed);
}
// [a, b]
ll randint(ll a, ll b) {
assert(a <= b);
uniform_int_distribution<ll> dist(a, b);
return dist(rnd);
}
// [a, b)
ll randrange(ll a, ll b) {
return randint(a, b - 1);
}
// [a, b]
double randreal(double a, double b) {
assert(a <= b);
uniform_real_distribution<double> dist(a, b);
return dist(rnd);
}
// [a, b]
char randchar(char a, char b) {
assert(a <= b);
uniform_int_distribution<ll> dist(a, b);
return dist(rnd);
}
// vector1
template <class T> T sample(const vector<T>& vec) {
ll i = randrange(0, vec.size());
return vec[i];
}
// `vec`inplace
template <class T> void shuffle(vector<T>& vec) {
std::shuffle(vec.begin(), vec.end(), rnd);
}
};
// MARK: TIMER
struct Timer {
ll time_limit_ms;
chrono::high_resolution_clock::time_point start_time;
Timer() {}
// :
Timer(ll time_limit_ms) {
assert(time_limit_ms > 0);
this->time_limit_ms = time_limit_ms;
this->start_time = chrono::high_resolution_clock::now();
}
ll get_elapsed_ms() const {
auto now = chrono::high_resolution_clock::now();
return ll(chrono::duration_cast<chrono::milliseconds>(now - this->start_time).count());
}
//
bool is_time_up() const {
ll elapsed_ms = get_elapsed_ms();
return elapsed_ms >= this->time_limit_ms;
}
// [0.0, 1.0]
double get_elapsed_ratio() const {
ll elapsed_ms = get_elapsed_ms();
double ratio = 1.0 * elapsed_ms / this->time_limit_ms;
return min(1.0, ratio);
}
};
// MARK: DEBUG
#ifdef __LOCAL
#define debug(...) \
if (DEBUG) do { \
cerr << '[' << #__VA_ARGS__ << "] "; \
debug_(__VA_ARGS__); \
} while (0)
#else
#define debug(...)
#endif
bool DEBUG = false;
void dbg_(const long long& e) {
if (e == INF) cerr << "INF";
else if (e == -INF) cerr << "-INF";
else cerr << e;
}
template <class T> void dbg_(const T& e) {
cerr << e;
}
template <class T, class U> void dbg_(const pair<T, U>& p) {
cerr << '(';
dbg_(p.first);
cerr << ' ';
dbg_(p.second);
cerr << ')';
}
template <class T1, class T2, class T3> void dbg_(const trio<T1, T2, T3>& t) {
cerr << '(';
dbg_(t.first);
cerr << ' ';
dbg_(t.second);
cerr << ' ';
dbg_(t.third);
cerr << ')';
}
template <class T1, class T2, class T3, class T4> void dbg_(const quartet<T1, T2, T3, T4>& t) {
cerr << '(';
dbg_(t.first);
cerr << ' ';
dbg_(t.second);
cerr << ' ';
dbg_(t.third);
cerr << ' ';
dbg_(t.fourth);
cerr << ')';
}
template <class T> void debug_(const T& e) {
dbg_(e);
cerr << '\n';
}
template <class T> void debug_(const vector<T>& v) {
if (v.size()) {
auto it = v.begin();
dbg_(*it++);
for (; it != v.end(); ++it) {
cerr << ' ';
dbg_(*it);
}
}
cerr << '\n';
}
template <class T> void debug_(const vector<vector<T>>& vv) {
cerr << '\n';
ll cnt = 0;
for (auto&& v : vv) {
cerr << cnt++ << ": ";
debug_(v);
}
}
template <class T, class U> void debug_(const map<T, U>& mp) {
if (mp.size()) {
auto it = mp.begin();
dbg_(*it++);
for (; it != mp.end(); ++it) {
cerr << ' ';
dbg_(*it);
}
}
cerr << '\n';
}
template <class T, class U> void debug_(const vector<map<T, U>>& vm) {
cerr << '\n';
ll cnt = 0;
for (auto&& mp : vm) {
cerr << cnt++ << ": ";
debug_(mp);
}
}
template <class T> void debug_(const set<T>& st) {
if (st.size()) {
auto it = st.begin();
dbg_(*it++);
for (; it != st.end(); ++it) {
cerr << ' ';
dbg_(*it);
}
}
cerr << '\n';
}
template <class T> void debug_(const multiset<T>& st) {
if (st.size()) {
auto it = st.begin();
dbg_(*it++);
for (; it != st.end(); ++it) {
cerr << ' ';
dbg_(*it);
}
}
cerr << '\n';
}
template <class T> void debug_(const vector<set<T>>& vs) {
cerr << '\n';
ll cnt = 0;
for (auto&& st : vs) {
cerr << cnt++ << ": ";
debug_(st);
}
}
template <class T> void debug_(const vector<multiset<T>>& vs) {
cerr << '\n';
ll cnt = 0;
for (auto&& st : vs) {
cerr << cnt++ << ": ";
debug_(st);
}
}
template <class H, class... T> void debug_(const H& h, const T&... t) {
dbg_(h);
cerr << ", ";
debug_(t...);
}
// MARK: OTHERS
// a > b a b .
template <class T, class U> bool chmin(T& a, U b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// a < b a b .
template <class T, class U> bool chmax(T& a, U b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// a % m. aOK.
template <class T, class U> T Mod(const T& a, const U& m) {
assert(m > 0);
return (a % m + m) % m;
}
// . xOK
template <class T, class U> T Ceil(const T& x, const U& y) {
assert(y > 0);
return x < 0 ? x / y : (x + y - 1) / y;
}
// . xOK
template <class T, class U> T Floor(const T& x, const U& y) {
assert(y > 0);
return -(Ceil(-x, y));
}
// . : -1, 0, +1
template <class T> ll Sign(const T& x) {
return (x > T(0)) - (x < T(0));
}
// x * x <= n`x`
ll Isqrt(ll n) {
assert(n >= 0);
ll x = round(sqrt(n));
while (x * x > n) --x;
return x;
}
// nCr. n=60, r=30OK.
ll Comb(ll n, ll r) {
if (r < 0 || n < r) return 0;
r = min(r, n - r);
ll ret = 1;
for (ll i = 0; i < r; i++) {
ret *= n - i;
ret /= i + 1;
}
return ret;
}
// x^n. mintOK.
template <class T> T Pow(T x, ll n) {
assert(n >= 0);
T ret = 1;
while (1) {
if (n % 2) ret *= x;
n /= 2;
if (!n) return ret;
x = x * x;
}
}
// (x^n) % mod.
template <class T> T Pow(T x, ll n, T mod) {
assert(n >= 0 && mod > 0);
T ret = 1;
while (1) {
if (n % 2) ret = (ret * x) % mod;
n /= 2;
if (!n) return ret;
x = (x * x) % mod;
}
}
// . mintOK.
template <class T> T Aseries(T a, T d, ll n) {
assert(n >= 0);
return a * n + n * (n - 1) / 2 * d;
}
// . mintOK.
template <class T> T Gseries(T a, T r, ll n) {
assert(n >= 0);
if (r == 1) return a * n;
else return a * (1 - Pow(r, n)) / (1 - r);
}
// `b``i`0 origin1
bool Bit(ll b, int i) {
assert(0 <= i && i < 64);
return (b >> i) & 1;
}
// `b`
ll Popcount(ll b) {
return __builtin_popcountll(b);
}
// [0, n)64bit.
ll Mask(ll n) {
assert(0 <= n && n < 63);
return (1LL << n) - 1LL;
}
// [m, n)64bit.
ll Mask(ll n, ll m) {
assert(n >= m);
return Mask(n) ^ Mask(m);
}
// [l1, r1)[l2, r2). : [3, 6) x [5, 7) = [5, 6)
pll GetOverlap(ll l1, ll r1, ll l2, ll r2) {
return {max(l1, l2), min(r1, r2)};
}
// [l1, r1)[l2, r2). false;
bool IsOverlap(ll l1, ll r1, ll l2, ll r2) {
return max(l1, l2) < min(r1, r2);
}
// b=true"Yes\n", false"No\n"
void PrintYesNo(bool b) {
string res = b ? "Yes" : "No";
cout << res << '\n';
}
// // MARK: MODINT
// #include <atcoder/modint> // https://atcoder.github.io/ac-library/production/document_ja/
// using namespace atcoder;
// using mint = modint998244353; // modint1000000007;
// istream& operator >>(istream& stream, mint& e) {int64_t n; stream >> n; e = n; return stream; }
// ostream& operator <<(ostream& stream, const mint& e) { stream << e.val(); return stream; }
// using vm = vector<mint>;
// using vvm = vector<vm>;
// MARK: PRINT
template <class T> void print(const T& e) {
cout << e << '\n';
}
template <class H, class... T> void print(const H& h, const T&... t) {
cout << h << ' ';
print(t...);
}
template <class... T> void End(const T&... t) {
print(t...);
exit(0);
}
template <class T> void print_err(const T& e) {
cerr << e << '\n';
}
template <class H, class... T> void print_err(const H& h, const T&... t) {
cerr << h << ' ';
print_err(t...);
}
/* ********************************************************************** */
//
struct IntervalSet {
map<ll, ll> mp; // mp[r] = l for [l, r)
IntervalSet() {}
//
ll size() const {
return len(mp);
}
// [l, r). :
bool insert(ll l, ll r) {
assert(l <= r);
if (l == r) return false;
auto it = mp.lower_bound(l);
//
if (it != mp.end() && (*it).se <= l && r <= (*it).fi) return false;
// [l, r)[l2, r2)
ll l2 = l;
ll r2 = r;
while(it != mp.end() && (*it).se <= r) {
chmin(l2, (*it).se);
chmax(r2, (*it).fi);
it = mp.erase(it);
}
mp[r2] = l2;
return true;
}
// [x, x + 1). :
bool insert(ll x) {
return insert(x, x + 1);
}
// [l, r). :
bool erase(ll l, ll r) {
assert(l <= r);
if (l == r) return false;
auto it = mp.upper_bound(l);
//
if (it == mp.end() || r <= (*it).se) return false;
// [l, r)
vector<pll> new_intervals;
while(it != mp.end() && (*it).se < r) {
auto [r2, l2] = *it;
if (l2 < l) new_intervals.eb(l, l2); // r, l
if (r < r2) new_intervals.eb(r2, r); // r, l
it = mp.erase(it);
}
fore(p, new_intervals) mp.emplace(p);
return true;
}
// [x, x + 1). :
bool erase(ll x) {
return erase(x, x + 1);
}
// [l, r)
bool contains(ll l, ll r) const {
assert(l <= r);
auto it = mp.upper_bound(l);
return it != mp.end() && (*it).se <= l && r <= (*it).fi;
}
// [x, x + 1)
bool contains(ll x) const {
return contains(x, x + 1);
}
// [l, r)
vector<pll> get_intersecting_intervals(ll l, ll r) const {
assert(l <= r);
vector<pll> ret;
if (l == r) return ret; // [l, l)
auto it = mp.upper_bound(l);
for (; it != mp.end(); it++) {
auto [r1, l1] = *it;
if (r <= l1) break;
ret.eb(l1, r1);
}
return ret;
}
};
// auto ist = IntervalSet();
// ist.insert(1, 8); // {[1, 8)} //
// ist.erase(3, 4); // {[1, 3), [4, 8)} //
// ist.size(); // 2 //
// ist.contains(5, 9); // false //
// ist.get_intersecting_intervals(3, 5); // {[4, 8)} //
// mexminimum excluded value
struct Mex {
IntervalSet ist; //
map<ll, ll> counts; //
Mex() : ist(IntervalSet()) {}
Mex(vl& vec) : ist(IntervalSet()) {
fore(e, vec) insert(e);
}
//
void insert(ll val) {
assert(val >= 0);
counts[val]++;
if (counts[val] == 1) ist.insert(val);
}
//
void erase(ll val) {
assert(counts.contains(val));
counts[val]--;
if (counts[val] == 0) {
counts.erase(val);
ist.erase(val);
}
}
// mex
ll get() {
auto it = ist.mp.begin();
if (it == ist.mp.end() || (*it).se > 0) return 0;
else return (*it).fi;
}
//
bool contains(ll val) {
return counts.contains(val);
}
};
// vl vec = {0, 1, 3, 5, 3};
// auto mex = Mex(vec);
// mex.insert(5); // {0, 1, 3, 3, 5, 5}
// mex.erase(1); // {0, 3, 3, 5, 5}
// mex.get(); // 1
// mex.contains(4); // false;
// MARK: MAIN
int main() {
cin.tie(nullptr);
cout << fixed << setprecision(10);
DEBUG = true;
ll N, X;
cin >> N >> X;
vl A(N);
cin >> A;
X--;
if (X < N) {
End(A[X]);
}
X -= N;
X %= N + 1;
auto mex = Mex(A);
vl B(N + 1);
rep(i, N + 1) {
ll val = mex.get();
B[i] = val;
mex.insert(val);
if (i == N) break;
mex.erase(A[i]);
}
debug(B);
print(B[X]);
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0