计数时,必须注意没有重复,没有遗漏。为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复,这种计数的方法称为容斥原理。  【百度百科】

通常我们遇到的题多是(A1∪A2)=A1+A2-A1∩A2和A1∩A2=A1+A2-(A1∪A2)。

例题:URAL 1091

Tmutarakan Exams

URAL - 1091

University of New Tmutarakan trains the first-class specialists in mental arithmetic. To enter the University you should master arithmetic perfectly. One of the entrance exams at the Divisibility Department is the following. Examinees are asked to find K different numbers that have a common divisor greater than 1. All numbers in each set should not exceed a given number S. The numbers K and S are announced at the beginning of the exam. To exclude copying (the Department is the most prestigious in the town!) each set of numbers is credited only once (to the person who submitted it first).
Last year these numbers were K=25 and S=49 and, unfortunately, nobody passed the exam. Moreover, it was proved later by the best minds of the Department that there do not exist sets of numbers with the required properties. To avoid embarrassment this year, the dean asked for your help. You should find the number of sets of K different numbers, each of the numbers not exceeding S, which have a common divisor greater than 1. Of course, the number of such sets equals the maximal possible number of new students of the Department.

Input

The input contains numbers K and S (2 ≤ KS ≤ 50).

Output

You should output the maximal possible number of the Department's new students if this number does not exceed 10000 which is the maximal capacity of the Department, otherwise you should output 10000.

Example

input output
3 10
11

题意:

输入K,S,问对于从集合{1,2,3......S}中选出K个数字,使他们的最大公因数大于1,这样的选法有几个?

分析:

可以参考素数筛的思想,我们先选出一个质数,那么这个质数的倍数和这个质数组成的集合,他们的最大公因数一定是这个质数本身,假设选取的质数是i,那么1~s有[(s-i)/i+1]个数是i的倍数,从这些数中选出k个数是一定满足条件的,所以我们可以枚举1~s所有的质数,然后有ans+=C([s-i]/i+1,k)。

但是我们发现:以2为例可以得到6,12,18,以3为例也可以得到6,12,18,不同质数的倍数可能会相同!假如k正好是2,那么选择2的倍数中的6,12和选择3的倍数中的6,12就会导致重复计算,所以我们要减去重复计算的部分。

我们只要在枚举的过程中判断一下当前枚举的数是不是两个质数之积,如果是,设i是两个质数之积,同理,我们从i的所有倍数中选出k个数,有C([s-i]/i+1,k)种选法,然后ans-=C([s-i]/i+1,k)即可。

AC code:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
bool u[];
ll su[];
ll c[][];
ll num,s,k;
void olas()
{
memset(u,true,sizeof(u));
num=;
u[]=u[]=false;
for(ll i=;i<=;i++)
{
if(u[i]) su[num++]=i;
for(ll j=;j<num;j++)
{
if(i*su[j]>) break;
u[i*su[j]]=false;
if(i%su[j]==) break;
}
}
}
void cal_C()
{
for(ll i=;i<=;i++) c[i][]=;
for(ll i=;i<=;i++)
for(ll j=;j<=;j++)
c[i][j]=c[i-][j]+c[i-][j-];
}
bool pxp(ll x)
{
for(ll i=;i<=;i++)
{
if(x%i==&&u[i]&&i!=x/i&&u[x/i])
return true;
}
return false;
}
ll work()
{
ll ans=;
for(ll i=;i<=s;i++)
{
if(u[i])
{
ans+=c[(s-i)/i+][k];
}
else if(pxp(i))
{
ans-=c[(s-i)/i+][k];
}
}
return ans>?:ans;
}
int main()
{
//freopen("input.txt","r",stdin);
olas();
cal_C();
scanf("%lld%lld",&k,&s);
printf("%lld\n",work());
return ;
}

容斥原理--计算并集的元素个数 URAL 1091的更多相关文章

  1. 计算元素个数(count和count_if)

    count 计算first和last之间与value相等于元素个数 template <class InputIterator,class EqualityComparable> type ...

  2. ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph

    ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...

  3. 【OJ】 : 容斥原理计算出 1< =n < 1e9 中是2,3,5倍数的整数的数量

    最近ACM时遇到个题,题意如下. 问题描述: 有个1到n的数列,数一下其中能够被 2, 的时候有 ,,,,.这5个数满足条件,所以我们应该输出 5 . 输入 多组输入到文件尾,每组输入一个 n (n ...

  4. C#数组维数及不同维数中元素个数的获取

    简单理解有关数组维数的概念: 1.编程中用到的多维的数组,最多也就是二维数组了 2.数组的维数从0开始计算 using System; using System.Collections.Generic ...

  5. STL查找序列中处于某一大小范围内的元素个数

    还是头条的笔试题(咦?),问题最后转换成这样的形式: 输入:不包含重复元素的有序数组a[N]以及上下界low, high; 输出:数组a[N]中满足元素处于闭区间[low,high]内(即low &l ...

  6. jdk1.8 ConcurrentHashMap 的工作原理及代码实现,如何统计所有的元素个数

    ConcurrentHashMap 的工作原理及代码实现: 相比于1.7版本,它做了两个改进 1.取消了segment分段设计,直接使用Node数组来保存数据,并且采用Node数组元素作为锁来实现每一 ...

  7. 阿里P7岗位面试,面试官问我:为什么HashMap底层树化标准的元素个数是8

    前言 先声明一下,本文有点标题党了,像我这样的菜鸡何德何能去面试阿里的P7岗啊,不过,这确实是阿里p7级岗位的面试题,当然,参加面试的人不是我,而是我部门的一个大佬.他把自己的面试经验分享给了我,也让 ...

  8. 神秘常量复出!用0x077CB531计算末尾0的个数 -- De Bruijn 序列

    http://www.matrix67.com/blog/archives/3985 神秘常量复出!用0x077CB531计算末尾0的个数 大家或许还记得 Quake III 里面的一段有如天书般的代 ...

  9. C++在数组元素个数未知情况下声明数组

    我们都从书上学习的方法,定义一个数组需要数组名.类型以及数组元素个数,一般定义必须明确元素的个数,否则无法通过编译. 1. int a[]; 2. int n; int a[n]; 就想上面这两种情况 ...

随机推荐

  1. Python【day 12】生成器和推导式

    一.生成器和生成器函数1.生成器和生成器函数的概念 1.生成器的本质是迭代器 2.函数中包含yield,就是生成器函数 2.生成器函数的写法 def func(): a =10 yield 20 ge ...

  2. spring cloud 框架源码 activiti工作流 vue.js html 跨域 前后分离 springboot

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  3. Windows中将nginx添加到服务(转)

    下载安装nginx http://nginx.org/en/download.html 下载后解压到C盘 C:\nginx-1.14.0 添加服务 需要借助"Windows Service ...

  4. opencv::GMM(高斯混合模型)

    GMM方法概述:基于高斯混合模型期望最大化. 高斯混合模型 (GMM) 高斯分布与概率密度分布 - PDF 初始化 初始化EM模型: Ptr<EM> em_model = EM::crea ...

  5. Linux CentOS 防止SSH暴力破解

    一. 问题的发现 昨晚苦逼加班完后,今早上班继续干活时,SSH连接服务器发现异常的提示,仔细看了一下吓一小跳,昨晚9点钟到现在,一夜之间被人尝试连接200+,慌~~~ 1. 速度查一下log [roo ...

  6. 关于重学Linux的随笔

    毕业已有半年, 现在想想真的后悔, 大学没有认真学Linux, 导致现在Linux操作抓瞎, 连服务器都搭不起来. 下定决心重学Linux, 不追求能比上大佬, 但是要熟练, 常用命令要熟悉. 作为一 ...

  7. Linux-CentOS-Nginx安装

    原文转自 jerryhe326:https://www.cnblogs.com/jerrypro/p/7062101.html 一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装n ...

  8. <Math> 165 8

    165. Compare Version Numbers class Solution { public int compareVersion(String version1, String vers ...

  9. 【转载】预处器的对比——Sass、LESS和Stylus

    常用的3大css预编译器:Sass.LESS和Stylus,你是否会混淆它们的区别和用法.这里有篇文章介绍的挺详细. 传送门:https://www.w3cplus.com/css/sass-vs-l ...

  10. Linux ssh 密钥创建与验证

    1.需要两个虚拟机,每一个创建一个用户登录到用户根下   2.每个用户都要创建密钥对   3.把两个用户的公用密钥用ssh-copy-id -i 命令将公用的密钥复制到另一个用户中   4.在客户端开 ...