Ural 1091. Tmutarakan Exams 排列组合+容斥原理+质因数分解

来源:互联网 发布:卡五星麻将app源码 编辑:程序博客网 时间:2024/05/22 12:45

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

inputoutput
3 10
11
Problem Author: Stanislav Vasilyev
Problem Source: USU Open Collegiate Programming Contest March'2001 Senior Session
要求找出K个不同的数字使他们有一个大于1的公约数。所有的数字都不能大于一个指定的数字S。
枚举素数和两个素数的乘积,运用容斥原理判断.
#include <iostream>#include <cstring>using namespace std;int k, s, ans, num, cnt;long long C[55][55];int prime[9] = {2,3,5,7,11,13,17,19,23};void calc(){    memset(C, 0, sizeof(C));    C[1][0] = 1;    C[1][1] = 1;    for (int i = 2; i <= 50; ++ i)    {        C[i][0] = 1;        for (int j = 1; j <= i; ++ j)            C[i][j] = C[i-1][j-1] + C[i-1][j];    }}int main(){    calc();    while (cin >> k >> s)    {        ans = 0;        cnt = 9;        for (int i = 0; i < 9; ++ i)        {            num = s / prime[i];            if (num < k)            {                cnt = i;                break;            }            else ans += C[num][k];        }        for (int i = 0; i < cnt; ++ i)        {            for (int j = i+1; j < cnt; ++ j)            {                num = s / (prime[i]*prime[j]);                if (num < k) break;                else ans -= C[num][k];            }        }        if (ans > 10000) ans = 10000;        cout << ans << endl;    }}


原创粉丝点击