TOJ 4008 The Leaf Eaters(容斥定理)
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
这题的大概意思是:
有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(容斥定理)的更多相关文章
- 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 ...
- HDU 1796How many integers can you find(简单容斥定理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 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 ...
- hdu_5213_Lucky(莫队算法+容斥定理)
题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...
- How Many Sets I(容斥定理)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...
- HDU - 4135 Co-prime 容斥定理
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...
- BZoj 2301 Problem b(容斥定理+莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 7732 Solved: 3750 [Submi ...
- BZOJ2839 : 集合计数 (广义容斥定理)
题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- js定时更换图片
//定时更换图片: 调用:smileChange.start(); smileChange.stop(); var smileChange = { start: function () { var t ...
- c# 根据文件夹或文件名返回(文件夹或文件)的完整路径
c# 根据文件夹或文件名返回(文件夹或文件)的完整路径 一.方案一:(使用windows API) 二.方案二:(扫描全盘)
- 定时器timer类
timer类 Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法. 构造方法: Timer(interval, function, args=[], kwargs={}) in ...
- 前端中onload与ready的区别
Jquery的ready()与Javascrpit的load() 1 window.onload() $(document).ready() 加载时机 必须等待网页全部加载完毕(包括图片等),然后再执 ...
- kali linux之DNS,NTP放大攻击
DNS放大: 产生大流量的攻击方法-----单机的带宽优势,巨大的单机数量形成的流量汇聚,利用协议特性实现放大效果的流量 DNS协议放大效果----查询请求流量小,但响应流量可能非常巨大(dig AN ...
- php代码审计7审计csrf漏洞
跨站请求伪造,也有人写出xsrf,黑客伪造用户的http请求,然后将http请求发送给存在csrf的网站,网站执行了伪造的http请求,就引发了跨站请求伪造 漏洞危害:攻击者盗用了你的身份信息,以你的 ...
- Java面向对象之继承extends 入门实例
一.基础概念 (一)继承的好处: 1.继承:提高了代码的复用性. 2.让类与类之间产生了关系,给多态这个特征提供了前提. (二)继承的种类: 1.单继承:一个类只能有一个父类. 2.多继承:一个类可以 ...
- JavaBean的概念
Java的一个特性,数据与行为相分离.数据就是成员变量,行为就是setter和getter方法 JavaBean是Java中开发的可以跨平台的重要组件,在JSP中常用来封装业务逻辑和数据库操作.Jav ...
- Azure ASM虚拟机部署反恶意软件-安全扩展
Azure虚拟机,默认情况下没有安装杀毒软件.如果您有此需求可以通过Azure 扩展进行安装,有关Azure反恶意软件的官方说明请参考:https://docs.azure.cn/zh-cn/secu ...
- adb命令connect设备必须添加端口号
Android开发,一般使用adb远程连接设备,习惯性一个命令: adb connect 10.100.1.202 这几天发现一个问题,同一个命令,提示错误: adb connect 10.100.1 ...