2014
01-26

# Judges’s Final Problem

For any ACM/ICPC participant, it is always discouraging to discover that the real problems they face in the programming world are seldom as interesting as the tasks they face in the algorithm-related contests. While the efficiency of your algorithm is certainly vital to the overall system, still it is seldom valued so much as other factors such as readability, and the ability of coding accurately often takes precedence in many projects. Still, we judges believe that a balance between these two can be achieved – a good programmer should be able to come up with code that is fast, reliable and readable in a reasonable amount of time – and that is why you, the contestant, is asked to solve the problem of building a ranking system for us.
You’re probably familiar with the following paragraph taken from Rules of ACM/ICPC:

A problem is solved when it is accepted by the judges. Teams are ranked according to the most problems solved. Teams who solve the same number of problems are ranked by least total time. The total time is the sum of the time consumed for each problem solved. The time consumed for a solved problem is the time elapsed from the beginning of the contest to the submittal of the accepted run plus 20 penalty minutes for every rejected run for that problem regardless of submittal time. There is no time consumed for a problem that is not solved.

During a contest, the following kinds of requests might be submitted by participants:

Submission
(Format: S [minute]:[Team No]:[Problem ID]:[Result])

If a team submits a problem which they have already solved, then this submission should be considered invalid and ignored by your system; otherwise, the submission is valid and saved for further processing – if the result is 1 (Correct), then this submission should be considered accepted by the system.

Query a team’s place on the rank list.
(Format: R [Team No])

Sort the teams according to the contest rules mentioned above. A tie may occur between two teams who solved equal number of problems and same penalty.
Two teams who solved same number of problems and have same penalty should always have same rank; however, when querying the Kth team on the rank list, even if many teams share the same rank, only one team should be printed (please refer to the rule given below).

Query the Kth team on the rank list.
(Format: T [k])

If a tie occurs, return the team with the minimum last accepted submission time. Note that the submission time of two teams might be different (i.e., in seconds) even though it may appear otherwise in the input.
Initially every team has no submissions, and the contest will last no more than 5 hours.

There are multiple test cases in the input.
Each test case starts with two integers, N and M, (1 <= N <= 10000, 1 <= M <= 10), the number of teams participating in the competition, and the total number of problems in the contest. Teams are numbered from 0 to N – 1. Each of the following lines is either in one of the three formats listed above, or a single line "Contest Ends" followed by an empty line indicating the end of current test case. It is guaranteed that there are no more than 100000 requests in each test case.
Input ends with End-of-File.

There are multiple test cases in the input.
Each test case starts with two integers, N and M, (1 <= N <= 10000, 1 <= M <= 10), the number of teams participating in the competition, and the total number of problems in the contest. Teams are numbered from 0 to N – 1. Each of the following lines is either in one of the three formats listed above, or a single line "Contest Ends" followed by an empty line indicating the end of current test case. It is guaranteed that there are no more than 100000 requests in each test case.
Input ends with End-of-File.

5 8
T 1
T 2
S 5:0:A:0
S 8:0:A:1
S 9:1:B:1
S 15:0:A:1
T 1
T 2
T 3
R 0
R 1
R 2
R 3
Contest Ends

0
-1
[0][A]
[1][B]
1
0
2
1
0
2
2

1. 为什么for循环找到的i一定是素数叻，而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak，而你每次取余都用的是原来的m，也就是n