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.

Source

TOJ

这题的大概意思是:
有N个数,从1开始开始走每次走M个数,然后求没有走过的数的个数。
比如N=20,从1开始走每次走3步,即走到的数有1,4,7,10,13,16,19;
从1开始走每次走2步,即走到的数有1,3,5,7,9,11,13,15,17,19;
从1开始走每次走5步,即走到的数有1,6,11,16;
最后有2,8,12,14,18,20没有走到,一共有6个数,所以输出6。

这题可以转换成从1-20之间找出不被2,3,5整除的个数,根据容斥定理,解得此题!

【容斥定理】
|A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C|

相交表示N个条件同时符合。

 #include <stdio.h>
__int64 N,K,V[];
__int64 sum; __int64 gcd(__int64 a, __int64 b){
if(a%b==)return b;
else return gcd(b,a%b);
}
__int64 lcm(__int64 a, __int64 b){
return a/gcd(a,b)*b;
} void dfs(int i, int flag, __int64 v){
if(i==K){
if(flag> && flag%){
sum+=(N-)/v;
}else if(flag> && !(flag%)){
sum-=(N-)/v;
}
return;
}
dfs(i+,flag,v);
dfs(i+,flag+,lcm(V[i],v));
}
int main()
{
while(scanf("%I64d %I64d",&N,&K)!=EOF){
for(int i=; i<K; i++){
scanf("%I64d",&V[i]);
}
sum=;
dfs(,,);
printf("%I64d\n",N-sum);
}
return ;
}

TOJ 4008 The Leaf Eaters(容斥定理)的更多相关文章

  1. TOJ 4008 The Leaf Eaters

    |A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C| 这个是集合的容斥,交集差集什么的,这个在概率论经常用到吧 4008: The Leaf Eaters   T ...

  2. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  4. hdu_5213_Lucky(莫队算法+容斥定理)

    题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...

  5. How Many Sets I(容斥定理)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...

  6. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  7. BZoj 2301 Problem b(容斥定理+莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 7732  Solved: 3750 [Submi ...

  8. BZOJ2839 : 集合计数 (广义容斥定理)

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  9. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. MVC - Model - Controller - View

    一. Model 1.1 在ASP.NET MVC 中 model 负责的是所有与 "数据“  相关的的任务. 也可以把Model 看成是 ASP.NET  中三层模式的 BLL层 加 DA ...

  2. 做ETL的时候用到的数据同步更新代码

    这里是用的从一个库同步到另一个库,代码如下 private void IncrementalSyncUpdate(string fromConn, string toConn, Dictionary& ...

  3. dynamic的好处

    dynamic 可在反射.json反序列化时使用.已达到减少代码量的效果.看代码 using System; namespace ConsoleApp2 { class Program { stati ...

  4. WebStrom-JS编程小技巧

    快速打印某个名为***的对象:***.log回车效果如下:

  5. string类------新标准c++程序设计

    定义: string类是STL中basic_string模板实例化得到的模板类.其定义如下: typedef basic_string<char>string; 构造函数: string类 ...

  6. javaee--学生成绩录入与显示--Struts2标签的使用

    类Score.java:各课程的成绩及平均成绩 类Student.java:学生姓名.学号及Score类 类ScoreAction.java:将Student类存在一个List对象中, execute ...

  7. windows server2008虚拟机系统盘扩容

    windows server2008虚拟机的系统盘空间过小,对系统盘进行扩容,操作如下: 1.将虚拟机关机: 2.VMware对该虚拟机进行设置,选中磁盘,点击扩容,输入扩容大小,等待扩容完成: 3. ...

  8. Django 使用第三方服务发送电子邮件

    在 Django 网站中使用 mailgun 的邮件收发服务. 1.在 mailgun 官网上注册个账号(免费,免费账号每个月有10000条收发邮件的服务,对我来说已经完全够用了),注册完成后界面如图 ...

  9. mac下对NTFS格式的磁盘进行读写操作

    mac对NTFS格式的分区读写有很大的限制,网上看到很多相关的文章,都表明了一个信息:需要购买类似NTFS for mac这样的软件才能实现对NTFS格式的分区读写的权限,其实不然,mac自带的hdi ...

  10. 使用过多的递归出现错误,“System.StackOverflowException”类型的未经处理的异常在 mscorlib.dll 中发生

    class Program { static void Main(string[] args) { sub(0); }     private static void sub(int count) { ...