#include<cstdio> #include<queue> #include<ctime> #include<cstring> #include<algorithm> usingnamespacestd; constint maxn = 500010; int F[maxn]; int n, k; voidinitF(){ for (int i = 1; i < maxn; i++) { for (int j = 1; i * j < maxn; j++) F[i * j]++; } }
int bit[maxn]; intlowbit(int x){ return x & -x; } intsum(int i){ int s = 0; for (; i; i -= lowbit(i)) s += bit[i]; return s; }
voidadd(int i, int x){ for (; i <= n; i += lowbit(i)) bit[i] += x; }
structkid { int p, id; kid(int p = 0, int id = 0):p(p), id(id) {} booloperator < (const kid &b)const { if (F[p] != F[b.p]) return F[p] < F[b.p]; return p > b.p; } }; priority_queue<kid> que; int cards[maxn]; char names[maxn][12];
typedeflonglong ll; ll tr(ll x, ll n){ x += n * 100000000LL; return x % n; }
intgetP(int k){ int l = 0, r = n; while (l + 1 < r) { int mid = (l + r) >> 1; if (sum(mid) < k) l = mid; else r = mid; } return r; }
intsolve(int k, int step, int n){ int ni = getP(k); add(ni, -1); que.push(kid(step, ni)); if (n == 0) return0; k += -1 + cards[ni]; if (cards[ni] > 0) k--; if (k < 0) k = (int)tr(k, n); k %= n; k++; return k; } voidClear(priority_queue<kid> &que){ priority_queue<kid> q1; swap(q1, que); }
intmain(){ initF();
while (scanf("%d%d", &n, &k) != EOF) { for (int i = 1; i <= n; i++) scanf("%s%d", names[i], cards + i); Clear(que); memset(bit, 0, sizeof(bit)); for (int i = 1; i <= n; i++) add(i, 1); for (int i = 1; i <= n; i++) { k = solve(k, i, n - i); } kid ans = que.top(); printf("%s %d\n", names[ans.id], F[ans.p]); } }