## [$>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. 【acmm】一道简单的数学题

    emm卡常 我本来写成了这个样子: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; struct ...

  2. 【BZOJ】1834 [ZJOI2010]network 网络扩容

    [算法]网络流-最大流+最小费用最大流(费用流) [题解] 第一问跑最大流. 第二问: 原始边相当于费用为0的边,再原图(跑过最大流的图)基础上添加带费用的边,容量为k(相当于inf). 第一问最大流 ...

  3. 首次成功的web渗透

    web渗透 今天给大家讲一个最近做的一件令我振奋的一件事情 渗透培训刚刚结束的第二天 我在公网上挖到了我人生中的第一个站 总体来说个人真的很振奋人心      这个网站还没有进行更改但我已经通知了他们 ...

  4. slave->pxc后GTID不一致

    以下两个参数在两个节点是对得上的. | wsrep_last_applied | 3363764 | | wsrep_last_committed | 3363764 但show master sta ...

  5. FileZilla 配置备份与还原【转】

    FileZilla是一款免费开源的FTP软件,安装和配置都很简单.在安装目录下的FileZilla Server Interface.xml和FileZilla Server.xml两个文件是程序的配 ...

  6. ELK&ElasticSearch5.1基础概念及配置文件详解【转】

    1. 配置文件 elasticsearch/elasticsearch.yml 主配置文件 elasticsearch/jvm.options jvm参数配置文件 elasticsearch/log4 ...

  7. videojs做直播、弹幕

    从上一年开始,我们开始接触直播,现在直播成本真的很低,很多CDN供应商都有提供,本文只是大概讲述播放器这个话题. 开始调研 播放格式,我挑了三种.分别是HLS,RTMP,HTTP-FLV. 下面简单说 ...

  8. java-String中的 intern()

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  9. [ python ] 变量及基础的数据类型

    python2 和 python3 不同的编码方式 python2 默认编码方式是 ascii码 python3 默认编码方式是 utf-8 具体表现为:当 python3 和 python2 在打印 ...

  10. Crypt加密函数简介(C语言)

    定义函数 char * crypt (const char *key,const char * salt); 函数说明 crypt是个密码加密函数,它是基于Data Encryption Standa ...