1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <cstdio> #define lc p<<1, s, mid #define rc p<<1|1, mid + 1, e #define mid ((s+e)>>1) using namespace std; const int N = 5e5 + 5; int tot[N * 4], val[N];
char name[N][20];
int ip[] = {1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560, 10080, 15120, 20160, 25200, 27720, 45360, 50400, 55440, 83160, 110880, 166320, 221760, 277200, 332640, 498960, 500001 };
int div[] = {1, 2, 3, 4, 6, 8, 9, 10, 12, 16, 18, 20, 24, 30, 32, 36, 40, 48, 60, 64, 72, 80, 84, 90, 96, 100, 108, 120, 128, 144, 160, 168, 180, 192, 200 };
void pushup(int p) { tot[p] = tot[p << 1] + tot[p << 1 | 1]; }
void build(int p, int s, int e) { if(s == e) { tot[p] = 1; return; } build(lc); build(rc); pushup(p); }
int update(int p, int s, int e, int x) { int ret; if(s == e) { tot[p] = 0; return s; } if(x <= tot[p << 1]) ret = update(lc, x); else ret = update(rc, x - tot[p << 1]); pushup(p); return ret; }
int main() { int n, k, m, r, ans, pos; while(scanf("%d%d", &n, &k) != EOF) { build(1, 1, n); for(int i = 1; i <= n; ++i) scanf("%s%d", name[i], &val[i]);
for(int i = 0; ip[i] <= n; ++i) { m = ip[i]; ans = div[i]; }
r = n; for(int i = 0; i < m; ++i) { r--; pos = update(1, 1, n, k); if(!r) break; if(val[pos] >= 0) k = (k - 1 - 1 + val[pos]) % r + 1; else k = ((k - 1 + val[pos]) % r + r) % r + 1; } printf("%s %d\n", name[pos], ans); }
return 0; }
|