uva - The Lottery(容斥,好题)
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(容斥,好题)的更多相关文章
- hdu1796:容斥入门题
简单的容斥入门题.. 容斥基本的公式早就知道了,但是一直不会写. 下午看到艾神在群里说的“会枚举二进制数就会容斥”,后来发现还真是这样.. 然后直接贴代码了 #include <iostream ...
- 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 ...
- 再探容斥好题——ROOK
这个时候考过:安师大附中集训 Day2 当时看shadowice1984的做法,但是没有亲自写,,, 雅礼集训考试的时候鼓捣半天,被卡常到80pts,要跑9s 卡不动. 正解实际是: 3重容斥 1.随 ...
- UVA 10325 - The Lottery(容斥)
以前做过的一个题,忘记/gcd了,看来需要把以前的东西看一下啊. #include <cstdio> #include <cstring> #include <iostr ...
- HDU 6106 17多校6 Classes(容斥简单题)
Problem Description The school set up three elective courses, assuming that these courses are A, B, ...
- UVA 11806 组合数学+容斥
UVA: https://vjudge.net/problem/UVA-11806 AC代码 #include <bits/stdc++.h> #define pb push_back # ...
- Cheerleaders UVA - 11806(容斥+二进制技巧)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- Make a Crystal UVA - 11014 (容斥定理)
题意:给定一个NxNxN的正方体,求出最多能选几个整数点,使得任意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O),那么所 ...
- uva 10325基础容斥
题目:给你一个数n以及m个数字,问1~n中不能被这m个数字整除的数字的个数. 分析:容斥原理.组合数学.数字1-n中能被a.b整除的数字的个数分别是n/a,n/b: 则1-n中能被a或b整数的数字个数 ...
随机推荐
- js 触摸事件
js触摸事件 应用在移动端 webkit内核都支持. 触摸事件api https://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html 事件 ...
- cookieless domain
概述 什么是cookieless domain?虽然名字中带有cookie,其实完全可以不使用cookie.这只是一种将网页中静态的文本,图片等的域名和主域名相区别开的方法. 主域名难免会使用到coo ...
- SQL修炼道路上必看的书籍
1 SQL应用重构 9787111263586 2 SQL 必知必会(第3版) 9787115162601 3 SQL Server 2005高级程序设计 9787115170798 4 SQL 解惑 ...
- C语言字符转换ASCII码
//函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输 入:ASCII字符//返 回:16进制///////// ...
- Android消息机制之Handler
Android为什么要提供Handler Android建议我们不要在UI线程中执行耗时操作,因为这很容易导致ANR异常(在Android源码中我们可以看到,UI如果对用户的操作超过5秒无响应,就会报 ...
- Android NDK开发(1)----- Java与C互相调用实例详解
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/13/2394153.html 一.概述 对于大部分应用开发者来说可能都不怎么接触到NDK,但如果 ...
- Integer Inquiry(大数相加)
Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his explo ...
- ANTLR3
ANother Tool for Language Recognition start...
- java 去除数组重复数据,并输出重复数据值
/** * 去除重复数据 * @author Sunqinbo */ public class RemoveDuplicateData { public static void main(String ...
- 无法在web服务器上启动调试,服务器不支持对ASP.NET 或ATL Server应用程序进行调试。
无法在web服务器上启动调试,服务器不支持对ASP.NET 或ATL Server应用程序进行调试. a>.DCOM配置里的属性灰色不可用的解决方法, 1>.管理工具->组件服务 ...