文章目录

  • 题目
  • 分析
  • 代码

题目

[CodeForces 439E] Devu and Birthday Celebration

分析

莫比乌斯函数比较重要的性质: μ ∗ 1 = ε \mu * 1 = \varepsilon μ∗1=ε 即 ∑ d ∣ n μ ( d ) = [ n = 1 ] \sum_{d | n}\mu(d) = [n = 1] d∣n∑​μ(d)=[n=1]

证明:
设 n = ∏ i = 1 m p i α i n = \prod_{i = 1}^{m} p_i^{\alpha_i} n=∏i=1m​piαi​​, n ′ = ∏ i = 1 m p i n' = \prod_{i = 1}^mp_i n′=∏i=1m​pi​,有 ∑ d ∣ n μ ( d ) = ∑ d ∣ n ′ μ ( d ) = ∑ i = 0 m ( m i ) ( − 1 ) i \sum_{d | n}\mu(d) = \sum_{d | n'}\mu(d) = \sum_{i = 0}^{m} \binom{m}{i} (-1)^i d∣n∑​μ(d)=d∣n′∑​μ(d)=i=0∑m​(im​)(−1)i 由二项式定理,当 n = 1 n = 1 n=1 时,上式为 1 1 1;当 n ≠ 1 n \neq 1 n​=1 时,上式为 [ 1 + ( − 1 ) ] m = 0 [1 + (-1)]^m = 0 [1+(−1)]m=0。

∑ a 1 = 1 n ∑ a 2 = 1 n ⋯ ∑ a f = 1 n [ ∑ i = 1 f a i = n ] [ gcd ⁡ i = 1 f a i = 1 ] = ∑ a 1 = 1 n ∑ a 2 = 1 n ⋯ ∑ a f = 1 n [ ∑ i = 1 f a i = n ] ∑ d ∣ gcd i = 1 f a i μ ( d ) = ∑ d ∣ n μ ( d ) ∑ k 1 = 1 n d ∑ k 2 = 1 n d ⋯ ∑ k f = 1 n d [ ∑ i = 1 f k i = n d ] \begin{aligned} &\sum_{a_1 = 1}^n \sum_{a_2 = 1}^n \cdots \sum_{a_f = 1}^n \left[\sum_{i = 1}^fa_i = n\right] \left[\gcd_{i = 1}^f a_i = 1\right] \\ =& \sum_{a_1 = 1}^n \sum_{a_2 = 1}^n \cdots \sum_{a_f = 1}^n \left[\sum_{i = 1}^f a_i = n\right] \sum_{d |\text{gcd}_{i = 1}^f a_i} \mu(d) \\ =& \sum_{d | n} \mu(d) \sum_{k_1 =1}^{\frac{n}{d}} \sum_{k_2 =1}^{\frac{n}{d}} \cdots \sum_{k_f =1}^{\frac{n}{d}} \left[\sum_{i = 1}^f k_i = \frac{n}{d} \right]\end{aligned} ==​a1​=1∑n​a2​=1∑n​⋯af​=1∑n​[i=1∑f​ai​=n][i=1gcdf​ai​=1]a1​=1∑n​a2​=1∑n​⋯af​=1∑n​[i=1∑f​ai​=n]d∣gcdi=1f​ai​∑​μ(d)d∣n∑​μ(d)k1​=1∑dn​​k2​=1∑dn​​⋯kf​=1∑dn​​[i=1∑f​ki​=dn​]​(转换枚举方式, a i = d ⋅ k i a_i = d\cdot k_i ai​=d⋅ki​,由于 d ∣ a i d | a_i d∣ai​,所以 d ∣ ∑ i = 1 f a i d | \sum_{i = 1}^f a_i d∣∑i=1f​ai​ 即 d ∣ n d | n d∣n)

预处理莫比乌斯函数,然后 O ( n ) O(\sqrt{n}) O(n ​) 枚举因数 + 隔板法即可。

代码

#include <bits/stdc++.h>typedef long long LL;template <const int _MOD> struct ModNumber {int x;inline ModNumber() { x = 0; }inline ModNumber(const int &y) { x = y; }inline int Int() { return x; }inline ModNumber Pow(int y) const { register int ret = 1, tmp = x; while (y) { if (y & 1) ret = ((LL)ret * tmp) % _MOD; y >>= 1; tmp = ((LL)tmp * tmp) % _MOD; } return ModNumber(ret); }inline bool operator == (const ModNumber &y) const { return x == y.x; }inline bool operator != (const ModNumber &y) const { return x != y.x; }inline bool operator < (const ModNumber &y) const { return x < y.x; }inline bool operator > (const ModNumber &y) const { return x > y.x; }inline bool operator <= (const ModNumber &y) const { return x <= y.x; }inline bool operator >= (const ModNumber &y) const { return x >= y.x; }inline ModNumber operator - () const { return _MOD - x; }inline ModNumber operator + (const ModNumber &y) const { return (x + y.x >= _MOD) ? (x + y.x - _MOD) : (x + y.x); }inline ModNumber operator - (const ModNumber &y) const { return (x - y.x < 0) ? (x - y.x + _MOD) : (x - y.x); }inline ModNumber operator * (const ModNumber &y) const { return ModNumber((LL)x * y.x % _MOD); }inline ModNumber operator / (const ModNumber &y) const { return *this * y.Pow(_MOD - 2); }inline ModNumber operator ^ (const int &y) const { return Pow(y); }inline void operator += (const ModNumber &y) { *this = *this + y; }inline void operator *= (const ModNumber &y) { *this = *this * y; }inline void operator -= (const ModNumber &y) { *this = *this - y; }inline void operator /= (const ModNumber &y) { *this = *this / y; }inline void operator ^= (const int &y) const { *this = *this ^ y; }inline bool operator == (const int &y) const { return x == y; }inline bool operator != (const int &y) const { return x != y; }inline bool operator < (const int &y) const { return x < y; }inline bool operator > (const int &y) const { return x > y; }inline bool operator <= (const int &y) const { return x <= y; }inline bool operator >= (const int &y) const { return x >= y; }inline ModNumber operator + (const int &y) const { return (x + y >= _MOD) ? (x + y - _MOD) : (x + y); }inline ModNumber operator - (const int &y) const { return (x - y < 0) ? (x - y + _MOD) : (x - y); }inline ModNumber operator * (const int &y) const { return ModNumber((LL)x * y % _MOD); }inline ModNumber operator / (const int &y) const { return *this * ModNumber(y).Pow(_MOD - 2); }inline void operator += (const int &y) { *this = *this + y; }inline void operator *= (const int &y) { *this = *this * y; }inline void operator -= (const int &y) { *this = *this - y; }inline void operator /= (const int &y) { *this = *this / y; }
};const int MAXN = 2 * 100000;
const int MOD = 1000000007;typedef ModNumber<MOD> Int;Int Fac[MAXN + 5], Inv[MAXN + 5];Int Mu[MAXN + 5];
bool Vis[MAXN + 5];
std::vector<int> Primes;void Init(int n) {Mu[1] = 1;for (int i = 2; i <= n; i++) {if (!Vis[i])Mu[i] = MOD - 1, Primes.push_back(i);for (int j = 0; j < (int)Primes.size() && i * Primes[j] <= n; j++) {Vis[i * Primes[j]] = true;if (i % Primes[j] == 0) {Mu[i * Primes[j]] = 0;break;}Mu[i * Primes[j]] = -Mu[i];}}Fac[0] = 1;for (int i = 1; i <= n; i++)Fac[i] = Fac[i - 1] * i;Inv[n] = Fac[n] ^ (MOD - 2);for (int i = n - 1; i >= 0; i--)Inv[i] = Inv[i + 1] * (i + 1);
}Int C(int n, int m) {if (n < m) return 0;return Fac[n] * Inv[m] * Inv[n - m];
}int main() {Init(MAXN);int Q; scanf("%d", &Q);while (Q--) {Int Ans = 0;int N, F; scanf("%d%d", &N, &F);for (int i = 1; i * i <= N; i++) {if (N % i) continue;int j = N / i; Ans += Mu[i] * C(j - 1, F - 1);if (j != i) Ans += Mu[j] * C(i - 1, F - 1);}printf("%d\n", Ans.x);}return 0;
}

[CodeForces 439E] Devu and Birthday Celebration(莫比乌斯反演) | 错题本相关推荐

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

  2. Codeforces.1139D.Steps to One(DP 莫比乌斯反演)

    题目链接 啊啊啊我在干什么啊.怎么这么颓一道题做这么久.. 又记错莫比乌斯反演式子了(╯‵□′)╯︵┻━┻ \(Description\) 给定\(n\).有一个初始为空的集合\(S\).令\(g\) ...

  3. 莫比乌斯反演 做题记录

    来自Peterwuyihong 的题单. 前置知识 前置芝士1 数论分块 UVA11526 H(n) P2261 [CQOI2007]余数求和 P2260 [清华集训2012]模积和 其中有一个式子需 ...

  4. BZOJ1011 莫比乌斯反演(基础题

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1101 [题目大意] 求[1,n][1,m]内gcd=k的情况 [题解] 考虑求[1,n ...

  5. CodeForces - 1305D Kuroni and the Celebration(思维,互动题)

    题目链接:点击查看 题目大意:给出一个由 n 个点组成的树,现在可以询问 n/2 次(向下取整)LCA,确定根节点是哪个节点 题目分析:因为最多只能求 n/2 次lca,每次需要两个节点作为参数,也就 ...

  6. 莫比乌斯反演杂题题解

    文章目录 一个小结论 证明: P4450 双亲数 题意: solution: 代码: P2522 [HAOI2011]Problem b 题意: solution: 代码 P3327 [SDOI201 ...

  7. [复习]莫比乌斯反演,杜教筛,min_25筛

    [复习]莫比乌斯反演,杜教筛,min_25筛 莫比乌斯反演 做题的时候的常用形式: \[\begin{aligned}g(n)&=\sum_{n|d}f(d)\\f(n)&=\sum_ ...

  8. 莫比乌斯反演入门题目(详细)

    目录 luoguP2568 GCD hdu1695 GCD luogu3455[POI2007]ZAP-Queries luogu2522 [HAOI2011]Problem b P4318 完全平方 ...

  9. HYSBZ - 2818 Gcd —— 莫比乌斯反演

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 8172  Solved: 3609 Description 给定整数N,求1&l ...

最新文章

  1. CF A. DZY Loves Hash
  2. 耶鲁计算机科学专业,Top3美国名校耶鲁大学录取 计算机科学专业
  3. C++描述杭电OJ 2000. ASCII码排序 ||
  4. 使用Lambda的装饰设计模式
  5. 腾讯ai开放平台 手册_创建手册以实现大规模开放
  6. java中io流浅析
  7. Centos7.6环境使用kubeadm部署kubernetes1.18.4
  8. Ubuntu下查看隐藏文件
  9. 贼好用的对比工具--BeyondComper
  10. 建筑灭火器配置设计规范
  11. [搜索引擎]Sphinx的介绍和原理探索
  12. Recap | Apache Pulsar Meetup 上海站
  13. c# 中 event 和 delegate 的区别
  14. Selector空轮询
  15. Spring-Mybatis整合 第一个Spring-Mybatis程序
  16. Kafka: Consumer
  17. 计算机主板尺寸,电脑主板大中小三个等级的尺寸是多少?
  18. pycharm新建项目环境设置详解
  19. Error: mkl-service + Intel(R) MKL: MKL_THREADING_LAYER=INTEL is incompatible with libgomp.so.1 libra
  20. 【无标题】Android10 编译错误

热门文章

  1. 获取文件夹的子文件夹名字
  2. Maven clean和install的区别
  3. Hadoop和大数据技术精讲班
  4. QnA,一个 Hexo FAQ 主题
  5. matlab学习中遇到的一些语句(记录下来)
  6. 网络虚拟化——vduse
  7. vs code 插件 koroFileHeader
  8. HyperLink与LinkButton的区别
  9. 上海亚商投顾:沪指逼近3400点 CPO概念股再度爆发
  10. 刚需市场“论文查重”副业思路,掌握方法轻松月入过万