UVA 10325 lottery 容斥原理
给出m个数, 求1-n的范围内, 无法整除这m个数之中任何一个数的数的个数。
设m个数为a[i], 对任意的i, n/a[i]是n中可以整除a[i]的数的个数, 但是这样对于有些数重复计算了, 那么就需要减去一些数, 对任意两个数, 设x为这两个数的lcm, 那么需要减去n/lcm,然后加上任意三个数的n/lcm....... 就这样类推。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int a[];
ll gcd(ll x, ll y) {
return y==?x:gcd(y, x%y);
}
ll lcm(ll x, ll y) {
return x/gcd(x, y)*y;
}
int main()
{
int n, m;
while(cin>>n>>m) {
for(int i = ; i<m; i++)
scanf("%d", &a[i]);
ll ans = ;
for(int i = ; i<(<<m); i++) {
ll cnt = , mul = ;
for(int j = ; j<m; j++) {
if((<<j)&i) {
mul = lcm(mul, 1LL*a[j]);
cnt++;
}
}
if(cnt&) {
ans += n/mul;
} else {
ans -= n/mul;
}
}
cout<<n-ans<<endl;
}
return ;
}
UVA 10325 lottery 容斥原理的更多相关文章
- UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)
UVA.10325 The Lottery (组合数学 容斥原理) 题意分析 首先给出一个数n,然后给出m个数字(m<=15),在[1-n]之间,依次删除给出m个数字的倍数,求最后在[1-n]之 ...
- UVA 10325 The Lottery( 容斥原理)
The Sports Association of Bangladesh is in great problem with their latest lottery `Jodi laiga Jai'. ...
- 容斥原理——uva 10325 The Lottery
首先推荐一篇介绍容斥原理很好的博客http://www.cppblog.com/vici/archive/2011/09/05/155103.html 题意:求1~n中不能被给定m个数中任意一个数整除 ...
- UVA 10325 - The Lottery(容斥)
以前做过的一个题,忘记/gcd了,看来需要把以前的东西看一下啊. #include <cstdio> #include <cstring> #include <iostr ...
- uva - The Lottery(容斥,好题)
10325 - The Lottery The Sports Association of Bangladesh is in great problem with their latest lotte ...
- uva 1393 - Highways(容斥原理)
题目连接:uva 1393 - Highways 题目大意:给定一个m∗n的矩阵,将矩阵上的点两两相连,问有多少条直线至少经过两点. 解题思路:头一次做这样的题目,卡了一晚上. dp[i][j]即为i ...
- UVA 11806 Cheerleaders (容斥原理)
题意 一个n*m的区域内,放k个啦啦队员,第一行,最后一行,第一列,最后一列一定要放,一共有多少种方法. 思路 设A1表示第一行放,A2表示最后一行放,A3表示第一列放,A4表示最后一列放,则要求|A ...
- uva 10325基础容斥
题目:给你一个数n以及m个数字,问1~n中不能被这m个数字整除的数字的个数. 分析:容斥原理.组合数学.数字1-n中能被a.b整除的数字的个数分别是n/a,n/b: 则1-n中能被a或b整数的数字个数 ...
- UVa 1393 (容斥原理、GCD) Highways
题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...
随机推荐
- 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果
ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似.Compo ...
- Error pulling origin: error: The following untracked working tree files would be overwritten by...
git在pull时,出现这样的错误的时候,可能非常多人进进行stash.相关stash的请看:Error pulling origin: error: Your local changes to th ...
- 2013年 ACM 有为杯 Problem I (DAG)
有为杯 Problem I DAG 有向无环图 A direct acylic graph(DAG),is a directed graph with no directed cycles . T ...
- Radio Link Failure and Recovery
四种会发生Radio Link Failure的场景 - DL Physical Layer Failure (PDCCH BLER > 10%) - Random Access Probl ...
- 单例模式 GetInstance()
如何设计一个含GetInstance()函数的类 直接上代码: 头文件(MyClass.h): class CMyClass { public: CMyClass(void); ~CMyClass(v ...
- appium locator
If you want to find out more about the UIAutomator library, then it might be helpful to check out ht ...
- Html5 自定义数据属性
html5 可以为元素添加自定义属性,但是要添加前缀data-.(下面这个例子中的自定义属性的命名,其实是不规范的,不应该包含大写字符,例如:data-myName 应改命名为:data-myname ...
- URL编码解码
ios url 编码和解码 1.url编码 ios中http请求遇到汉字的时候或者像是%…@#¥%&*这些字符的时候也可以使用下面的方法,需要转化成UTF-8,用到的方法是: NSString ...
- python 推导式和迭代器、生成器
1.常用推导式 推导式是从一个或者多个迭代器快速简洁创建数据结构的一种方法. 1.1 _ 列表推导式 最简单的形式: [exprssion for item in iterable] 示例: nu ...
- scanf(),gets(),gechar()函数小结
1. 使用scanf()函数从终端输入字符串时,刚开始输入的所有空格不计入当前字符串,以 space,enter,tab键结束当前字符串的输入:最后的space,enter,tab字符会留在输入缓冲区 ...