Codeforces 493 E.Devu and Birthday Celebration
## [$>Codeforces \space 493\ E.Devu\ and\ Birthday\ Celebration
题目大意 : 有 \(q\) 组询问,每次有 \(n\) 小朋友和 \(m\) 个苹果,求有多少种方案,使得每个小朋友都分到一个苹果,且每个小朋友分到的苹果的数目的 \(gcd = 1\),输出答案对 \(10^9 + 7\) 取模的值
\(1 \leq n, m, q \leq 10^5\)
解题思路 :
设 \(f(n)\) 表示分苹果的方案满足 \(n | gcd\) 的方案数,\(g(n)\) 表示分苹果的方案满足 \(gcd = n\) 的方案数
显然有 \(f(n) = \sum_{n|d} g(d)\)
考虑到 \(f(n)\) 本质上是将 \(\frac{M}{n}\) 个球,放到 \(N\) 个不同盒子里盒子不能为空的方案数,插板法组合数求解即可
通过简单的莫比乌斯反演可以得到 \(g(n) = \sum_{n|d} \mu(\frac{n}{d}) f(d)\)
实际上对于每一组询问我们要求的就是 \(g(1)\) 的值,而对于 \(f(n)\) 来说,只有当 \(n|M\) 的时候才可能有值
所以只需要大力枚举 \(M\) 的因子,预处理组合数和莫比乌斯函数 \(O(1)\) 计算单个 \(f\) 的值即可
总复杂度是 \(O(Q\sqrt{m})\)
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
const int N = 200005, Mod = 1000000007;
int js[N], mu[N], b[N], prime[N], inv[N], tot, n, m;
inline int Pow(int a, int b){
int ans = 1;
for(; b; b >>= 1, a = a * a % Mod)
if(b & 1) ans = ans * a % Mod;
return ans;
}
inline void Prework(){
mu[1] = 1, js[0] = 1, inv[0] = 1;
for(int i = 1; i < N; i++)
js[i] = js[i-1] * i % Mod, inv[i] = Pow(js[i], Mod - 2);
for(int i = 2; i < N; i++){
if(!b[i]) prime[++tot] = i, mu[i] = -1;
for(int j = 1; j <= tot && i * prime[j] < N; j++){
b[i*prime[j]] = 1;
if(i % prime[j]) mu[i*prime[j]] = -mu[i]; else break;
}
}
}
inline int C(int n, int i){ return js[n] * inv[i] % Mod * inv[n-i] % Mod; }
inline int calc(int x){ return x < m ? 0 : C(x - 1, m - 1) * mu[n/x] % Mod; }
inline void solve(){
int ans = 0; read(n), read(m);
int num = (int) sqrt(n);
for(int i = 1; i <= num; i++) if(n % i == 0){
int x = i, y = n / i;
(ans += calc(x)) %= Mod;
if(x != y) (ans += calc(y)) %= Mod;
}
printf("%lld\n", ((ans % Mod) + Mod) % Mod);
}
signed main(){
Prework();
int T; read(T); while(T--) solve();
return 0;
}
Codeforces 493 E.Devu and Birthday Celebration的更多相关文章
- codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理
题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...
- Codeforces 439E Devu and Birthday Celebration 容斥
Devu and Birthday Celebration 我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥. #include<bits/std ...
- Codeforces 439 A. Devu, the Singer and Churu, the Joker
这是本人第一次写代码,难免有点瑕疵还请见谅 A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory l ...
- Codeforces 451 E Devu and Flowers
Discription Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th ...
- 【Codeforces 258E】 Devu and Flowers
[题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...
- codeforces#439 D. Devu and his Brother (二分)
题意:给出a数组和b数组,他们的长度最大1e5,元素范围是1到1e9,问你让a数组最小的数比b数组最大的数要大需要的最少改变次数是多少.每次改变可以让一个数加一或减一 分析:枚举a数组和b数组的所有的 ...
- codeforces 493 div1 e
题解: 和这件zhcs的那题有点像 第一种做法是考虑i,i+1之间的贡献 这样子就是矩形加减然后求矩形最小值个数 另一种做法是我们从左向右维护mx-nx-r+l 跟之前那题一样我们知道这个的最小值为0 ...
- CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...
- codeforces 493 C Vasya and Basketball
题意:给出三分线的值d,分别有两支队伍,如果小于等于d,得2分,如果大于d,得三分,问使得a-b最大时的a,b 一看到题目,就想当然的去二分了----啥都没分出来---55555555 后来才知道不能 ...
随机推荐
- 【BZOJ】1572: [Usaco2009 Open]工作安排Job
[题意]给定n项工作的截止时间和价值,每项工作需要1单位时间完成,求最大价值.n<=10^5. [算法]贪心+堆 [题解] 如果是访问到x时将d[x]前的点从价值最大的能加就加是错误的贪心,因为 ...
- vsftpd 安装配置详细教程
linux下ftp软件不少,大致特点:<br /> wu-ftp:比较老牌,但针对它的攻击比较多,设置比较麻烦,但功能比较强大.<br /> vsftpd:功能强大,配置也比较 ...
- POJ 3026 Borg Maze(Prim+bfs求各点间距离)
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
- 数据分析python应用到的ggplot(二)
还是优达学院的第七课 数据:https://s3.amazonaws.com/content.udacity-data.com/courses/ud359/hr_by_team_year_sf_la. ...
- Mariadb 10.2中的json使用及应用场景思考
-- 创建示例表DROP TABLE IF EXISTS `t_base_user`;CREATE TABLE `t_base_user` ( `USER_ID` char(36) CHARACT ...
- 救济金发放(UVa133)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- spring_150807_hibernate_transaction_annotation
实体类: package com.spring.model; import javax.persistence.Entity; import javax.persistence.Id; import ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- grep 同时排除多个关键字
不说废话, 例如需要排除 abc.txt 中的 mmm nnn grep -v 'mmm\|nnn' abc.txt 再举个例子,需要确定mac 的本机ip地址, 显然直接可以输入 ifcon ...
- email 校验
email 校验: javascript: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/ java: ^([a- ...