Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 11955 | Accepted: 3734 | |
Case Time Limit: 2000MS |
Description
N children are sitting in a circle to play a game.
The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the next child will be the (−A)-th child to the right.
The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets the most candies?
Input
Output
Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first.
Sample Input
4 2
Tom 2
Jack 4
Mary -1
Sam 1
Sample Output
Sam 3 像是约瑟夫问题,用线段树表示区间中有多少可用
#include <stdio.h>
char name[][];
int ex[], sum[];
int prim[] = {, , , , , , , , , , , , , , , , , , , , , , , , };
void build(int o, int l, int r) {
if (l == r) {
sum[o] = ;
return;
}
int mid = l + r >> ;
build(o << , l, mid);
build(o << | , mid + , r);
sum[o] = sum[o << ] + sum[o << | ];
}
int update(int o, int l, int r, int pos) {
int ans();
if (l == r) {
sum[o] -= ;
ans = l;
return ans;
}
int mid = l + r >> ;
if (sum[o << ] >= pos) ans = update(o << , l, mid, pos);
else ans = update(o << | , mid + , r, pos - sum[o << ]);
sum[o] = sum[o << ] + sum[o << | ];
return ans;
}
int query(int o, int l, int r, int ql, int qr) {
int ans();
if (ql <= l && r <= qr) {
return sum[o];
}
int mid = l + r >> ;
if (ql <= mid) ans += query(o << , l, mid, ql, qr);
if (qr > mid) ans += query(o << | , mid + , r, ql, qr);
return ans;
}
int solve(int x) {
int sum = ;
for (int i = ; i < ; i++) {
int ans = ;
while (x % prim[i] == ) {
ans++;
x /= prim[i];
}
sum *= ans + ;
}
return sum;
}
int main() {
int n, k, ans, key;
while (~scanf("%d%d", &n, &k)) {
for (int i = ; i <= n; i++) scanf("%s %d", name[i], &ex[i]);
int m = n;
build(, , n);
int pos = update(, , n, k);
ans = solve(); key = ;
m--;
for (int i = ; i <= n; i++) {
int id = ((query(, , n, , (pos - == ? : pos - )) + (ex[pos] < ? ex[pos] + : ex[pos])) % m + m) % m;
if (!id) id = m;
m--;
pos = update(, , n, id);
int p = solve(i);
if (p > ans) {
key = pos;
ans = p;
}
}
printf("%s %d\n", name[key], ans);
}
return ;
}