洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)
题目链接:P3455 [POI2007]ZAP-Queries
题意
给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\)。
思路
莫比乌斯函数的一个性质:
\]
设 \(a \le b\),对原式转化:
= \sum_{x=1}^{\lfloor \frac{a}{d} \rfloor} \sum_{y=1}^{\lfloor \frac{b}{d} \rfloor}[gcd(x, y) = 1] \\
= \sum_{x=1}^{\lfloor \frac{a}{d} \rfloor} \sum_{y=1}^{\lfloor \frac{b}{d} \rfloor} \sum_{d'|gcd(x, y)} \mu(d') \\
= \sum_{x=1}^{\lfloor \frac{a}{d} \rfloor} \sum_{y=1}^{\lfloor \frac{b}{d} \rfloor} \sum_{d'=1}^{\lfloor \frac{a}{d} \rfloor} \mu(d') \cdot [d'|gcd(x, y)] \\
= \sum_{d'=1}^{\lfloor \frac{a}{d} \rfloor} \mu(d') \sum_{x=1}^{\lfloor \frac{a}{d} \rfloor} \sum_{y=1}^{\lfloor \frac{b}{d} \rfloor} [d'|gcd(x, y)] \\
= \sum_{d'=1}^{\lfloor \frac{a}{d} \rfloor} \mu(d') \sum_{x=1}^{\lfloor \frac{a}{d} \rfloor} \sum_{y=1}^{\lfloor \frac{b}{d} \rfloor} [d'|x \wedge d'|y] \\
= \sum_{d'=1}^{\lfloor \frac{a}{d} \rfloor} \mu(d') \sum_{x=1}^{\lfloor \frac{a}{d} \rfloor} [d'|x] \sum_{y=1}^{\lfloor \frac{b}{d} \rfloor} [d'|y] \\
= \sum_{d'=1}^{\lfloor \frac{a}{d} \rfloor} \mu(d') \lfloor \frac{a}{dd'} \rfloor {\lfloor \frac{b}{dd'} \rfloor}\\
\]
然后预处理 \(\mu\) 的前缀和,用一下整除分块求解。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 10;
ll a, b, d;
ll mu[maxn], vis[maxn], sum[maxn];
vector<ll> p;
// 莫比乌斯函数的前缀和
void init() {
sum[1] = mu[1] = 1;
for(int i = 2; i < maxn; ++i) {
if(!vis[i]) {
mu[i] = -1;
p.push_back(i);
}
for(int j = 0; j < p.size() && i * p[j] < maxn; ++j) {
vis[i * p[j]] = 1;
if(i % p[j] == 0) break;
mu[i * p[j]] = -mu[i];
}
}
for(int i = 2; i < maxn; ++i) {
sum[i] = sum[i - 1] + mu[i];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
init();
int T;
cin >> T;
while(T--) {
cin >> a >> b >> d;
if(a > b) {
swap(a, b);
}
a /= d;
b /= d;
ll ans = 0;
// 整除分块
for(int i = 1, j; i <= a; i = j + 1) {
j = min(a / (a / i), b / (b / i));
ans += (sum[j] - sum[i - 1]) * (a / i) * (b / i);
}
cout << ans << endl;
}
return 0;
}
洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)的更多相关文章
- 洛谷P3455 [POI2007]ZAP-Queries (莫比乌斯反演)
题意:求$\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)==d]$(1<=a,b,d<=50000). 很套路的莫比乌斯反演. $\sum_{i=1}^{n}\ ...
- 洛谷P3455 [POI2007]ZAP-Queries(莫比乌斯反演)
传送门 设$$f(k)=\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=k]$$ $$g(n)=\sum_{n|k}f(k)=\lfloor\frac{a}{n}\rflo ...
- 洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301
https://www.luogu.org/problemnew/show/P3455 就是https://www.cnblogs.com/hehe54321/p/9315244.html里面的方法2 ...
- 【刷题】洛谷 P3455 [POI2007]ZAP-Queries
题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...
- 洛谷P3455 [POI2007]ZAP-Queries
题目大意: 给定\(n,m,k,\) 求 \[\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)==k]\] 莫比乌斯反演入门题,先进行一步转化,将每个\( ...
- 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)
先看一道例题:[POI2007]Zap BZOJ 洛谷 题目大意:$T$ 组数据,求 $\sum^n_{i=1}\sum^m_{j=1}[gcd(i,j)=k]$ $1\leq T\leq 50000 ...
- 洛谷 [P3455] ZAP
莫比乌斯函数 #include <iostream> #include <cstdio> #include <cmath> #include <cstring ...
- 【BZOJ1101】[POI2007] Zap(莫比乌斯反演)
点此看题面 大致题意: 求\(\sum_{x=1}^N\sum_{y=1}^M[gcd(x,y)==d]\). 一道类似的题目 推荐先去做一下这道题:[洛谷2257]YY的GCD,来初步了解一下莫比乌 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis(树链剖分,Splay)
洛谷题目传送门 正解是树状数组维护dfn序上的前缀和,这样的思路真是又玄学又令我惊叹( 我太弱啦,根本想不到)Orz各路Dalao 今天考了这道题,数据范围还比洛谷的小,只有\(10^5\)(害我复制 ...
随机推荐
- javascript获取网页宽高,屏幕宽高,屏幕分辨率等
<script> var s = ""; s += "\r\n网页可见区域宽:"+ document.body.clientWidth; s + ...
- App知识点(持续更新......)
1.app的性能测试,即专项测试,需要重点关注那些方面? 内存.cpu占用.耗电量.流量.流畅度等 2.什么是activity?它的生命周期? Activity是一个Android的应用组件,它提供屏 ...
- 启动项目时出现Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (72)
前几天趁假期重新装了一次系统,重新安装各种配置之后再启动项目的时候就报这个错误 第一反应就是去搜这个错误怎么解决,搜来搜去基本上都是让我重新安装node-sass,但我重装node-sass的时候又出 ...
- js的几个特殊的运算符略解
js运算符的一些特殊应用及使用技巧. 1. 是否包含指定字符: ~ ~"str1".indexOf("str2") 含义为:str1 被查找的字符串 str2 ...
- python 打印日历
import calendar as c'''x = c.monthcalendar(2017,11) 使用这个结果打印出日历 s = 1while s <= 7: print('周%d '%( ...
- VMware中Centos7的静态ip设置
网络连接方式:桥接模式.修改后确定.启动centos7,root账户进行登录. 2.修改网卡配置文件 (1) 打开网卡配置文件 vim /etc/sysconfig/network-scripts/i ...
- ArcGis基础——相接面制造指定距离的分隔带
回家,出发前夜,看完电影吃晚饭回到住处已近十一点,和同事扯了一会儿淡,正准备去睡觉,这哥们儿突然想起一个问题: 如何把相接的面搞出一个20cm的分隔带?因为两区划定项目数据质检要求不同的地块图斑间应有 ...
- Spring学习笔记(10)——方法注入
引用 在大部分情况下,容器中的bean都是singleton类型的.如果一个singleton bean要引用另外一个singleton bean,或者一个非singleton bean要引用另外一个 ...
- shells - 有效登录 shell 的路径名
描述 /etc/shells 是一个文本文件,其中包含有效登录 shell 的路径全名. chsh(1) 需要参考这个文件,并且其他程序也可以查询该文件.有些程序从这个文件判断用户是不是标准用户.比如 ...
- PL/SQL to update all columns
undefine schema_name; declare l_Err ); begin for r in (select atc.table_name, atc.column_name, atc.d ...