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. des 加密 iOS

    转载请注明原文出处 生成同名的Des.h文件和Des.m文件后,拷贝下方的代码可直接使用.注意在Des.m文件中将key值修改为项目所需的key.--->  #define DesKey 过程很 ...

  2. Hibernate学习之缓存简析

    一.一级缓存 Hibernate的Session提供了一级缓存的功能,默认总是有效的,当应用程序保存持久化实体.修改持久化实体时,Session并不会立即把这种改变提交到数据库,而是缓存在当前的Ses ...

  3. PHP的一些函数

    //进制转换类 base_convert //字符转十六进制 binhex

  4. ssh远程登录linux服务器

    ssh远程登录linux服务器 用法: ssh -l user -p port server_ip 或者 ssh -p port user@server_ip 参数: -l 后接要登录的远程系统用户名 ...

  5. cocos2dx定时器

    cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce 首先,什么是定时器呢?或许你有时候会想让某个函数不断的去执行,或许只是执行一次,获取你 ...

  6. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  7. SqlServer2012导入Oracle详细案例

    第一次使用SqlServer2012,界面和VS2012风格一致了,Great! 进入正题,这篇博文主要写一下自己亲测的一个案例,使用SqlServer2012的“导入和导出数据”功能向Oracle1 ...

  8. swjtu 1962 A+B(模拟)

    题目链接:http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1962 问题思路:考察编程基础的问题,涉及到字符串转为数字的问题. 代 ...

  9. I can do it!(贪心)

    I can do it! Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  10. Duplicate entry &#39;97112&#39; for key 1

    1.错误描写叙述 2014-07-08 10:27:13,939 ERROR(com.you.conn.JDBCConnection:104) -com.mysql.jdbc.exceptions.j ...