您的位置:首页 > 其它

ural1091(莫比乌斯 容斥)

2017-06-14 09:55 316 查看
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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  莫比乌斯反演