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整数的数字个数 ...
随机推荐
- mvc 防止客服端多次提交
但凡web开发中都会有户多次点击了提交按钮导致多次提交的情况,一般的集中做法 1.通过js在用户点击的时候将按钮disabled掉,但是这样并不是很可靠(我就可以跳过这个,用一个for循环 我直接自己 ...
- C语言字符转换ASCII码
//函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输 入:ASCII字符//返 回:16进制///////// ...
- DEV GridControl 小结(持续添加)
一.属性: 1.Views OptionsBehavior=>Editable:False 列表不可编辑 OptionsSelection=>EnableAppearanceFocuse ...
- Android应用开发提高篇(6)-----FaceDetector(人脸检测)
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/10/2388776.html 一.概述 初次看到FaceDetector这个类时,心里想:And ...
- Queue(队列)
队列是一种后进后出的数据结构,下面介绍一下队列中常见的函数: 一.queue 中的 empty 函数 queue<int> q ; q.empty() ; // 若队列中无元素,返回tr ...
- UVA 10798 - Be wary of Roses (bfs+hash)
10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...
- 采用translate实现垂直水平居中
今天分享一个利用css3新特性实现垂直水平居中的方法. 通过对元素进行绝对定位再配合transform中的translate实现. 代码如下: html <div id="conten ...
- nodejs入门demo
demo的实例引用自:http://www.runoob.com/nodejs/nodejs-event.html, 官方文档:https://nodejs.org/dist/latest-v6.x/ ...
- Symfony框架系列----常用命令
一.常用命令 从Entity操作数据库: app/console doctrine:database:create # 创建数据库 app/console doctrine:schema:update ...
- Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数
python一切皆对象,linux一切皆文件,python操作文件是很常见的O/I操作,其内置来open()函数可以完成文件的基本操作: 一:使用内置open()函数操作文件,基本语法如下: with ...