1. 程式人生 > 實用技巧 >CodeChef-LECOINS Little Elephant and Colored Coins 題解

CodeChef-LECOINS Little Elephant and Colored Coins 題解

CodeChef-LECOINS Little Elephant and Colored Coins

Little Elephant and Colored Coins

The Little Elephant from the Zoo of Lviv very likes coins. But most of all he likes colored coins.

He has N types of coins, numbered from 1 to N, inclusive. The coin of the i-th type has the value Vi dollars and the color Ci

. Note that he has infinite supply of each type of coins.

The Little Elephant wants to make exactly S dollars using the coins. What is the maximal number of different colors he can use to make exactly S dollars using some of the coins he has? If it's impossible, output -1. Also note that the Little Elephant wants to know this for many values of S

.

Input

The first line of the input contains a single integer N, denoting the number of types of coins. Each of the following N lines contains two space-separated integers Vi and Ci, denoting the value and the color of the coin of the i-th type. The next line contains a single integer Q, denoting the number of values of S

to process. Each of the following Q lines contains a single integer S, denoting the coinage you should represent via given coins using maximum number of colors.

Output

For each value of S in the input, output the maximum number of different colors in the representation of S or -1 if it is impossible to represent S via given coins.

Constraints

  • 1N30
  • 1Vi200000 (2 * 10^5)
  • 1Ci10^9
  • 1Q200000 (2 * 10^5)
  • 1S10^18

Example

Input:
3
2 1
3 4
4 4
4
1
3
5
7
Output:
-1
1
2
2

Explanation

  • It is not possible to represent S = 1 since every coin has value more than 1.
  • S = 3 can only be represented using one coin of the second type, hence only one color is used in the representation.
  • S = 5 can only be represented as 2 + 3, which leads to two colors used.
  • For S = 7 we have two representations as 2 + 2 + 3 (with two colors used) and 3 + 4 (with one color used). Hence, the answer is 2.

妙題啊。

可達性

如果有個 \(C_i \le 1\) 的SubTask的話會更容易想到正解。

我們先不考慮顏色,只考慮是否能找到一種方案把硬幣總面值湊成 \(S\)

有一種非常樸素的想法是從 \(0\)\(S\) 做可達性dp: $f_{x+V_i} = f_{x+V_i} | f_x $ ,不過因為 \(S \le 10^{18}\) ,顯然這是天方夜譚。

可達性dp只有 \(0/1\) 兩種狀態,非常浪費;發現 \(V_i\) 的值域很小,這啟示我們將 \(V_i\) 作為狀態減少有效狀態數。但究竟如何設定呢?

這裡是最巧妙的地方。不失一般性,設 \(V_1 = \min V_i\) 。我們嘗試把 \(V_1\) 孤立出來考慮。 假設保證過程中最終選擇的總面額永遠不會比 \(S\) 大(換句話說,允許選擇負數個硬幣),那麼只需知道硬幣集合 \(V - \{ V_1 \}\) 中是否存在一種方案其總面值模 \(V_i\)\(S\) 同餘即可。這樣就只需對去掉 \(V_1\) 的硬幣集合做 \(1\)\(V_1- 1\) 的可達性dp就好了。

考慮把前面那個假設幹掉。之前提到 \(f​\) 只有 \(0/1​\) 兩種狀態,明顯可以再塞點東西進去。於是令 \(f_x​\) 為硬幣集合 \(V - \{ V_1 \}​\) 中所有總面值模 \(V_1​\) 等於 \(x​\) 的選擇方案中最小的總面值。容易寫出狀態轉移

\[\mathrm{relax} \ f_{(x+V_i)\% V_1} \ \mathrm{by} \ f_x + V_i \]

\(\%\) 代表取模, \(\mathrm{relax} \ A \ \mathrm{by} \ B\)\(A = \min(A,B)\)

這樣一來,若 \(f_{S \% V_1} \le S\) ,那麼 \(S\) 就是可以被組成的。

發現這個dp就是個最短路,形成了一個 \(|V| = V_1, |E| = (n-1) V_1​\) 的圖。於是 Dijkstra 一下就可以了。不過這個圖還有更好的性質。

首先,路徑中邊的順序可以任意調換而不影響可達性和最短路,所以我們可以分開考慮每一種邊的鬆弛。而一分開看就非常明朗了,根據數論常識,所有 \(x\)\((x + V_i) \% m\) 的邊會在圖上形成 \(\gcd(V_i, m)\) 個大小為 \(\frac{V_1}{\gcd({V_i, m})}\) 的環,於是現在我們又可以分別考慮每一個環。只需從環上當前距離值最小的點繞環一圈就可以做到鬆弛了。於是現在預處理的時間複雜度是 \(O(|E|) = O(nV_1)\) ,比直接 Dijkstra 少了個 \(\log\) 。詢問當然是 \(O(1)​\) 的。

考慮顏色

把顏色放到dp狀態裡面去就好了。設 \(f_{x, c}\)\(c​\) 記錄了選取的顏色種類數,其餘與前述相同。

比較懶,不寫程式碼了。實在需要可以看官方題解。

參考

官方題解

這篇題解基本上是官方題解的簡化翻譯版本

2020/11/18