1091. Tmutarakan Exams

Time limit: 1.0 second
Memory limit: 64 MB
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 Kand 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 ≤ K ≤ S ≤ 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.

Sample

input output
3 10
11

题意:

New Tmutarakan大学培养在心算方面一流的专家。要进入大学学习您必须能熟练地进行计算。其中一个系的入学考试如下:考生被要求找出K个不同的数字使他们有一个大于1的公约数。所有的数字都不能大于一个指定的数字S。数字K和S在考试开始时给出。为了避免抄袭(这个系是大学里最有名望的!),各组解只能被承认一次(承认最先提交它的人)。

去年,这些数字是K=25和S=49,但是不幸地,没有人能通过考试。并且,它后来被系里最有头脑的人证明了,并不存在一组数字可以满足那些规律。今年为了避免困窘,教务长请求您的帮忙。您要找到K个不同的数字使他们有一个大于1的公约数。所有的数字都不能大于一个指定的数字S。当然,这些解的数量应该与系的新招学生的最大数目相等。

输入格式

输入包含数字K和S (2≤K≤S≤50)。

输出格式

您应该输出系的新学生的最大的可能的数量(也就是解的数量)。如果这个数字不大于10000,请输出这个数字,否则您应该输出10000。

思路:

题目的大意是给出k, s,就在s间的k个不同数的公因子大于1, 我们以 k = 3,   s = 25 为例:
首先用一张素数表, 避免有些数的重复计算:
                             int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,47, 53 };
         因为所有比1大的数都可以由素数相乘得出, 但素数的因子就只有它本身和1,所以可以避免很多不必        要的计算:

在2到25之间有 
     1:   25/2 = 12个2的倍数 , 所以算出 combination(12, 3)= 220, 得出所有2的倍数3个数一组 的组合

2: 25/3 = 8    个3的倍数, 所以算出combination(8, 3) = 56, 得出 所有3的倍数 3个一组的组合;

3: 25 / 5 = 5   个5 的倍数,所以算出combination(5, 3) = 10

4: 25 / 7 = 3 个 7的倍数,所以算出combination(3,3) = 1
    
     5: 25 / 11 = 2   个11的倍数,所以组合数不够, 跳出循环
      结果= 220 + 56 + 10 + 1
     但是: 有些组合是被我们计算了2次, 所以应该被减去, 比如:
      2 的倍数有2, 4, 6 , 8, 10 ,12, ..........
      3的倍数有3, 6 , 9, 12, 15, 18..........
                 所以6, 12....被重复计算了........然后:
       2~~~~~25之间有25 / (2 *3) = 4 , combination (4,3) = 4,所以结果要减去4, 然后继续,看还有不?

代码1:

 #include <iostream>
#include <string>
#include<cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std; int kiss[*+]={}; int zuhe(int n,int r)
{
if(n==)
return ;
if(r==)
return ;
if(r==)
return n;
if(r==n)
return ;
return zuhe(n-,r)+zuhe(n-,r-);
} int main()
{
int k,s;
cin>>k>>s;
long long sum=;
int prime[]={,,,,,,,,,,,,,,,};
// int sgin=0;
int i;
for(i=;i<;i++){
if(s/prime[i]<k){
break;
}
else{
sum+=zuhe(s/prime[i],k);//计算组合数,进行统计,按质数的进行哈希式的划分
}
}
for(i=;i<;i++){
for(int j=i+;j<;j++){
sum-=zuhe(s/(prime[i]*prime[j]),k);//减去冗余的项
}
}
sum=sum>?:sum;//注意输出的要求
cout<<sum<<endl;
return ;
}

代码2:

 #include <iostream>
#include <string>
#include<cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std; int kiss[][]={}; int main()
{
int k,s;
cin>>k>>s;
long long sum=;
int prime[]={,,,,,,,,,,,,,,,};
// int sgin=0;
int i;
memset(kiss,,sizeof(kiss));
for(i=;i<;i++){
kiss[i][]=;
kiss[i][i]=;
}
kiss[][]=;
for(i=;i<=;i++){
for(int j=;j<i;j++){
kiss[i][j]=kiss[i-][j-]+kiss[i-][j];
}
}
for(i=;i<;i++){
if(s/prime[i]<k){
break;
}
else{
sum+=kiss[s/prime[i]][k];//计算组合数,进行统计,按质数的进行哈希式的划分
}
}
for(i=;i<;i++){
for(int j=i+;j<;j++){
sum-=kiss[s/(prime[i]*prime[j])][k];//减去冗余的项
}
}
sum=sum>?:sum;//注意输出的要求
cout<<sum<<endl;
return ;
}

ural 1091. Tmutarakan Exams(容斥原理)的更多相关文章

  1. Ural 1091 Tmutarakan Exams

    Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...

  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. URAL - 1091 Tmutarakan Exams (简单容斥原理)

    题意:K个不同数组成的集合,每个数都不超过S且它们的gcd>1.求这样的数的个数 分析:从2开始枚举gcd,但这样会发生重复.譬如,枚举gcd=2的集合个数和gcd=3的集合个数,枚举6的时候就 ...

  4. ural 1091. Tmutarakan Exams(容斥)

    http://acm.timus.ru/problem.aspx? space=1&num=1091 从1~s中选出k个数,使得k个数的最大公约数大于1,问这种取法有多少种. (2<=k ...

  5. 1091. Tmutarakan Exams

    1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...

  6. 2014 Super Training #3 H Tmutarakan Exams --容斥原理

    原题: URAL 1091  http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...

  7. 容斥原理--计算并集的元素个数 URAL 1091

    在计数时,必须注意没有重复,没有遗漏.为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计 ...

  8. F - Tmutarakan Exams URAL - 1091 -莫比乌斯函数-容斥 or DP计数

    F - Tmutarakan Exams 题意 : 从 < = S 的 数 中 选 出 K 个 不 同 的 数 并 且 gcd > 1 .求方案数. 思路 :记 录 一 下 每 个 数 的 ...

  9. Tmutarakan Exams URAL - 1091(莫比乌斯函数 || 容斥)

    题意: 求1 - s 中 找出k个数 使它们的gcd  > 1 求这样的k个数的对数 解析: 从每个素数的倍数中取k个数  求方案数 然后素数组合,容斥一下重的 奇加偶减 莫比乌斯函数的直接套模 ...

随机推荐

  1. How I Mathematician Wonder What You Are!(poj 3130)

    题意:求问多边形的核(能够看到所有点的点)是否存在. /* 对于这样的题目,我只能面向std编程了,然而还是不理解. 算法可参考:http://www.cnblogs.com/huangxf/p/40 ...

  2. jmeter之jtl文件解析

    我们知道命令行的方式执行完成jmeter后,会生成jtl文件,里面打开后就是一行行的测试结果, <httpSample t="1" lt="1" ts=& ...

  3. 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 转载

    最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在chrome,firefox,safari,opera,360浏览器(极速模式).搜狗浏览器等浏览器下均没 ...

  4. linode更换Linux内核教程(独家)

    Linode服务器性价比高,最低套餐2G内存,享受每月2TB流量,机房40Gb带宽,每月供需10美元(Linode优惠链接).Linode用户创建vps服务器后,可在后台自定义Linux系统版本,包括 ...

  5. Dreamweaver层使用八定律

    当然,这些并非真正的定律,而只是一些有益的忠告,使你免陷于使用层时可能的困顿中.原来有九条定律的,我们精简掉一条,还有下面的八条: 1. 如果你要嵌套层,决不要使用多重父层,应共享一个共同的单一父层. ...

  6. Path.OS 模块的使用方法(转自DK的博客)

    Python os.path模块 使用方法 os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonpref ...

  7. sqlserver2012评估期已过问题处理

    于之前安装sqlserver2012忘记输入序列号,现在出现评估期已过的问题,网上忙活半天,才解决,发现网上叙述都很凌乱,而且只有大意,新手很难操作,所以把我操作的过程分享给大家 步骤阅读   百度经 ...

  8. win10锁屏壁纸路径

    C:\Users\ShanYu\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalS ...

  9. zkw费用流

    期末结束,竞赛生活继续开始,先怒刷完寒假作业再说 至于期末考试,数学跪惨,各种哦智障错,还有我初中常用的建系大法居然被自己抛至脑后,看来学的还是不扎实,以后数学要老老实实学.物理被永哥黑了两分,然后很 ...

  10. 在ASP.NET Web Forms中使用页面导出伪xls Excel表格

    将数据导出为Excel表格是比较常见的需求,也有很多组件支持导出真正的Excel表格.由于Excel能打开HTML文件,并支持其中的table元素以及p之类的文本元素的显示,所以把.html扩展名改为 ...