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

通常我们遇到的题多是(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. laravel Method Illuminate\Validation\Validator::validateReuqired does not exist.

    Method Illuminate\Validation\Validator::validateReuqired does not exist. 此错误是由于我们在配置验证时,写错了关键字, publ ...

  2. ef linq多表查询(三表)

    var ph = (from c in reef.CQICC_CLIENT join v in reef.CQICC_VISIT_CLIENT on c.CLIENT_T_ID equals v.CL ...

  3. Tp5.1开发初入门

    今天需要给金融部门那边做一个信用卡的推广页面,他们系统是用PHP的tp框架做的.我记得最早做tp还是2的时候,和现在的5.1相差太大了,中间开发的时候,还是遇到了点问题.所以,把今天的问题记录下,作个 ...

  4. swift(一)基础变量类型

    import Foundation println("Hello, World!") /* int a; */ var a = //隐式类型转换 a = println(a) le ...

  5. js 过滤字符 和检测 特殊字符【转】

    // var str1 = str.replace(/\[\\'\\"\\\\\\/\\b\\f\\n\\r\\t\]/g, '');// 去掉转义字符 // var str2= str.r ...

  6. JMETER 用户变量作用域

    在编写JMETER 脚本时,我们会使用到变量,变量的作用域是线程. 我们通过下面的脚本验证一下变量的返回是线程. 1. 我们先定义一个amount的流程变量. 2.线程组使用三个线程 3.在线程组中添 ...

  7. DBCP连接池使用

    转载请注明原文地址https://www.cnblogs.com/ygj0930/p/11280475.html 一:引入jar包或添加依赖 1:jar包 2:maven依赖 <dependen ...

  8. odoo10学习笔记二:继承(扩展)、模块数据

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189252.html 一:继承 在不改变底层对象的时候添加新的功能——这是通过继承机制来实现的,作为在现有 ...

  9. UVa 202 Repeating Decimals 题解

    The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle ...

  10. 使用GCP_EC2云服务器搭部署网络服务

    首先,在此阿里云/腾讯云/华为云购买一个云服务器推荐使用阿里云的 首先链接你的VPS,可以使用X-shell / Putty / SecureCRTPortable 等SSH链接工具 注意:如果不知道 ...