#define Cmp(a, b) memcmp(a, b, sizeof(b)) #define Cpy(a, b) memcpy(a, b, sizeof(b)) #define Set(a, v) memset(a, v, sizeof(a)) #define debug(x) cout << #x << ": " << x << endl #define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++) #define _rep(i, l, r) for(int i = (l); i <= (r); i++) #define _for(i, l, r) for(int i = (l); i < (r); i++) #define _forDown(i, l, r) for(int i = (l); i >= r; i--) #define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i]) #define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second) #define debugS(str) cout << "dbg: " << str << endl; #define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); } #define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++) #define lowbit(i) (i & (-i)) #define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) { int st = sqrt(n); int fac = n / st; while (n % st) { st += 1; fac = n / st; }
return make_pair(st, fac); }
inline ll qpow(ll a, int n) { ll ans = 1; for(; n; n >>= 1) { if(n & 1) ans *= 1ll * a; a *= a; } return ans; }
template <class T> inline bool chmax(T& a, T b) { if(a < b) { a = b; returntrue; } returnfalse; }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll ksc(ll a, ll b, ll mod) { ll ans = 0; for(; b; b >>= 1) { if (b & 1) ans = (ans + a) % mod; a = (a * 2) % mod; } return ans; }
ll ksm(ll a, ll b, ll mod) { ll ans = 1 % mod; a %= mod;
for(; b; b >>= 1) { if (b & 1) ans = ksc(ans, a, mod); a = ksc(a, a, mod); }
return ans; }
template <class T> inline bool chmin(T& a, T b) { if(a > b) { a = b; returntrue; } returnfalse; }
template<class T> bool lexSmaller(vector<T> a, vector<T> b) { int n = a.size(), m = b.size(); int i; for(i = 0; i < n && i < m; i++) { if (a[i] < b[i]) returntrue; elseif (b[i] < a[i]) returnfalse; } return (i == n && i < m); }
int norm(int x) { if (x < 0) x += P; if (x >= P) x -= P; return x; }
template<class T> T power(T a, int b) { T res = 1; for (; b; b >>= 1) { if (b & 1) res *= a; a *= a; } return res; }
struct Z { int x; Z(int x = 0) : x(norm(x)) {}
int val() const { return x; } Z operator-() const { return Z(norm(P-x)); } Z &operator *= (const Z &rhs) { x = (ll)(x) * rhs.x % P; return *this; } Z &operator += (const Z &rhs) { x = norm(x + rhs.x); return *this; } Z &operator -= (const Z &rhs) { x = norm(x - rhs.x); return *this; } Z &operator /= (const Z &rhs) { return *this *= rhs.inv(); } Z inv() const { assert(x != 0); return power(*this, P-2); } friend Z operator* (const Z &lhs, const Z &rhs) { Z res = lhs; res *= rhs; return res; } friend Z operator+ (const Z &lhs, const Z &rhs) { Z res = lhs; res += rhs; return res; } friend Z operator- (const Z &lhs, const Z &rhs) { Z res = lhs; res -= rhs; return res; } friend Z operator/ (const Z &lhs, const Z &rhs) { Z res = lhs; res /= rhs; return res; } };
struct Int { static constexpr int B = 10; vector<int> X; int size() const { return (int)X.size(); }
Int(int x = 0) { while (x) { X.push_back(x % B), x /= B; } }
Int(string str) { reverse(str.begin(), str.end()); for (auto u : str) X.push_back(u-'0'); }
friend Int operator+ (const Int &lhs, const Int &rhs) { if (lhs.size() < rhs.size()) return rhs + lhs; Int res;
int t = 0; for (int i = 0; i < lhs.size(); i++) { t += lhs.X[i]; if (i < rhs.size()) t += rhs.X[i]; res.X.push_back(t % B), t /= B; } if (t) res.X.push_back(t); while (res.X.size() > 1 && res.X.back() == 0) res.X.pop_back(); return res; }
friend Int operator- (const Int &lhs, const Int &rhs) { Int res; int t = 0; for (int i = 0; i < lhs.size(); i++) { t = lhs.X[i] - t; if (i < rhs.size()) t -= rhs.X[i]; res.X.push_back((t + B) % B);
if (t < 0) t = 1; else t = 0; } while (res.X.size() > 1 && res.X.back() == 0) res.X.pop_back(); return res; }
friend Int operator* (const Int &lhs, int b) { Int res; int t = 0; for (int i = 0; i < lhs.X.size() || t; i++) { if (i < lhs.X.size()) t += lhs.X[i] * b; res.X.push_back(t % B), t /= B; } return res; }
friend Int operator/ (const Int &lhs, int b) { Int res; int r = 0; for (int i = lhs.X.size()-1; i >= 0; i--) { r = r * B + lhs.X[i]; res.X.push_back(r / b), r %= b; } reverse(res.X.begin(), res.X.end()); while (res.X.size() > 1 && res.X.back() == 0) res.X.pop_back(); return res; }
friend Int operator* (const Int &lhs, const Int &rhs) { Int res; res.X.resize(lhs.size() + rhs.size() + B); fill(res.X.begin(), res.X.end(), 0);
for (int i = 0; i < lhs.size(); i++) { for (int j = 0; j < rhs.size(); j++) { res.X[i+j] += lhs.X[i] * rhs.X[j]; } } for (int i = 0; i < res.X.size(); i++) { if (res.X[i] >= B) res.X[i+1] += res.X[i] / B, res.X[i] %= B; }
friend Int operator/ (const Int& lhs, const Int &rhs) { int dv = lhs.size() - rhs.size(); assert(dv >= 0);
Int res; res.X.resize(dv+1); fill(res.X.begin(), res.X.end(), 0);
// append suffix zero Int a = lhs, b = rhs; reverse(b.X.begin(), b.X.end()); for (int i = 0; i < dv; i++) b.X.push_back(0); reverse(b.X.begin(), b.X.end());
for (int i = 0; i <= dv; i++) { while (b < a) { a = a-b; res.X[dv-i]++; } b.X.erase(b.X.begin()); } while (res.X.size() > 1 && res.X.back() == 0) res.X.pop_back(); return res; }
friend bool operator< (const Int &lhs, const Int &rhs) { if (lhs.size() < rhs.size()) returntrue; if (lhs.size() > rhs.size()) returnfalse;
if (vector<int>(lhs.X.rbegin(), lhs.X.rend()) <= vector<int>(rhs.X.rbegin(), rhs.X.rend())) returntrue; returnfalse; } void out() { reverse(X.begin(), X.end()); for (auto x : X) printf("%d", x); printf("\n"); } };
Int max_int(const Int &A, const Int &B) { if (A < B) return B; elsereturn A; }
class A { public: int u, c; A(int u = 0, int c = 0) : u(u), c(c) {} bool operator< (const A &rhs) const { return u < rhs.u || (u == rhs.u && c < rhs.c); } };
inline int Hash(const A &a) { return a.u * 1003 + a.c; }
struct hashfun { int operator() (const A &a) const { return Hash(a); } };