题意:

给n,m,和m个数(k1~km)。求1~n中有多少个数不是(k1~km)中任意一数的倍数。

题解:

容斥模板题。反面考虑,a的倍数有n/a个;既是a,也是b的倍数,即lcm(a,b)的倍数有n/lcm(a,b)个。是a,b,c的倍数,即lcm(a,b,c)的倍数有n/lcm(a,b,c)个。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a[20],m;
LL n,ans; LL lcm(LL a,LL b)
{
return a/__gcd(a,b)*b;
} void dfs(int c,int cur,int i,LL ans1) //dfs(c,1,i,0,1);
{
if(cur==c+1)
{
if(c&1)
ans-=n/ans1;
else
ans+=n/ans1;
return;
}
for(;i<m;i++)
{
dfs(c,cur+1,i+1,lcm(ans1,a[i]));
}
} int main()
{
while(cin>>n>>m)
{
for(int i=0;i<m;i++)
scanf("%lld",&a[i]);
ans=n;
for(int c=1;c<=m;c++)
dfs(c,1,0,1);
printf("%lld\n",ans);
}
}

The Lottery

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

The Sports Association of Bangladesh is in great problem with their latest lottery ‘Jodi laiga Jai’. There are so many participants this time that they cannot manage all the numbers. In an urgent meeting they have decided that they will ignore some numbers. But how they will choose those unlucky numbers!!! Mr. NondoDulal who is very interested about historic problems proposed a scheme to get free from this problem. You may be interested to know how he has got this scheme. Recently he has read the Joseph’s problem. There are N tickets which are numbered from 1 to N. Mr. Nondo will choose M random numbers and then he will select those numbers which is divisible by at least one of those M numbers. The numbers which are not divisible by any of those M numbers will be considered for the lottery. As you know each number is divisible by 1. So Mr. Nondo will never select 1 as one of those M numbers. Now given N, M and M random numbers, you have to find out the number of tickets which will be considered for the lottery.

Input

Each input set starts with two Integers N (10 ≤ N <2^31) and M (1 ≤ M ≤ 15). The next line will contain M positive integers each of which is not greater than N. Input is terminated by EOF.

Output

Just print in a line out of N tickets how many will be considered for the lottery.

Sample Input

10 2

2 3

20 2

2 4

Sample Output

3

10

UVA10325 The Lottery(容斥原理)的更多相关文章

  1. UVA 10325 lottery 容斥原理

    题目链接 给出m个数, 求1-n的范围内, 无法整除这m个数之中任何一个数的数的个数. 设m个数为a[i], 对任意的i, n/a[i]是n中可以整除a[i]的数的个数, 但是这样对于有些数重复计算了 ...

  2. UVA 10325 The Lottery( 容斥原理)

    The Sports Association of Bangladesh is in great problem with their latest lottery `Jodi laiga Jai'. ...

  3. UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)

    UVA.10325 The Lottery (组合数学 容斥原理) 题意分析 首先给出一个数n,然后给出m个数字(m<=15),在[1-n]之间,依次删除给出m个数字的倍数,求最后在[1-n]之 ...

  4. 容斥原理——uva 10325 The Lottery

    首先推荐一篇介绍容斥原理很好的博客http://www.cppblog.com/vici/archive/2011/09/05/155103.html 题意:求1~n中不能被给定m个数中任意一个数整除 ...

  5. HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

    HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...

  6. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  7. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  8. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  9. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

随机推荐

  1. 【Win 10应用开发】在RichEditBox中使用自定义菜单

    前面给大伙儿简单介绍了RichEditBox控件的基本用法,以及解决其中的一些小问题. 本文咱们来看看如何自定义RichEditBox控件的上下文菜单. 原理比较简单,所以先说一说原理.当RichEd ...

  2. 开发node桌面级应用工具:apk转化epub

    随着苹果ibooks对国内的开放,最近接了个麻烦的需求: 把现有的APK转化支持苹果ibooks电子书的epub格式 apk,基本都知道就是安卓的应用程序 epub,是ibooks支持的电子书格式 ( ...

  3. 7. SVM松弛变量

    我们之前讨论的情况都是建立在样例线性可分的假设上,当样例线性不可分时,我们可以尝试使用核函数来将特征映射到高维,这样很可能就可分了.然而,映射后我们也不能100%保证可分.那怎么办呢,我们需要将模型进 ...

  4. 《ES6基础教程》之 map、forEach、filter indexOf 用法

    1,map,对数组的每个元素进行一定操作,返回一个新的数组. var oldArr = [{first_name:"Colin",last_name:"Toh" ...

  5. ElasticSearch 5学习(6)——分布式集群学习分享1

    在使用中我们把文档存入ElasticSearch,但是如果能够了解ElasticSearch内部是如何存储的,将会对我们学习ElasticSearch有很清晰的认识.本文中的所使用的ElasticSe ...

  6. ios 开发需要看的书籍

    1.吴航写的<iOS应用逆向工程 第2版> 2.<iOS 应用安全攻防实战> 3.

  7. SQL连接

    SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:             book表                                          stu表 1.内连接 ...

  8. [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一)

    [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一) Date  周二 06 一月 2015 By 钟谢伟 Tags mvc4 / asp.net 示 ...

  9. jQuery中怎样阻止后绑定事件

    你的代码在页面载入过程中已经完成事件绑定了,没有阻止后绑定的事件的办法了,不过可以删除当前指定节点的事件绑定.方法如下:$("#btn").click(function(){if( ...

  10. [unity]UGUI界面滑动,ScrollRect嵌套滑动

    原因:老板蛋痛,让我去抄皇室战争. 思路:我大概知道ngui(后来改成UGUI的)里面有个ScrollView.于是我就想一个横着的SV加上5个竖的SV不就好了吗. 过程: 于是 但是有个问题就是UI ...