Description
NChildren are sitting in a circle to play a game.
The children are numbered from 1NIn clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts fromK-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. LetADenote the integer. IfAIs positive, the next child will beA-Th child to the left. IfAIs negative, the next child will be (?A)-Th child to the right.
The game lasts until all children have jumped out of the circle. During the game,P-Th child jumping out will getF(P) Candies whereF(P) Is the number of positive integers that perfectly divideP. Who gets the most candies?
Input
There are several test cases in the input. Each test case starts with two integers
N(0 <
N≤ 500,000) and
K(1 ≤
K≤
N) On the first line. The next
NLines contains the names of the children (consisting of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing order of the children's numbers, A name and an integer separated by a single space in a line with no leading or trailing spaces.
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 2Tom 2Jack 4Mary -1Sam 1
Sample output
Sam 3
Let's just talk about the idea of the simulation process. In the process of reducing the number of people, we regard a circle as where all people are, and think of it as relative positions. In fact, some positions in the process are empty. In this way, you can use the line segment tree to query the true position of the point. For example, if you are at the position 3, you should find the position in the circle as 4 on the card, but the third position is to exit, it will affect the relative position of the people behind the scenes. Therefore, we can only find the sixth position. Well, now we can go back to the real position, that is, there is a vacant circle, the purpose is to find the location where 6th people exist, and then iterate the entire process according to the cards in that person.
# Include <stdio. h> # include <string. h> # include <algorithm> # include <math. h> # include <ctype. h> # define lson o <1, l, m # define rson o <1 | 1, m + 1, rusing namespace STD; typedef long ll; const int max = 0x3f3f3f; const int maxn = 500050; struct c {int V; char name [11];} A [maxn]; int N, K, ANS [maxn], ID, Res [maxn <2], Pos; void INI () {for (INT I = 1; I <= N; I ++) {ans [I] ++; For (Int J = 2 * I; j <= N; j + = I) ans [J] ++;} Int _ max = ans [1]; id = 1; for (INT I = 2; I <= N; I ++) // find out the most sugar obtained by the first few people. If (ANS [I]> _ max) {_ max = ans [I]; id = I ;}} void up (int o) {res [O] = res [O <1] + Res [O <1 | 1];} void build (int o, int l, int R) {If (L = r) {res [O] = 1; return;} int M = (L + r)> 1; build (lson ); build (rson); up (o);} int Update (int p, int o, int L, int R) {If (L = r) {res [O] = 0; return l;} int M = (L + r)> 1, TMP; If (P <= res [O <1]) TM P = Update (p, lson); else TMP = Update (p-res [O <1], rson); up (o); Return TMP;} int main () {While (~ Scanf ("% d", & N, & K) {INI (); int CNT = N, I, Cc = ID; for (I = 1; I <= N; I ++) scanf ("% S % d", a [I]. name, & A [I]. v); Build (1, 1, n); int POs, next = K; Pos = Update (next, 1, 1, n); CNT = res [1]; CC --; while (CC --) {if (a [POS]. v> 0) Next = (next-1 + a [POS]. v-1) % CNT + CNT) % CNT + 1; else next = (next-1 + a [POS]. v) % CNT + CNT) % CNT + 1; Pos = Update (next, 1, 1, n); CNT = res [1];} printf ("% S % d \ n", a [POS]. name, ANS [ID]);} return 0 ;}
Zookeeper