#P7790. [COCI 2016/2017 #6] Gauss

[COCI 2016/2017 #6] Gauss

Description

You are given KK integers F(1),F(2),...,F(K)F(1), F(2), ..., F(K), and define F(i)=0F(i)=0 for i>Ki>K. You are also given TT lucky integers XiX_i and their prices C(Xi)C(X_i), and MM integers L1,L2,...,LML_1, L_2, ..., L_M.

At the beginning, there is an integer AA on the blackboard. You can perform the following two operations:

  • Let the current number on the blackboard be NN. You may write down any divisor MM of NN that is smaller than NN, paying F(d(N÷M))F(d(N \div M)). Here d(N÷M)d(N \div M) denotes the number of divisors of the positive integer N÷MN \div M (including N/MN/M).

  • If NN is a lucky integer, you may keep NN on the blackboard, paying C(N)C(N).

Define G(A,B,L)G(A,B,L) as the minimum cost to start from AA, perform LL operations, and finally leave BB on the blackboard. Given AA and BB, compute G(A,B,L1)+G(A,B,L2)+...+G(A,B,LM)G(A,B,L_1)+G(A,B,L_2)+...+G(A,B,L_M).

Input Format

The first line contains a positive integer KK.

The second line contains KK positive integers F(1),F(2),...,F(K)F(1), F(2), ..., F(K).

The third line contains a positive integer MM.

The fourth line contains MM positive integers L1,L2,...,LML_1, L_2, ..., L_M.

The fifth line contains a positive integer TT.

The next TT lines each contain two positive integers XiX_i and C(Xi)C(X_i), indicating that XiX_i is a lucky number and its price is C(Xi)C(X_i).

The (T+5)(T+5)-th line contains a positive integer QQ, the number of queries.

The next QQ lines each contain two positive integers AA and BB.

Output Format

For each query, output G(A,B,L1)+G(A,B,L2)+...+G(A,B,LM)G(A,B,L_1)+G(A,B,L_2)+...+G(A,B,L_M).

4
1 1 1 1
2
1 2
2
2 5
4 10
1
4 2 
7
3
6 9 4
2
5 7
3
1 1
7 8
6 10
2
6 2
70 68 
118
-2
3
8 3 10
2
8 4
3
1 6
5 1
3 7
2
5 1
3 1 
16
66

Hint

Sample Explanation #1

Since L1=1L_1=1, replace 44 with 22, so the cost is G(4,2,1)=F(d(2))=1G(4,2,1)=F(d(2))=1.

Since L2=2L_2=2, there are two ways to turn 44 into 22:

  • Replace 44 with 22, and since 22 is a lucky number, keep 22 in the second step. The cost is F(d(4÷2))+C(2)=1+5=6F(d(4\div 2))+C(2)=1+5=6.

  • Since 44 is a lucky number, keep 44 in the first step, then replace 44 with 22 in the second step. The cost is C(4)+F(d(4÷2))=11+1=12C(4)+F(d(4\div 2))=11+1=12.

The first plan is cheaper, so choose the first plan.

Therefore, the answer is G(4,2,1)+G(4,2,2)=1+6=7G(4,2,1)+G(4,2,2)=1+6=7.

Constraints

For 100%100\% of the testdata, 1K1041\le K\le 10^4, 1F(i)1031\le F(i)\le 10^3, 1M1031\le M\le 10^3, 1Li1041\le L_i\le 10^4, 1T501\le T\le 50, 1Xi1061\le X_i\le 10^6, 1C(Xi)1031\le C(X_i)\le 10^3, 1Q5×1041\le Q\le 5\times 10^4, 1A,B1061\le A,B\le 10^6.

Notes

The score of this problem follows the original COCI setting, with a full score of 160160.

Translated from COCI2016_2017 CONTEST #6 T6 GAUSS.

Translated by ChatGPT 5