## [$>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的更多相关文章

  1. codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理

    题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...

  2. Codeforces 439E Devu and Birthday Celebration 容斥

    Devu and Birthday Celebration 我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥. #include<bits/std ...

  3. 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 ...

  4. Codeforces 451 E Devu and Flowers

    Discription Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th ...

  5. 【Codeforces 258E】 Devu and Flowers

    [题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...

  6. codeforces#439 D. Devu and his Brother (二分)

    题意:给出a数组和b数组,他们的长度最大1e5,元素范围是1到1e9,问你让a数组最小的数比b数组最大的数要大需要的最少改变次数是多少.每次改变可以让一个数加一或减一 分析:枚举a数组和b数组的所有的 ...

  7. codeforces 493 div1 e

    题解: 和这件zhcs的那题有点像 第一种做法是考虑i,i+1之间的贡献 这样子就是矩形加减然后求矩形最小值个数 另一种做法是我们从左向右维护mx-nx-r+l 跟之前那题一样我们知道这个的最小值为0 ...

  8. 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 ...

  9. codeforces 493 C Vasya and Basketball

    题意:给出三分线的值d,分别有两支队伍,如果小于等于d,得2分,如果大于d,得三分,问使得a-b最大时的a,b 一看到题目,就想当然的去二分了----啥都没分出来---55555555 后来才知道不能 ...

随机推荐

  1. LintCode 397: Longest Increasing Continuous Subsequence

    LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...

  2. 【leetcode 简单】第十八题 爬楼梯

    假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解释: 有两 ...

  3. iOS中响应者链条-触摸事件,hitTest方法坐标转换

    总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...

  4. VMvare 复制的数据库,需要改变的配置

    当我在VMware 上安装了一个linux虚拟机,同时在虚拟机上安装了一系列软件(包括数据库) 我们会修改hostname  ,修改后 对于数据库:我们要把/u01/app/oracle/produc ...

  5. sicily 1003. Hit or Miss

    Description One very simple type of solitaire game known as "Hit or Miss" (also known as & ...

  6. python的sorted函数对字典按value进行排序

    场景:词频统计时候,我们往往要对频率进行排序 sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数.其中iterable表示可 ...

  7. Spring Boot学习——单元测试

    本随笔记录使用Spring Boot进行单元测试,主要是Service和API(Controller)进行单元测试. 一.Service单元测试 选择要测试的service类的方法,使用idea自动创 ...

  8. centos上git搭建

    1 git的安装需要一些包: yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-Ex ...

  9. Spring之IOC,DI,动态代理,反射

    Spring框架是J2EE开发中一个使用广泛的框架,它使得dao和service层的维护更加便利.Spring框架有两个重要的特征,一个是IOC,另一个是AOP.我们在这里主要介绍IOC,以及IOC中 ...

  10. gan对抗式网络

    感觉好厉害,由上图噪声,生成左图噪声生成右图以假乱真的图片, gan网络原理: 本弱又盗了一坨博文,不是我写的,如下:(跪膜各路大神) 前面我们已经讲完了一般的深层网络,适用于图像的卷积神经网络,适用 ...