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. 微信运动数据抓取(Python)

    "微信运动"能够向朋友分享一个包含有运动数据的网页,网页中就有我们需要的数据.url类似于:http://hw.weixin.qq.com/steprank/step/person ...

  2. MySQL参数log_bin_trust_function_creators

    问题:执行创建函数的sql文件报错如下: [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ...

  3. php tp5常用小知识

    1. tp5 获取当前访问的模块名,控制器名,方法名 $request= \think\Request::instance(); $module = $request->module(); // ...

  4. iframe里面的元素触发父窗口元素事件的jquery代码 转

    例如父窗口定义了一个事件. top: $(dom1).bind('topEvent', function(){}); 那么iframe里面的元素怎样触发父窗口dom1的事件呢?这样吗? $(dom1, ...

  5. SpringBoot中 application.yml /application.properties常用配置介绍

    # Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 10444 ser ...

  6. Qt 学习之路 2(40):隐式数据共享

    Qt 学习之路 2(40):隐式数据共享 豆子 2013年1月21日 Qt 学习之路 2 14条评论 Qt 中许多 C++ 类使用了隐式数据共享技术,来最大化资源利用率和最小化拷贝时的资源消耗.当作为 ...

  7. mysql5.6 的st_distance 实现按照距离远近排序。

    当前所处在的位置(113.858202 , 22.583819 ),需要查询我附近1000米内的小区,并安装由近到远的顺序排列  SELECT s.id,s.name,s.lng,s.lat, rou ...

  8. IDEA 在 专注模式下 显示 行号 和 缩进线...

    16down voteaccepted +50 Open the settings and navigate to Editor > General > Appearance and ti ...

  9. PHP 备份还原 MySql 数据库

    原生 PHP 备份还原 MySql 数据库 支持 MySql,PDO 两种方式备份还原 php5.5 以上的版本建议开启pdo扩展,使用 pdo 备份还原数据 备份文件夹 db_backup.impo ...

  10. ggplot你不知道的细节

    例一 Michaelis-Menten动力学方程 这个例子中采用出自文献中的一组有关于浮萍氮摄取的数据,共2两个变量8个观测值,其中底物浓度与浮萍的氮取速率之间可以通过M-M动力学方程来进行描述.在这 ...