## [$>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. 【BZOJ】4316: 小C的独立集 静态仙人掌

    [题意]给定仙人掌图,求最大独立集(选择最大的点集使得点间无连边).n<=50000,m<=60000. [算法]DFS处理仙人掌图 [题解]参考:[BZOJ]1023: [SHOI200 ...

  2. 【leetcode 简单】第十三题 最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  3. nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-type,first-child,last-child伪类区别和用法

    我将这坨伪类分成三组,第一组:nth-child,nth-last-child,only-child第二组:nth-of-type,nth-last-of-type,第三组:first-of-tpye ...

  4. ruby post json

    require 'net/http' require 'json' uri = URI('http://localhost/test1.php') req = Net::HTTP::Post.new ...

  5. sniffer简单使用

    跟wireshark类似. 只是说显示的容易忘记所以丢张图记录一下. 该工具还是很坑爹的,不是比赛要用到所以都不是很想弄.一般机器运行不起来.不是蓝屏就是装了运行不了各种闪退,找了学校一台内网服务器才 ...

  6. Appium 1.6.3使用的自动化测试引擎

    automationName项的值: Appium:默认值. Selendroid:安卓2.3(API 9)-4.1(API 16)版本使用. UiAutomator2:最新安卓版本. XCUITes ...

  7. 使用angluar-cli的ng g component home指令出现的错误

    Error: ELOOP: too many symbolic links encountered, stat '/Users/zzy/angular/taskmgr/node_modules/@an ...

  8. falsk注册etcd

    部署web服务集群时,我们希望能动态调整集群大小.当一个新的节点启动时,可以将自己的信息注册给master, 让master把它加入到集群里, 关闭之后也可以把自己从集群中删除.我这里使用的是flas ...

  9. navigator.geolocation详解

    https://blog.csdn.net/qq_27626333/article/details/51815467 PositionOptions: JSON对象,监听设备位置信息参数 naviga ...

  10. 接口测试(概念、Postman、SoapUI、jmeter)

    一.什么是接口测试 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑 ...