10325 - The Lottery

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

题解:容斥,题意是买彩票,在1~N里面找幸运数字,幸运数字与m个数字分别互质,因为互质,所以想到容斥,sum每次加上与lcy不互质的个数,在减去与lcy1,lcy2的最小公倍数不互质的个数。。。。依次。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
LL gcd(int a,int b){return b==0?a:gcd(b,a%b);}
LL m[20];
int N,M;
vector<int>p;
LL rc(){
LL sum=0,lrc;
for(int i=1;i<(1<<M);i++){
p.clear();
for(int j=0;j<M;j++){
if(i&(1<<j)){//1<<j
p.push_back(m[j]);
}
}
lrc=p[0];
for(int j=1;j<p.size();j++)lrc=lrc*p[j]/gcd(lrc,p[j]);
if(p.size()&1)sum+=N/lrc;
else sum-=N/lrc;
}
printf("%lld\n",N-sum);
}
int main(){
while(~scanf("%d%d",&N,&M)){
for(int i=0;i<M;i++)scanf("%lld",m+i);
rc();
}
return 0;
}

  

uva - The Lottery(容斥,好题)的更多相关文章

  1. hdu1796:容斥入门题

    简单的容斥入门题.. 容斥基本的公式早就知道了,但是一直不会写. 下午看到艾神在群里说的“会枚举二进制数就会容斥”,后来发现还真是这样.. 然后直接贴代码了 #include <iostream ...

  2. hdu 1796 How many integers can you find 容斥第一题

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

  3. 再探容斥好题——ROOK

    这个时候考过:安师大附中集训 Day2 当时看shadowice1984的做法,但是没有亲自写,,, 雅礼集训考试的时候鼓捣半天,被卡常到80pts,要跑9s 卡不动. 正解实际是: 3重容斥 1.随 ...

  4. UVA 10325 - The Lottery(容斥)

    以前做过的一个题,忘记/gcd了,看来需要把以前的东西看一下啊. #include <cstdio> #include <cstring> #include <iostr ...

  5. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

  6. UVA 11806 组合数学+容斥

    UVA: https://vjudge.net/problem/UVA-11806 AC代码 #include <bits/stdc++.h> #define pb push_back # ...

  7. Cheerleaders UVA - 11806(容斥+二进制技巧)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  8. Make a Crystal UVA - 11014 (容斥定理)

    题意:给定一个NxNxN的正方体,求出最多能选几个整数点,使得任意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O),那么所 ...

  9. uva 10325基础容斥

    题目:给你一个数n以及m个数字,问1~n中不能被这m个数字整除的数字的个数. 分析:容斥原理.组合数学.数字1-n中能被a.b整除的数字的个数分别是n/a,n/b: 则1-n中能被a或b整数的数字个数 ...

随机推荐

  1. XMLHttpRequest对象的使用

    1.首先要创建XMLHttpRequest对象,这个对象是前台与后台进行异步的重要对象,现在的浏览器有很多种,创建 XMLHttpRequest 的方法也不相同,所以为了兼容各种浏览器,在创建XMLH ...

  2. C#WebService 客户端通过Http调用请求(转)

    1.webservice帮助类 public class WebServiceHelper    {               public static string CallServiceByG ...

  3. [Jobdu] 题目1521:二叉树的镜像

    不知道怎么回事下面的代码通过了4个测试用例,还有1个测试用例始终是Runtime Error,各位帮我看一下是哪里出了问题 镜像输出两种方法,一种是递归进行调整,另外一种就是直接在先序遍历的基础上进行 ...

  4. 算法之旅,直奔<algorithm>之十五 find

    find(vs2010) 引言 这是我学习总结 <algorithm>的第十五篇.关于<algorithm>,每篇都很小,但是都很好用,可以秀出你的个性. 作用 find的作用 ...

  5. Codeforces 116C - Party(dfs)

    n个人,每个人之多有一个上司.“上司”关系具有传递性.求最少将人分成多少组,每组中的每个人的上司或者间接上司都不在该组.拿到题就用树的直径wa了一炮... 正解是有向无环森林的最长路.从每个跟节点df ...

  6. .htaccess Rewrite apache重写和配置

    首先: 必须要空间支持 Rewrite 以及对站点目录中有 .htaccess 的文件解析,才有效. 如何让空间支持Rewrite 和 .htaccess 的文件解析呢 往下看 第一步:要找到apac ...

  7. 转移服务器,DEDE网站遇到no input file specified!

    公司新配置了服务器,需要从旧服务器上把原来的站点迁移到新服务器,迁移.NET网站没有遇到任何问题,但是在迁移过来一个用DEDE做的网站后,访问首页出现No Input File Specified,后 ...

  8. installscript类型 完成时实现推荐安装其他产品的功能

    目前好多软件在安装完成时都有什么 立刻运行.打开网址.推荐安装其他工具等功能 我司领导也追时髦要求了这个功能而且要推荐多个,所以这个功能实现起来就需要自己去写代码了.陆陆续续研究了研究了好长时间,由于 ...

  9. 再探java基础——break和continue的用法

    再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...

  10. Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8176   Accepted: 2862 De ...