ural1091(莫比乌斯 容斥)

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 numberS. 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 wereK=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 exceedingS, 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 numbersK 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

http://acm.timus.ru/problem.aspx?space=1&num=1091

题意:

从1-s中选k个不同的数组成一个集合,要求它们的最大公约数大于1,问有多少个这样的集合,大于10000输出10000

tip:

直接从2开始枚举每个数i,i得所有倍数作为一个集合,这个集合任意选k个,gcd为I,从中选k个为c[s / i][k],再用莫比乌斯函数容斥一下,容斥系数为mu[i]*(-1)

关于莫比乌斯反演:

http://blog.csdn.net/u013632138/article/details/52250567
大概就是简化了对两个函数之间得关系是公约数时候得运算,目前只理解了作为容斥得系数得含义,(-1)^ (一个数含有不同得素因子得个数),时间复杂度==筛nlogn

void muius(){
    int pnum = 0;
    mu[1] = 1;
    for(int i = 2; i < MAX; i++){
        if(!noprime[i]){
            p[pnum ++] = i;
            mu[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++){
            noprime[i * p[j]] = true;
            if(i % p[j] == 0){
//i不是primes[j]的整数倍时,i*primes[j]就不会包含相同质因子,

                mu[i * p[j]] = 0;
                break;
            }
            mu[i * p[j]] = -mu[i];
//mu[I]作为-1得m次方,乘上一个素数,就是-1得(m+1)次方,直接取负号
        }
    }
}

关于循环求得组合数:C(n,m)

void init(){
    for(int i = 0; i <= 25; i++)
        c[i][0] = 1;
    for(int i = 1; i <= 25; i++)
        for(int j = 1; j <= 25; j++)
            c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
#include <cstdio>
#include <iostream>
int const MAX = 55;
int c[30][30];
int mu[MAX], p[MAX];
bool noprime[MAX];
int k, s;

void init(){
    for(int i = 0; i <= 25; i++)
        c[i][0] = 1;
    for(int i = 1; i <= 25; i++)
        for(int j = 1; j <= 25; j++)
            c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
void muius(){
    int pnum = 0;
    mu[1] = 1;
    for(int i = 2; i < MAX; i++){
        if(!noprime[i]){
            p[pnum ++] = i;
            mu[i] = -1;
        }
        for(int j = 0; j < pnum && i * p[j] < MAX; j++){
            noprime[i * p[j]] = true;
            if(i % p[j] == 0){
                mu[i * p[j]] = 0;
                break;
            }
            mu[i * p[j]] = -mu[i];
        }
    }
}
void sov(){
    int ans = 0;
    scanf("%d%d", &k,&s);
    for(int i = 2; i <= s; i++)
        //for(int j = i; j <= s; j += i)
            ans +=  c[s / i][k] * mu[i] *(-1);
    if(ans > 10000)
        ans = 10000;
    printf("%d\n", ans);
}
int main(){
    init();
    muius();
    sov();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值