51nod 1594 Gcd and Phi(莫比乌斯反演)
题目链接
思路
如果这题是这样的:
\]
那么我们可能会想到下面方法进行反演:
F(n)=&\sum\limits_{k=1}^{n}\phi(k)\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}[gcd(i,j)=k]&\\
=&\sum\limits_{k=1}^{n}\phi(k)\sum\limits_{i=1}^{\frac{n}{k}}\sum\limits_{j=1}^{\frac{n}{k}}[gcd(i,j)=1]&\\
\end{aligned}
\]
令\(f(n)\)为\(gcd(i,j)=n\)的有序对的对数,\(g(n)\)为\(gcd(i,j)=n\text{和}n\)的倍数的有序对的对数,则
&g(n)=\sum\limits_{n|d}f(d)&\\
\rightarrow&f(n)=\sum\limits_{n|d}\mu(\frac{d}{n})g(d)&
\end{aligned}
\]
然后将\(f(1)=\sum\limits_{i=1}^{n}\mu(i)g(i)\)代入\(F(n)\)得到\(F(n)=\sum\limits_{k=1}^{n}\phi(k)\sum\limits_{i=1}^{\frac{n}{k}}\mu(i)g(i)\),然后先预处理\(\sum\limits_{i=1}^{\frac{n}{k}}\mu(i)g(i)\),然后暴力枚举\(k\)或者数论分块求解答案就可以了,这里有几道类似的题目,有兴趣的可以做一下。
但是这题却不是我们所希望的那种形式,那么该怎么办呢?我们发现\(n\)小于等于\(2e6\),那么我们可以记录一下每个数的\(\phi\)是多少,然后记录一下每个\(\phi\)出现的次数后就可以转换成上述题意了(这个思想昨天的\(CCPC\)网络赛1010也用到了,不过这题由于后面不会处理就没补了23333),假设\(c_i\)表示\(\phi=i\)的个数,那么求解的式子就变成了
\]
令\(f(n)\)为\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}c_ic_j[gcd(i,j)=n]\),\(g(n)\)为\(为和的倍数\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}c_ic_j[gcd(i,j)\text{为}n\text{和}n\text{的倍数}]\),则
g(n)=&\sum\limits_{n|i}\sum\limits_{n|j}c_ic_j&\\
=&\sum\limits_{i=1}^{n}c_i\sum\limits_{j=1}^{n}c_J&\\
=&(\sum\limits_{i=1}^{n}c_i)^2&\\
=&\sum\limits_{n|d}f(d)&
\end{aligned}
\]
那么
\]
最后答案为
\]
然后暴力枚举\(k\)求解即可。
代码
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 2000000 + 2;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
bool v[maxn];
int t, n, cnt;
int F[maxn], c[maxn], phi[maxn], p[maxn], mu[maxn];
void init() {
phi[1] = mu[1] = 1;
for(int i = 2; i < maxn; ++i) {
if(!v[i]) {
p[cnt++] = i;
phi[i] = i - 1;
mu[i] = -1;
}
for(int j = 0; j < cnt && i * p[j] < maxn; ++j) {
v[i*p[j]] = 1;
phi[i*p[j]] = phi[i] * (p[j] - 1);
mu[i*p[j]] = -mu[i];
if(i % p[j] == 0) {
mu[i*p[j]] = 0;
phi[i*p[j]] = phi[i] * p[j];
break;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
time_t s = clock();
#endif
init();
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
++c[phi[i]];
}
F[1] = c[1];
for(int i = 2; i <= n; ++i) {
F[1] += c[i];
for(int j = 1; j <= n / i; ++j) {
F[i] += c[i*j];
}
}
LL sum = 1LL * F[1] * F[1], ans = 0;
for(int i = 2; i <= n; ++i) {
sum += 1LL* F[i] * F[i] * mu[i];
LL tmp = 0;
for(int j = 1; j <= n / i; ++j) {
tmp += 1LL * F[i*j] * F[i*j] * mu[j];
}
ans += tmp * phi[i];
}
ans += sum;
printf("%lld\n", ans);
for(int i = 1; i <= n; ++i) F[i] = c[i] = 0;
}
#ifndef ONLINE_JUDGE
printf("It costs %.3fs\n", 1.0 * (clock() - s) / CLOCKS_PER_SEC);
#endif
return 0;
}
51nod 1594 Gcd and Phi(莫比乌斯反演)的更多相关文章
- 51nod 1594 Gcd and Phi 反演
OTZ 又被吊打了...我当初学的都去哪了??? 思路:反演套路? 提交:\(1\)次 题解: 求\(\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(gcd(\varphi(i ...
- 【CJOJ2512】gcd之和(莫比乌斯反演)
[CJOJ2512]gcd之和(莫比乌斯反演) 题面 给定\(n,m(n,m<=10^7)\) 求 \[\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)\] 题解 首先把公因数直 ...
- 【51nod】1594 Gcd and Phi
题解 跟随小迪学姐的步伐,学习一下数论 小迪学姐太巨了! 这道题的式子很好推嘛 \(\sum_{i = 1}^{n} \sum_{j = 1}^{n} \sum_{d|\phi(i),\phi(j)} ...
- 【Project Euler】530 GCD of Divisors 莫比乌斯反演
[题目]GCD of Divisors [题意]给定f(n)=Σd|n gcd(d,n/d)的前缀和F(n),n=10^15. [算法]莫比乌斯反演 [题解]参考:任之洲数论函数.pdf 这个范围显然 ...
- [luogu P2586] GCD 解题报告 (莫比乌斯反演|欧拉函数)
题目链接:https://www.luogu.org/problemnew/show/P2568#sub 题目大意: 计算$\sum_{x=1}^n\sum_{y=1}^n [gcd(x,y)==p ...
- bnu——GCD SUM (莫比乌斯反演)
题目:GCD SUM 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=39872 算法:莫比乌斯反演.优化 #include<stdio.h& ...
- GCD HDU - 1695 莫比乌斯反演入门
题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/ ...
- HDU - 4675 GCD of Sequence (莫比乌斯反演+组合数学)
题意:给出序列[a1..aN],整数M和k,求对1-M中的每个整数d,构建新的序列[b1...bN],使其满足: 1. \(1 \le bi \le M\) 2. \(gcd(b 1, b 2, -, ...
- 【HDU4947】GCD Array(莫比乌斯反演+树状数组)
点此看题面 大致题意: 一个长度为\(n\)的数组,实现两种操作:将满足\(gcd(i,k)=d\)的\(a_i\)加上\(v\),询问\(\sum_{i=1}^xa_i\). 对于修改操作的推式子 ...
随机推荐
- 复杂模拟 | 1095 模拟N个学生有K个志愿填M个学校
妈的智障 #include <stdio.h> #include <memory.h> #include <math.h> #include <string& ...
- Educational Codeforces Round 70 题解
噩梦场. 题目出奇的难,好像一群外国老哥看 A 看着看着就哭了-- A 找到 \(b\) 最低的 \(1\),这个 \(1\) 肯定要跟 A 中的一个 \(1\) 搭配,而且是能搭配的 \(1\) 中 ...
- [LeetCode] 547. Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- [LeetCode] 315. Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The countsarray has t ...
- Elasticsearch由浅入深(七)搜索引擎:_search含义、_multi-index搜索模式、分页搜索以及深分页性能问题、query string search语法以及_all metadata原理
_search含义 _search查询返回结果数据含义分析 GET _search { , "timed_out": false, "_shards": { , ...
- Kubernetes 学习(九)Kubernetes 源码阅读之正式篇------核心组件之 Scheduler
0. 前言 继续上一篇博客阅读 Kubernetes 源码,参照<k8s 源码阅读>首先学习 Kubernetes 的一些核心组件,首先是 kube-scheduler 本文严重参考原文: ...
- 阿里云虚拟空间.net 网站错误
Web.config 文件的 <compilation> 元素中的“targetFramework”特性仅用于目标 .NET Framework 版本 4.0 或更高版本(例如“< ...
- AbstractExecutorService源码
public class RunnableFutureTask { static FinalizableDelegatedExecutorService executorService = (Fina ...
- Phaser也可以实现countdownLatch的功能
/** * 可用用phaser模拟countDownLatch * awaitAdvance方法:如果传入的参数和当前的phase相等,线程就阻塞住等待phase的值增加:否则就立即返回 */ pub ...
- Mysql select into outfile 命令
[1]Mysql select into outfile命令 在Mysql中,与load data infile命令作用相反的一个命令是select into outfile命令 select int ...