#P5330. [SNOI2019] 数论

[SNOI2019] 数论

Description

Given positive integers P,Q,TP, Q, T, an integer set AA of size nn, and an integer set BB of size mm, please compute:

$$\sum_{i=0}^{T-1}[(i\bmod P) \in A \land (i\bmod Q) \in B]$$

In other words, ask how many non-negative integers xx less than TT satisfy: the remainder of xx divided by PP belongs to AA, and the remainder of xx divided by QQ belongs to BB.

Input Format

The first line contains 55 integers separated by spaces: P,Q,n,m,TP, Q, n, m, T.

The second line contains nn integers separated by spaces, representing the set A={A1,A2,,An}A=\{A_1,A_2,\cdots,A_n\}. It is guaranteed that all AiA_i are pairwise distinct, and 0Ai<P0 \leq A_i < P.

The third line contains mm integers separated by spaces, representing the set B={B1,B2,,Bm}B=\{B_1,B_2,\cdots,B_m\}. It is guaranteed that all BiB_i are pairwise distinct, and 0Bi<Q0 \leq B_i < Q.

Output Format

Output one integer on a single line, denoting the answer.

4 6 3 3 14
0 1 3
2 4 5
4

Hint

For all testdata, 1n,m1061 \leq n,m \leq 10^6, 1P,Q1061 \leq P,Q \leq 10^6, and 1T10181 \leq T \leq 10^{18}.

For 10%10\% of the testdata, T106T \leq 10^6.

For another 20%20\% of the testdata, P,Q1000P,Q \leq 1000.

For another 10%10\% of the testdata, TT is a common multiple of PP and QQ.

For another 10%10\% of the testdata, PP and QQ are coprime, and P,Q105P,Q \leq 10^5.

For another 10%10\% of the testdata, PP and QQ are coprime.

For another 10%10\% of the testdata, P,Q105P,Q \leq 10^5.

For the remaining 30%30\% of the testdata, there are no special constraints.

  • 2023.11.17 Added three groups of hack testdata.

Translated by ChatGPT 5