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

通常我们遇到的题多是(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. SAP 预制发票报错 -Material 20000000 D119223073 in valuation area VNSM has inconsistant ML data- 之对策

    SAP 预制发票报错 -Material 20000000 D119223073 in valuation area VNSM has inconsistant ML data- 之对策 业务人员报对 ...

  2. xcode6新建工程

    xcode6中新建空工程 (2014-10-29 13:14:44) 转载▼ 标签: it ios 分类: iOS 升级xcode6之后,直接建立Empty工程后发现,这是太坑,真的是什么都没有啊.只 ...

  3. [b0015] python 归纳 (一)_python组织方式

    结论: xxx.yyyy yyyy 可以是 类.类对象.函数.变量 xxx 可以是 包.模块.类 代码: ref1.py # -*- coding: utf-8 -*- import os class ...

  4. Python 定时调度

    APScheduler APScheduler是基于Quartz的一个Python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务 ...

  5. F5部署SSL证书

    查找中间证书 为了保证可以兼容所有浏览器,我们必须在服务器上安装中间证书,请到 中间证书下载工具,输入您的Server.cer,然后下载中间证书,请将中间证书保存为Chain.cer. 证书文件的上传 ...

  6. springcloud学习之路: (五) springcloud集成SpringCloudConfig分布式配置中心

    SpringCloud全家桶中的分布式配置中心SpringCloudConfig, 它使用git来管理配置文件, 在修改配置文件后只需要调用一个接口就可以让新配置生效, 非常方便. SpringClo ...

  7. vm|vmware workstation 15|14 pro 激活|密钥|序列号|许可证

    VMware Workstation Pro 15 激活许可证 UY758-0RXEQ-M81WP-8ZM7Z-Y3HDA VF750-4MX5Q-488DQ-9WZE9-ZY2D6 UU54R-FV ...

  8. Java八大排序之插入排序

    插入排序 也可叫直接插入排序,该算法的思路是:初始可认为文件中的第1个记录已排好序,然后将第2个到第n个记录依次插入到已排序的记录组成的文件中. 步骤: 假设有一组数组为(数组下标0—n-1): ar ...

  9. 2.1 Scala语言概述

    一.编程范式 命令式编程没有办法充分利用多核CPU: 函数式编程很多变量是不可修改的. 二.Scala简介 特点 scala运行在JVM上,兼容现有的Java程序: 面向对象的编程语言: 一门函数式语 ...

  10. oracle_day1

    本节内容: 1:oracle的服务 2:oracle 11 G 的新功能 3:数据库的三大范式 1:oracle的服务 安装完oracle 想要使用oracle 必须要启动的两个服务. 要是还想要使用 ...