Ural 1091 Tmutarakan Exams (水题 容斥+ 莫比乌斯反演)

来源:互联网 发布:淘宝店铺资质25000元 编辑:程序博客网 时间:2024/05/15 23:53


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 numberS. The numbersK 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 andS=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 andS (2 ≤ KS ≤ 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

inputoutput
3 10
11
Problem Author: Stanislav Vasilyev
Problem Source: USU Open Collegiate Programming Contest March'2001 Senior Session
Tags: number theory

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

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

题目分析:k和s都那么小,直接从2开始枚举最大公约数,i的倍数有s / i个,从中选k个为c[s / i][k],再用莫比乌斯函数容斥一下

#include <cstdio>#define ll long longint const MAX = 55;ll c[30][30];int mob[MAX], p[MAX];bool noprime[MAX];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 Mobius(){    int pnum = 0;    mob[1] = 1;    for(int i = 2; i < MAX; i++)    {        if(!noprime[i])        {            p[pnum ++] = i;            mob[i] = -1;        }        for(int j = 0; j < pnum && i * p[j] < MAX; j++)        {            noprime[i * p[j]] = true;            if(i % p[j] == 0)            {                mob[i * p[j]] = 0;                break;            }            mob[i * p[j]] = -mob[i];        }    }}int main(){    Init();    Mobius();    int k, s;    ll ans = 0;    scanf("%d %d", &k, &s);    for(int i = 2; i <= s; i++)        for(int j = i; j <= s; j += i)            ans += (ll) c[s / j][k] * mob[j / i];    if(ans > 10000)        ans = 10000;    printf("%I64d\n", ans);}   


0 0
原创粉丝点击