题目传送门
#include <cstdio>
#include <cstring>
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
const int MAX_N = + ;
int sum[MAX_N << ];
struct node
{
char name[];
int val;
}boy[MAX_N<<];
int ans[MAX_N];
int id;
int n, k;
void build(int l, int r, int rt)
{
sum[rt] = r - l + ;
if (l == r)
return ;
int m = (l + r) >> ;
build (lson);
build (rson);
}
int update(int p, int l, int r, int rt)
{
sum[rt]--;
if (l == r)
{
return l;
}
int m = (l + r) >> ;
if (p <= sum[rt<<])
{
return update (p, lson);
}
else
{
return update (p - sum[rt<<], rson);
}
}
void Solve(){ //计算ans
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++){
ans[i]++;
for(int j=*i;j<=n;j+=i)
ans[j]++;
}
int max=ans[];
id=;
for(int i=;i<=n;i++) //找出第几个人跳出获得的糖最多
if(ans[i]>max){
max=ans[i];
id=i;
}
}
int main(void) //POJ 2886 Who Gets the Most Candies?
{
//freopen ("inF.txt", "r", stdin);
while (~scanf ("%d%d", &n, &k))
{
build (, n, );
for (int i=; i<=n; ++i)
{
scanf ("%s%d", &boy[i].name, &boy[i].val);
//printf ("%s%d\n", boy[i].name, boy[i].val);
}
Solve();
//int maxn = f(n);
int mod = sum[];
boy[].val = ;
int pos = ;
//printf ("%d\n", id);
int m=id;
while (m--)
{
if (boy[pos].val > )
k=((k-+boy[pos].val-)%mod+mod)%mod+;
else
k=((k-+boy[pos].val)%mod+mod)%mod+;
pos = update (k, , n, );
//printf ("%d ", pos);
mod = sum[];
}
printf ("%s %d\n", boy[pos].name, ans[id]);
}
}