【容斥定理】台州oj 4008

4008: The Leaf Eaters 分享至QQ空间 去爱问答提问或回答

时间限制(普通/Java):5000MS/15000MS     内存限制:65536KByte
总提交: 68            测试通过:20

描述

 As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eats as much of it as it can (or wants), then stretches out to its full length to reach a new leaf with its front end, and finally "hops" to it by contracting its back end to that leaf.

We have with us a very long, straight branch of a tree with leaves distributed uniformly along its length, and a set of caterpillars sitting on the first leaf. (Well, our leaves are big enough to accommodate upto 20 caterpillars!). As time progresses our caterpillars eat and hop repeatedly, thereby damaging many leaves. Not all caterpillars are of the same length, so different caterpillars may eat different sets of leaves. We would like to find out the number of leaves that will be undamaged at the end of this eating spree. We assume that adjacent leaves are a unit distance apart and the length of the caterpillars is also given in the same unit.

For example suppose our branch had 20 leaves (placed 1 unit apart) and 3 caterpillars of length 3, 2 and 5 units respectively. Then, first caterpillar would first eat leaf 1, then hop to leaf 4 and eat it and then hop to leaf 7 and eat it and so on. So the first caterpillar would end up eating the leaves at positions 1,4,7,10,13,16 and 19. The second caterpillar would eat the leaves at positions 1,3,5,7,9,11,13,15,17 and 19. The third caterpillar would eat the leaves at positions 1,6,11 and 16. Thus we would have undamaged leaves at positions 2,8,12,14,18 and 20. So the answer to this example is 6.

 

输入

The first line of the input contains two integers N and K, where N is the number of leaves and K is the number of caterpillars. Lines 2,3,...,K+1 describe the lengths of the K caterpillars. Line i+1 (1 ≤ i ≤ K) contains a single integer representing the length of the ith caterpillar.

You may assume that 1 ≤ N ≤ 1000000000 and 1 ≤ K≤ 20. The length of the caterpillars lie between 1 and N.

输出

A line containing a single integer, which is the number of leaves left on the branch after all the caterpillars have finished their eating spree.

 

样例输入

样例输出

提示

You may use 64-bit integers (__int64 in C/C++) to avoid errors while multiplying large integers. The maximum value you can store in a 32-bit integer is 2^31-1, which is approximately 2 * 10^9. 64-bit integers can store values greater than 10^18.

题目来源

TOJ

题目上传者

crq


[提交] [提交状态] [讨论版]

题意:有至多20个虫子,每个虫子有各自的长度,刚开始每个虫子都在第一个叶子,长度为k的虫子会接下来吃第1+k个叶子,问最后还剩下多少个叶子没有吃;

思路:这一题就是个开灯问题,仅把为开灯的状态变为开灯,利用容斥定理;

代码:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值