#P4527. [CTSC2008] 奥运抽奖

[CTSC2008] 奥运抽奖

Description

With 9090 days left before the opening of the Beijing 20082008 Olympics, CTSC plans to hold a lottery for volunteers. As one of the volunteers, you are very excited about it.

The CTSC committee introduces the rules. Suppose there are pp volunteers in total. At the beginning, each volunteer receives a distinct number from 00 to p1p-1.

In the center of the screen are the avatars of the five Fuwa (Beibei, Jingjing, Huanhuan, Yingying, Nini), blinking to welcome everyone. When the lottery starts, the staff presses the button next to the screen and waits for the screen to stop. At that moment, the Fuwa stop blinking.

Of course, when the screen stops, some Fuwa may have their eyes open while others have them closed. If all Fuwa have their eyes closed, the staff presses the button again, repeating this until at least one Fuwa has eyes open. Then, the staff observes which Fuwa have their eyes open.

The five Fuwa are labeled as follows: Beibei, Jingjing, Huanhuan, Yingying, Nini are labeled 22, 33, 44, 55, 66 respectively (the staff think 00 and 11 are not good numbers). Lucky numbers are defined as follows:

  • If a Fuwa’s eyes are open, then his/her label is a lucky number.
  • If numbers l1l_1 and l2l_2 (possibly equal) are lucky numbers, then their product is also a lucky number.
  • All other numbers are not lucky.

Let LL denote the set of all lucky numbers. For example, if Beibei and Jingjing have eyes open while Huanhuan, Yingying, and Nini have eyes closed, then L={2,3,4,6,8,9,12,}L=\{2,3,4,6,8,9,12,\dots\}. Let l(x)l(x) denote the xx-th lucky number in ascending order. For example, in the above case, l(1)=2l(1)=2, l(4)=6l(4)=6, and so on.

Next, the staff randomly generates two numbers; let the smaller one be aa and the larger one be bb. Define the set Ta,bT_{a,b} by

$$T_{a,b}=\{\,d \mid d\in L,\ l(a)\mid d,\ d\mid l(b)\,\}$$

(where xyx\mid y means xx divides yy).

Define the characteristic value ff of a finite subset of natural numbers as follows:

  • The characteristic value of the empty set is 00.
  • For a non-empty set SS, let dd be the smallest element of SS. Then
$$f(S)=d+f(S\backslash d)+q\times d\times f(S\backslash d),$$

where S\dS\backslash d denotes the set obtained by removing the element dd from SS, and qq is a given non-negative integer.

After aa and bb are generated, the winner’s number is the remainder when f(Ta,b)f(T_{a,b}) is divided by pp. The staff will generate many pairs (a,b)(a,b), thus producing multiple winners.

However, the on-site program takes a long time to compute the winners. Eager to know the results, you decide to rewrite the computation program, and your eyes turn to the keyboard in front of you.

Input Format

The first line contains five space-separated numbers, each either 00 or 11, indicating whether Beibei, Jingjing, Huanhuan, Yingying, and Nini have their eyes open. Here 00 means closed and 11 means open. The 55 numbers cannot all be 00.

The second line contains two space-separated numbers pp and qq. Here pp is the number of volunteers, and qq is as described above for computing the characteristic value of a set.

The third line contains the number nn, the number of generated pairs (a,b)(a,b).

Each of the next nn lines contains two space-separated integers aa and bb, representing one lottery draw.

Output Format

Output nn lines, each containing one integer, the winner’s number for that draw. The order corresponds one-to-one with the nn pairs (a,b)(a,b) in the input. Of course, a person may win multiple times.

1 0 0 1 0
10001 2
3
1 10
2 12
4 15
3265
5816
0

Hint

Sample explanation:

Beibei and Yingying have their eyes open. Therefore, the first 1515 lucky numbers are 2,4,5,8,10,16,20,25,32,40,50,64,80,100,1252,4,5,8,10,16,20,25,32,40,50,64,80,100,125.

l(1)=2l(1)=2, l(10)=40l(10)=40. The lucky numbers that are multiples of 22 and divisors of 4040 are 2,4,8,10,20,402,4,8,10,20,40. So T1,10={2,4,8,10,20,40}T_{1,10}=\{2,4,8,10,20,40\}. The characteristic value of T1,10T_{1,10} is computed as follows:

f()=0f(\emptyset)=0

f({40})=40+0+2×40×0=40f(\{40\})=40+0+2\times40\times0=40

f({20,40})=20+40+2×20×40=1660f(\{20,40\})=20+40+2\times20\times40=1660

f({10,20,40})=10+1660+2×10×1660=34870f(\{10,20,40\})=10+1660+2\times10\times1660=34870

$f(\{8,10,20,40\})=8+34870+2\times8\times34870=592798$

$f(\{4,8,10,20,40\})=4+592798+2\times4\times592798=5335186$

$f(\{2,4,8,10,20,40\})=2+5335186+2\times2\times5335186=26675932$

Therefore, the winner’s number is the remainder of 2667593226675932 divided by 1000110001, which is 32653265. Similarly, T2,12={4,8,16,32,64}T_{2,12}=\{4,8,16,32,64\}, whose characteristic value is 2116793221167932, and its remainder modulo 1000110001 is 58165816. Also, T4,15=T_{4,15}=\emptyset.

Constraints:

  • For 20%20\% of the testdata, 1ab10001 \le a \le b \le 1000, n2000n \le 2000.
  • For 60%60\% of the testdata, pp is prime.
  • For 100%100\% of the testdata, 1ab1051 \le a \le b \le 10^5, n105n \le 10^5, p,q2×109p, q \le 2\times 10^9.

Translated by ChatGPT 5