URI Online Judge | 1356

Jupiter Attacks!

By Fidel I. Schaposnik Argentinaa

Timelimit: 3

Jupiter is invading! Major cities have been destroyed by Jovian spacecrafts and humanity is fighting back. Nlogonia is spearheading the counter-offensive, by hacking into the spacecrafts’ control system.

Unlike Earthling computers, in which usually a byte has 28 possible values, Jovian computers use bytes with B possible values, {0,1,...,B−1}. Nlogonian software engineers have reverse-engineered the firmware for the Jovian spacecrafts, and plan to sabotage it so that the ships eventually selfdestruct.

As a security measure, however, the Jovian spacecrafts run a supervisory program that periodically checks the integrity of the firmware, by hashing portions of it and comparing the result against known good values. To hash the portion of the firmware from the byte at position i to the byte at position j, the supervisor uses the hash function

where P is a prime number. For instance, if B = 20 and P = 139, while bytes 2 to 5 of the firmware have the values f2 = 14, f3 = 2, f4 = 2 and f5 = 4 then

H(f2, . . . f5) = Bf5 + Bf4 + B2 f3 + Bf2 (mod P)
                                      = 200 × 4 + 201 × 2 + 202 × 2 + 203 × 14 (mod 139)
               = 4 + 40 + 800 + 112000 (mod 139)
= 112844 (mod 139)      
= 115                            

The Nlogonian cryptologists need to find a way to sabotage the firmware without tripping the supervisor. As a first step, you have been assigned to write a program to simulate the interleaving of two types of commands: editing bytes of the firmware by the Nlogonian software engineers, and computing hashes of portions of the firmware by the Jovian supervisory program. At the beginning of the simulation the value of every byte in the firmware is zero.

Input

Each test case is described using several lines. The first line contains four integers B, P, L and N, where B is the number of possible values of a Jovian byte, P is the modulus of the Jovian hash (2 ≤ B < P ≤ 109 and P prime), L is the length (number of Jovian bytes) of the spacecrafts’ firmware, and N is the number of commands to simulate (1 ≤ L, N ≤ 105). At the beginning of the simulation the value of every byte in the firmware is fi = 0 for 1 ≤ iL. Each of the next N lines describes a command to simulate. Each command description starts with an uppercase letter that is either ‘E’ or ‘H’, with the following meanings.

The last test case is followed by a line containing four zeros.

Output

For each test case output the results of the hash commands in the input. In the i-th line write an integer representing the result of the i-th hash command. Print a line containing a single character '-' (hyphen) after each test case.

Sample Input Sample Output

20 139 5 7
E 1 12
E 2 14
E 3 2
E 4 2
E 5 4
H 2 5
E 2 14
10 1000003 6 11
E 1 3
E 2 4
E 3 5
E 4 6
E 5 7
E 6 8
H 1 6
E 3 0
E 3 9
H 1 3
H 4 6
999999935 999999937 100000 7
E 100000 6
E 1 7
H 1 100000
E 50000 8
H 1 100000
H 25000 75000
H 23987 23987
0 0 0 0

115
-
345678
349
678
-
824973478
236724326
450867806
0
-