`

Ural 1091. Tmutarakan Exams

阅读更多
1091. Tmutarakan Exams
Time Limit: 1.0 second
Memory Limit: 16 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 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 ≤ 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

题目大意: 对于k,s<=50,试统计{a1,a2,...,ak|ai<=s and gcd(a1,...ak)>1}的数目。

算法分析:首先按照约数分类{2,3,5,……,s}。

我们会注意到如下事实:如果gcd(x,y)!=1且x<y,那么按照约数x分类的集合A包含按照约数y分类的集合B。如:对s=20,x=2,y=4有:
A={2,4,6,8,10,12,14,16,18,20}
B={4,8,12,16,20}
再如s=20,x=3,y=6有:
A={3,6,9,12,15,18}
B={6,12,18}
显然均有:A包含B
所以由此事实知:我们只需要按照素数分类即可!
但问题又来了,这样分类计数仍有重复!如:
当p=2时,对应的集合为:A={2,4,6,8,10,12,14,16,18,20}
当p=3时,对应的集合为:B={3,6,9,12,15,18}
当k=2时,从A集合中取{6,12}时,从B集合中同时也可以取到{6,12},产生重复!!!咋办呢???
这时容斥原理可以派上用场!对于上面的那种情况只需要再减去按6分类的集合所产生的集合数。
由于s<=50,k>=2,所以只需要考虑这些素数{2,3,5,7,11,13,17,19,23}按这些素数进行分类
以及排除应的情况为6->(2,3),10->(2,5),14->(2,7),22->(2,11),15->(3,5),21->(3,7),其它的不需要考虑。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=60;
int c[maxn][maxn];//存储组合数C(n,m)
void getC()
{//C(n,m)=C(n-1,m)+C(n-1,m-1)
    for(int i=0;i<maxn;i++)
    {
        c[i][0]=1;
        c[i][1]=i;
        c[i][i]=1;
    }
    for(int i=1;i<maxn;i++)
    {
        for(int j=1;j<i;j++)
        {
            int sum=c[i-1][j]+c[i-1][j-1];
            if(sum>10000)   sum=10000;//对于10000的只需存储10000
            c[i][j]=sum;
        }
    }
}
int main()
{
    getC();
    int k,s;
    //while(scanf("%d%d",&k,&s)!=EOF){
    scanf("%d%d",&k,&s);
    int sum=0;
    int prime[9]={2,3,5,7,11,13,17,19,23};
    for(int i=0;i<9;i++)
    {
        if(s/prime[i]<k)    break;
        if(c[s/prime[i]][k]==10000)  {sum=100000;break;}
        sum+=c[s/prime[i]][k];
        //printf("c[%d][%d]=%d\n",s/prime[i],k,c[s/prime[i]][k]);
    }
    if(sum==100000)
    {
        printf("10000\n");
        //continue;
        return 0;
    }
    for(int i=1;i<=4;i++)
    {
        if(s/(prime[i]*2)<k)    break;
        sum-=c[s/(prime[i]*2)][k];
    }
    for(int i=2;i<=3;i++)
    {
        if(s/(prime[i]*3)<k)    break;
        sum-=c[s/(prime[i]*3)][k];
    }
    sum=min(sum,10000);
    printf("%d\n",sum);
    //}
    return 0;
}

分享到:
评论

相关推荐

    Python库 | ural-0.28.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:ural-0.28.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    acm_ural_1148

    Pascal acm_timus_ural_1148.pas

    Ural URAL 解题思路

    Ural解题思路 Ural解题思路 Ural解题思路 Ural解题思路

    Ural

    Ural

    acm_ural_1099

    Pascal acm_timus_ural_1099.pas

    URAL3D

    URAL3D

    URAL部分测试数据

    URAL(Timus Online Judge)部分测试数据 不全

    Ural 1238 源代码

    Ural 1238 源代码 涉及算法(动态规划、贪心、DFS)

    ural vol I 题解 by yuhch123 pdf

    ural 题解 yuhch123。 关于yuhch123: ioi2008 金牌第一名,这是当初它做ural 第一卷时的题解

    ural题解

    包含了ural题库中Vol_I 到Vol_III的所有题目的解题思路

    ural部分题解

    部分题解 大牛出品 Vol1-3 不是很全,约有200题左右

    URAL-PHA

    URAL-PHA

    PART II THE URAL-ALTAIC HYPOTHESIS CHAPTER IV. THE URAL-ALTAIC HYPOTHESIS AND TUNGUS MATERIAL (1931年)

    28. The Theoretical Background of the Ural-Altale Hypothesis The hypothesis of common origin of the Altaic languages,and even the Ural-Altaic languages,dates from the first half of the last century....

    Ural ACM 1000源代码(c++)

    Ural ACM 1000源代码(c++),vc++6.0在XPsp2下编译通过,Timus Online Judge再线测评通过

    oss-js.zip_Hold_qt oss

    This site is created and administrated by the students and graduates of the Ural Federal University. If you want to publish your problems in the archive or hold your own online programming contest, ...

    hdu pku ural 题目分类

    因为大三了 不弄ACM了 所以这些就删了 删之前希望可以帮到别人

    线段树题目

    大量线段树题目 zoj 1610 线段覆盖 poj 2777 线段覆盖 poj 2528 需要离散化,建树不同,需要处理不同-&gt;...ural 1019 覆盖加统计最长同一个颜色 zoj 2301 和上一题差不多,但是这个染色染的是点,注意染色为空的状况

    Ural-开源

    基于张量类模板的线性代数C ++库,可用于表示等级0的张量,标量向量,1的向量,2的矩阵,3的等级3的张量等。它还包括BLAS和LAPACK子例程的接口。

    光学镜头结构

    chieves t he integration of optical and st ruct ural design sof tware. Mainly based on black box compo2 nent met hod , t he development of optical and st ruct ural design sof tware is completed using ...

    Pokupki59.RF「Покупки59.РФ」-crx插件

    在Shopping59.rf网站上进行联合购物扩张 Shopping59.RF - 这是在彼尔姆联合...ural-toys.ru 我们也接受来自其他网站的订单: odezhda-master.ru gepur.com.ua milofo.ru z29.ru butik-duhov.ru 支持语言:русский

Global site tag (gtag.js) - Google Analytics