|A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C|

这个是集合的容斥,交集差集什么的,这个在概率论经常用到吧

4008: The Leaf Eaters  

Time Limit(Common/Java):5000MS/15000MS     Memory Limit:65536KByte
Total Submit: 61            Accepted:18

Description

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.

Input

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.

Output

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.

Sample Input

20 3
3
2
5

Sample Output

6

Hint

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.

举个栗子,比如这道题吧。你可以从1走到20,有3种走路方式,你可以从1开始走三步,走四步,走五步,看下那些地方你没有走到,可能第一次想到的都是用数组标记,可是一看下面的数据量,就瞬间爆炸,所以需要状态压缩,容斥原理就很好的满足了需求,因为要从1开始,不从0开始,所以呢要先-1,这个问题可能会迷。然后DFS搜索下吧,每个都要加1次,假如j是奇数,就是并的要加,如果偶数次就是交集,要减去

#include<stdio.h>
__int64 n,m;
__int64 ans,a[];
__int64 gcd(__int64 a,__int64 b){
return b==?a:gcd(b,a%b);
}
void DFS(int cur,__int64 lcm,int id){
lcm=a[cur]/gcd(a[cur],lcm)*lcm;
if(id&)
ans+=(n-)/lcm;
else
ans-=(n-)/lcm;
for(int i=cur+;i<m;i++)
DFS(i,lcm,id+);
}
int main(){
while(~scanf("%I64d%d",&n,&m)){
for(int i=;i<m;i++)
scanf("%I64d",&a[i]);
ans=;
for(__int64 i=;i<m;i++)
DFS(i,a[i],);
printf("%I64d\n",n-ans);
}
return ;
}

最新文章

  1. AngularJs angular.equals
  2. js获取元素的innerText属性为什么为空
  3. LeetCode Implement pow(x, n).
  4. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
  5. OpenJudge计算概论-扩号匹配问题【这个用到了栈的思想】
  6. JQUERY 判断选择器选择的对象 是否存在
  7. hadoop 异常处理实例(一)hadoop内存配置项
  8. second blog编程之美------控制cpu曲线
  9. copy模块
  10. (札记)Java应用架构设计-模块化模式与OSGi
  11. android 操蛋的gradle
  12. 在.NET项目中使用PostSharp,使用MemoryCache实现缓存的处理(转)
  13. 线下市场,选择微信小程序从未显得如此重要
  14. oracle修改有数据的字段属性
  15. 201521123025《java程序设计》第13周学习总结
  16. 用Spring Tools Suite(STS)开始一个RESTful Web Service
  17. Hdu 5595 GTW likes math
  18. [bzoj4866] [Ynoi2017]由乃的商场之旅
  19. python之字典、列表、元组生成器的使用
  20. 使用kbmmw 的REST 服务实现上传大文件

热门文章

  1. while嵌套应用二:九九乘法表
  2. ReactiveCocoa 响应式函数编程
  3. url post 请求方法
  4. PHP 根据两点的经纬度计算距离
  5. COGS 1913. AC自动机
  6. Python学习日志_2017/09/09
  7. leetcode 127 单词接龙
  8. UVA - 658 It&#39;s not a Bug, it&#39;s a Feature! (隐式图的最短路,位运算)
  9. numpy.random.randint
  10. How to debug add-ins for arcgis