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. Windows端口转发

    1. PortTunnel 2. windows 自带的 netsh -----windows下也有一个小工具:portforward.exe,图形界面容易操作,个人平常使用可以,但是也没有办法实现与 ...

  2. lda 主题模型--TOPIC MODEL--Gibbslda++结果分析

    在之前的博客中已经详细介绍了如何用Gibbs做LDA抽样.(http://www.cnblogs.com/nlp-yekai/p/3711384.html) 这里,我们讨论一下实验结果: 结果文件包括 ...

  3. 使用ajax和history.pushState无刷新改变页面URL(转)

    表现 如果你使用chrome或者firefox等浏览器访问本博客.github.com.plus.google.com等网站时,细心的你会发现页面之间的点击是通过ajax异步请求的,同时页面的URL发 ...

  4. centos6 + tomcat+ jdk配置步骤

    1. 获取tomcat, jdk安装文件 mkdir /media/smbdirmount -o username=pas,password=111111 //109.110.100.50/pas / ...

  5. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  6. python--day4--迭代器、生成器

    列表生成式: 需求:列表[1,2,3,4,5,6,7,8,9]每个值加1,实现的方法: a = [0,1,2,3,4,5,6,7,8,9] b = [] for i in a:b.append(i+1 ...

  7. 移动端ios电话号码

    <meta name="format-detection" content="telephone=no"> <meta http-equiv= ...

  8. Crazy-Links

    1. 数据模型 2. Admin Formset RED is customized class |- object |- AdminForm |- InlineAdminForm |- BaseFo ...

  9. phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接。

     phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接.您应该检查配置文件中的主机.用户名和密码,并确认这些信息与 MySQL 服务器管理员所给出的信息一致. 错误产生原因: 修改了 ...

  10. zoj 1718 poj 2031 Building a Space Station

    最小生成树,用了Kruskal算法.POJ上C++能过,G++不能过... 算出每两个圆心之间的距离,如果距离小于两半径之和,那么这两个圆心之间的距离直接等于0,否则等于距离-R[i]-R[j]. # ...