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

  1. Input:
  2. 3
  3. 2 1
  4. 3 4
  5. 4 4
  6. 4
  7. 1
  8. 3
  9. 5
  10. 7
  11. Output:
  12. -1
  13. 1
  14. 2
  15. 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​\) 记录了选取的颜色种类数,其余与前述相同。

比较懒,不写代码了。实在需要可以看官方题解。

参考

官方题解

这篇题解基本上是官方题解的简化翻译版本

CodeChef-LECOINS Little Elephant and Colored Coins 题解的更多相关文章

  1. CodeChef:Little Elephant and Colored Coins

    类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...

  2. CodeChef - LEMOVIE Little Elephant and Movies

    Read problems statements in Mandarin Chineseand Russian. Little Elephant from Zoo of Lviv likes to w ...

  3. [CF780C]Andryusha and Colored Balloons 题解

    前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...

  4. Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形

    题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...

  5. Colored Boots题解

    题目来自Codeforce 1141Dhttp://codeforces.com/problemset/problem/1141/D 因为是全英文题面,就先简单的阐述一下题面. 首先输入一个数n,然后 ...

  6. 2018 ACM-ICPC南京区域赛题解

    解题过程 开场开A,A题shl看错题意,被制止.然后开始手推A,此时byf看错E题题意,开始上机.推出A的规律后,shl看了E题,发现题意读错.写完A题,忘记判断N=0的情况,WA+1.过了A后,sh ...

  7. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  8. 【翻译】A Next-Generation Smart Contract and Decentralized Application Platform

    原文链接:https://github.com/ethereum/wiki/wiki/White-Paper 当中本聪在2009年1月启动比特币区块链时,他同时向世界引入了两种未经测试的革命性的新概念 ...

  9. Ethereum White Paper

    https://github.com/ethereum/wiki/wiki/White-Paper White Paper EditNew Page James Ray edited this pag ...

随机推荐

  1. 利用python3监控服务器状态进行邮件报警

    在正式的生产环境中,我们常常会需要监控服务器的状态,以保证公司整个业务的正常运转,常常我们会用到像nagios.zabbix这类工具进行实时监控,那么用python我们怎么进行监控呢?这里我们利用了p ...

  2. 【实验课选题详解】用C语言实现万年历

    题目要求 编程实现万年历,要求: 可根据用户输入或系统日期进行初始化,如果用户无输入则显示系统日期所在月份的月历,并突出显示当前日期: 可根据用户输入的日期查询,并显示查询结果所在月份的月历,突出显示 ...

  3. k8s部署之系统初始化(一)

    初始化 1.安装依赖包 yum -y install tree lrzsz nmap nc telnet vim wget lsof network-tools bash-completion bas ...

  4. Learn day8 re正则表达式\search函数\反射\tcp发送消息(循环)\udp发送消息

    1.匹配单个字符 # ### 正则表达式 - 单个字符匹配 import re ''' findall 把匹配的结果直接返回到列表中 lst = re.findall("正则表达式" ...

  5. 错误解析:org.apache.catalina.LifecycleException: Protocol handler start failed

    以下是报错代码: org.apache.catalina.LifecycleException: Protocol handler start failed at org.apache.catalin ...

  6. c# 创建delphi的代码

    需求: 有一个专业表,没有插入数据,读取的excel,复制为txt文件 每行读取 并且 将汉字的一转换为数字1 private void barButtonItem4_ItemClick(object ...

  7. XDG0062 XAML 与XDG0008 XAML 错误的解决办法

    昨天在使用wpf开发系统的时候,突然出现了错误(其实也不能称为错误,就是打开XAML文件时,在解决方案管理器中出现错误提示,XAML编辑文档出现波浪线的提示 The XAML designer dis ...

  8. 在java9+版本中,接口的内容和注意

    1.成员变量其实就是常量,格式: [public] [static] [final] 数据类型 常量名称 = 数据值: 注意: 常量必须进行赋值,而且一旦赋值不能改变. 常量名称完全大写,用下划线进行 ...

  9. pandas.DataFarme内置的绘图功能参数说明

    可视化是数据探索性分析及结果表达的一种非常重要的形式,因此打算写一个python绘图系列,本文是第一篇,先说一下pandas.DataFrame.plot()绘图功能. pandas.DataFram ...

  10. Django 配置 Mysql

    先安装mysqlclient pip install mysqlclient sttings中的 DATABASES = { 'default': { 'ENGINE': 'django.db.bac ...