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)^ (一个数含有不同得素因子得个数),时间复杂度==筛nlogn
关于循环求得组合数:C(n,m)
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输出10000tip:
直接从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();
}
相关文章推荐
- URAL1091:Tmutarakan Exams(容斥 & 莫比乌斯反演)
- URAL 1091 Tmutarakan Exams 【容斥】【组合数】
- URAL 1091 Tmutarakan Exams (DP或容斥)
- 【数论】ural1091
- URAL1091---Tmutarakan Exams(dp)
- poj1091跳蚤---------容斥
- 【容斥原理,莫比乌斯反演】用容斥替代莫比乌斯反演第二种形式解决gcd统计问题
- URAL 1091 Tmutarakan Exams(容斥原理)
- 2017杭电多校联赛第二场-TrickGCD (hdu6053)莫比乌斯容斥
- CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
- ural 1932 The Secret of Identifier 容斥
- Ural 1091 Tmutarakan Exams [容斥原理]
- Ural 1091 Tmutarakan Exams【容斥原理】
- ACdreamoj1114(Number theory)莫比乌斯容斥
- POJ 1091 跳蚤 【容斥】【质因数分解】
- ural 1091
- 容斥原理1004 POJ 1091
- [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]
- ural 1091. Tmutarakan Exams 容斥
- URAL 1091