|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 ;
}

TOJ 4008 The Leaf Eaters的更多相关文章

  1. TOJ 4008 The Leaf Eaters(容斥定理)

    Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...

  2. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  3. Autumn is a second spring when every leaf is a flower.

    Autumn is a second spring when every leaf is a flower. 秋天即是第二个春天,每片叶子都是花朵.——阿尔贝·加缪

  4. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  5. 23. Sum Root to Leaf Numbers

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  6. hdu 5682 zxa and leaf

    zxa and leaf  Accepts: 25  Submissions: 249  Time Limit: 5000/2500 MS (Java/Others)  Memory Limit: 6 ...

  7. HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树

    zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected ...

  8. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

  9. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

随机推荐

  1. 《高性能MySQL》读书笔记之创建高性能的索引

    索引是存储引擎用于快速找到记录的一种数据结构.索引优化是对查询性能优化的最有效手段.索引能够轻易将查询性能提高几个数量级.创建一个最优的索引经常需要重写查询.5.1 索引基础 在MySQL中,存储引擎 ...

  2. CF1062D Fun with Integers

    思路: 找规律. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ...

  3. 内存泄露,C++

    内存泄露,C++通常是指new出来的内存没有delete掉.在你的代码里边,new了一块内存,然后马上就delete,自然就没有内存泄露了(呃……其实我也不确定啦,因为你用的是delete,而不是de ...

  4. 关于&0xF0的一些认识

    首先,要明白0xF0转换成二进制是多少 ----- 1111 0000(0xF0相当于高四位保留,低四位置为0) 我们拿麻将的一万(0x01).一条(0x11).一筒(0x21) 一万的二进制原码   ...

  5. Yii2.0 Cookies机制和使用方法

    在实际的项目开发过程中,用到了Yii2.0 Cookies机制!但是遇到一个十分奇葩的问题,同一个YII框架,backend下Cookies能够正常存储于客户端,但是frontend始终不行.文章的最 ...

  6. php随机生成国内ip地址

    获得一个国家所有ip段,随机生成国内ip地址的缩水实现.注意:  $ip_long数组中后5个值在64位系统中可能是错误的(下面代码中  $ip_long 数组的后五个值在32位系统中为负数,64位系 ...

  7. XDU——受教了

    存在的问题还是很多的 GG 突然觉得刷题的目的并不是追求A.我们应该在那个过程中提高代码能力和建立模型解题能力 会的算法会巧妙应用才是王道 吐槽自己两句,写高数了

  8. FaceBook pop 动画开源框架使用教程说明

    https://github.com/facebook/pop Pop is an extensible animation engine for iOS and OS X. In addition ...

  9. python学习(day1)

    一.在这次实训之前,虽然听说过很多次python这种语言,但是从来没有真正去学习过,仅仅知道它是一种目前十分流行且功能非常强大的语言,可以方便快捷的实现很多功能.今天的课程带我了解了python,并且 ...

  10. PHP的PDF扩展库TCPDF将中文字体设置为内嵌字体的方法

    1. 下载要设置的字体,如名为simfang.ttf,放在./vendor/tecnickcom/tcpdf/tools目录中 2.在tools目录中按住shift,点击鼠标右键,点击“在此处打开命令 ...