POJ 1742 Coins

  • 2018-07-16
  • 35
  • 0

题目:

People in Silverland use coins.They have coins of value A1,A2,A3…An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input:

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

Output:

For each test case output the answer on a single line.

Sample Input:

3 10

1 2 4 2 1 1

2 5

1 4 2 1

0 0

Sample Output:

8

4

题目链接

本篇博文以

牛客练习赛22 C 简单瞎搞题

为基础。

在“牛客练习赛22 C 简单瞎搞题”中是选择n个区间中选择任意一个数对其平方求和,算种类数,先不看平方就是n个区间中选择任意一个数对其求和,算种类数,当区间长度为1时就和这道题特别像。

可以用一个bitset对每个数进行循环记录求和种类,写法为

相当于对每种面值的硬币的每一个都循环计数一次,但是这种写法会超时,需要优化为:

这么优化的原理是:

以数据

1 10

1 6

为例,在循环中按照原来的写法第一个数需要经过6次循环,其中过程为(取s后10位):

  • 1100000000(向左移动1位)
  • 1110000000(向左移动2位)
  • 1111000000(向左移动3位)
  • 1111100000(向左移动4位)
  • 1111110000(向左移动5位)
  • 1111111000(向左移动6位)

这样的数据可以解释为若硬币1有六枚则代表这枚硬币的1被记录于s[1~6],但是移动了六次才记录完成,这里就可以优化:

  • 第一次向左移动一位:1100000000
  • 第二次可以两个一一起向左移动两位,这样第三位上也会被第一位移动后填充1:111100000

    循环结束,这时c=2,再向左移动两位即可。因为本来就有n个1整体向左移动n位就会产生2n个1。

最后题目问的是1~m之间的种类数,需要在1~m中循环遍历统计种类数。

AC代码:

评论

还没有任何评论,你来说两句吧