H. Alice's Number Game time limit per test2 seconds memory limit per test256 megabytes…
- User
H. Alice's Number Game time limit per test2 seconds memory limit per test256 megabytes Alice, an aspiring mathematician, is engaged in a challenging game set up by her mentor. She has a list of integers Γ(1),Γ(2),…,Γ(K) , representing certain properties of numbers. For all indices t>K , Γ(t) is defined to be zero. Additionally, Alice possesses a special list of "favorable numbers," each associated with a specific cost C(X) , where X is a favorable number.
Initially, a positive integer A is written on the board. Alice's task is to transform this number into another positive integer B by performing exactly L moves. Each move has two possible actions:
If N is the current number on the board, she can replace it with one of its divisors M , where M<N . The cost of this move is determined by Γ(d(N/M)) , where d(N/M) is the count of all divisors of N/M , including itself. If N is one of the favorable numbers, Alice can choose to keep it on the board at the cost C(N) . The objective is to minimize the cumulative cost of these L moves while ensuring that B is the final number on the board after the last move. If it's impossible to reach B in L moves, Alice records the result as −1 for those moves. Her mentor provides Q challenges, each containing specific starting and ending numbers A and B . For each challenge, Alice must compute the total cost across various move counts L1,L2,…,LM , summing up Λ(A,B,L1)+Λ(A,B,L2)+…+Λ(A,B,LM) , where Λ(A,B,L) represents the minimal cost for exactly L moves.
Input The first line contains an integer K (1≤K≤10,000) .
The second line lists K integers Γ(1),Γ(2),…,Γ(K) , each value being ≤1,000 .
The third line has an integer M (1≤M≤1,000) .
The fourth line lists M integers L1,L2,…,LM (1≤Li≤10,000) .
The fifth line has an integer T (1≤T≤50) representing the count of favorable numbers.
The next T lines each contain two integers X and C(X) (1≤X≤1,000,000;1≤C(X)≤1,000) representing a favorable number and its cost.
The next line contains an integer Q (1≤Q≤50,000) .
The following Q lines each contain two integers A and B (1≤A,B≤1,000,000) .
Output For each challenge, output the total cost for the given move counts, each result on a new line.
Examples InputCopy 4 1 1 1 1 2 1 2 2 2 5 4 10 1 4 2 OutputCopy 7 InputCopy 3 6 9 4 2 5 7 3 1 1 7 8 6 10 2 6 2 70 68 OutputCopy 118 -2 InputCopy 3 8 3 10 2 8 4 3 1 6 5 1 3 7 2 5 1 3 1 OutputCopy 16 66 Note In the first challenge:
For L1=1 , Alice changes 4 to 2 , costing Γ(d(2))=1 .
For L2=2 , Alice has two options:
-
Replace 4 with 2 and keep 2 (as it is a favorable number) for a cost of Γ(d(4/2))+C(2)=1+5=6 .
-
Keep 4 initially and replace it with 2 in the second move, costing C(4)+Γ(d(4/2))=10+1=11 .
The first path is optimal for L2 , giving Λ(4,2,2)=6 .
-
- Assistant