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. 获取Java的32位MD5实现

    获取Java的32位MD5实现 public static String md5(String s) { char hexDigits[] = {'0','1','2','3','4','5','6' ...

  2. [O]打印时闪退问题

    1. 使用的是Office批量打印精灵1.2版,软件可以打开 2. Win8.1 MSDN原版操作系统,系统重装了,.NET Framework也装了 3. 使用真实打印机打印,打印时闪退,没有任何提 ...

  3. 《Intel汇编第5版》 Mov指令

    一.Mov用于数据传送,用法如下: 二.当传送的数据和目标数据位宽不一致的时候,需要使用MOVZX.MOVSX扩展.MOVZX使用0填充高位,MOVSX使用源操作数最高位填充 下面是汇编代码演示: I ...

  4. PHP-FPM进程数的设定

    近日,服务器出现异常,网站不能正常访问.经排查是php的问题. 在重启php-fpm时,恢复正常.1分钟之后又出现故障.查看php日志文件 /usr/local/php/var/log 后提示 WAR ...

  5. linux安装GraphicsMagick

    下载GraphicsMagick-1.3.21.tar.gz 解压:tar -zxvf GraphicsMagick-1.3.21.tar.gz cd /usr/local/GraphicsMagic ...

  6. 关于mongodb的一些笔记

    1.以服务的形式安装mongodb dos -- 进入到mongodb的bin目录下,执行 D:\mongodb\bin>mongod --logpath D:\mongodb\logs\mon ...

  7. 重读The C programming Lanuage 笔记四:c预处理

    C预处理器执行宏替换.条件编译以及包含指定的文件.以#开头的命令行就是与处理器的对象.这些命令行的语法独立于语言的其他部分,它们可以出现在任何地方,其作用可延续到所在编译单元的末尾(与作用域无关).行 ...

  8. JavaBean的属性变量名前两个字母大小写问题

    Java属性命名规范! 一般情况下.Java的属性变量名都已小写字母开头,如:userName,showMessage等,但也存在着特殊情况,考虑到一些特定的有意思的英文缩略词如(USA,XML等), ...

  9. 自动安装Redis服务端与PHP扩展Redis

    该脚本基于阿里云服务器安装脚本,并只能运用于centos / aliyun os,该脚本使用时,需要与阿里云安装脚本的install.sh放在同一目录下.有缘人切忌乱用: #! /bin/bash # ...

  10. C#委托与事件讲解(一)

    首先,我们还是先说说委托吧,从字面上理解,只要是中国人应该都知道这个意思,除非委托2个中文字不认识,举个例子,小明委托小张去买车票.     但是在我们的程序世界里,也是这么的简单吗?当然,什么是OO ...