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. 敏捷开发(十一)- Scrum Sprint评审会议

    本文主要是为了检测你对SCRUM 评审会议的了解和使用程度, 通过本文你可以检测一下     1.你们的SCRUM 评审会议的过程和步骤    2.SCRUM 评审会议的输出结果一.会议目的     ...

  2. Webkit浏览器点击控件时出现的边框消除

    -webkit-tap-highlight-color:rgba(0,0,0,0); 其实是将边框颜色透明,让其不可见了而已

  3. 键盘快速启动工具Launchy的简单使用技巧

    打开电脑面对林林总总的图标,找到对应的程序,快速启动显得尤为重要.这样有利于提高我们的效率. 好了,直接上图: 就是这款小巧的工具,界面如上. 接下来介绍这款工具的使用技巧. 1.安装成功后:打开工具 ...

  4. 【Python】迭代器

    对迭代器和生成器的概念一直很混乱,总结一下: 迭代器: 1.所谓的迭代器,就是具有__next__()方法的对象: 2.__iter__()方法返回一个迭代器对象,这个对象必须具有__next__() ...

  5. 《Intel汇编第5版》 数组求和

    一.LOOP指令 二.间接寻址 三.汇编数组求和 INCLUDE Irvine32.inc includelib Irvine32.lib includelib kernel32.lib includ ...

  6. 用Karma和Jasmine测试Angular应用

    TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...

  7. Digi. Certificates: Key pairs usages

    In short, we have some sort of algorithms to gen pair of private and public keys. The public key is ...

  8. 单链表,循环链表,双向链表(C++实现)

    首先是单链表(带附加表头),实现类代码如下: template<class T> struct LinkNode{//链表节点 T data; LinkNode *link; LinkNo ...

  9. 【原创】对Java的synchronized关键字的学习

    在Java中,每一个线程都有一个内部锁.当我们使用synchronized关键字时,就是利用这个内部锁来实现线程对某个对象的锁定控制. 那么,如果某个对象中有两个方法,方法一和方法二都使用了synch ...

  10. HDU猜数字

    G - 猜数字 Time Limit:10000MS       Memory Limit:32768KB       64bit IO Format:%I64d & %I64u Descri ...