题目大意:求 $$\sum\limits_{i=1}a\sum\limits_{j=1}b[gcd(i,j)=c]$$

题解:学会了狄利克雷卷积。

\[\epsilon=\mu \ast 1
\]

将艾弗森表达式转化成卷积的形式,在调换求和顺序,消去不必要的和式。最后利用除法分块+预处理的莫比乌斯函数前缀和在 \(O(\sqrt n)\) 时间内单次回答询问。

代码如下

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int maxn = 5e4 + 10;

int mu[maxn], sum[maxn];
vector<int> primes;
bool vis[maxn]; void RunLinearSieve() {
mu[1] = 1, vis[1] = 1;
int n = 5e4;
for (int i = 2; i <= n; i++) {
if (!vis[i]) {
primes.push_back(i);
mu[i] = -1;
}
for (int j = 0; i * primes[j] <= n; j++) {
vis[i * primes[j]] = 1;
if (i % primes[j] == 0) {
mu[i * primes[j]] = 0;
break;
} else {
mu[i * primes[j]] = -mu[i];
}
}
}
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + mu[i];
}
} int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
RunLinearSieve();
while (T--) {
LL a, b, c;
cin >> a >> b >> c;
a /= c, b /= c;
LL range = min(a, b);
LL ans = 0;
for (int i = 1; i <= range; i++) {
int j = min(a / (a / i), b / (b / i));
ans += (LL)(sum[j] - sum[i - 1]) * (a / i) * (b / i);
i = j;
}
cout << ans << endl;
}
return 0;
}

【洛谷P3455】ZAP-Queries的更多相关文章

  1. 洛谷 [P3455] ZAP

    莫比乌斯函数 #include <iostream> #include <cstdio> #include <cmath> #include <cstring ...

  2. 莫比乌斯反演学习笔记+[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 ...

  3. 【刷题】洛谷 P3455 [POI2007]ZAP-Queries

    题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...

  4. 洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301

    https://www.luogu.org/problemnew/show/P3455 就是https://www.cnblogs.com/hehe54321/p/9315244.html里面的方法2 ...

  5. 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)

    题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...

  6. 洛谷 P3455&BZOJ1101 【[POI2007]ZAP-Queries】

    这应该是入坑莫比乌斯反演的第一道题了吧 其实题目让我们求的东西很简单,就是 \[ ans=\sum_{i=1}^{a}\sum_{j=1}^{b}\left [ gcd(i,j)=k \right ] ...

  7. 洛谷P3455 ZAP-Queries [POI2007] 莫比乌斯反演+数论分块

    正解:莫比乌斯反演 解题报告: 传送门! 首先这题刚看到就很,莫比乌斯反演嘛,和我前面写了题解的那个一模一样的,所以这儿就不讲这前边的做法辣QAQ 但是这样儿还有个问题,就现在已知我每次都是要O(n) ...

  8. 洛谷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 ...

  9. 洛谷P3455 [POI2007]ZAP-Queries

    题目大意: 给定\(n,m,k,\) 求 \[\sum\limits_{x=1}^n\sum\limits_{y=1}^m[gcd(x,y)==k]\] 莫比乌斯反演入门题,先进行一步转化,将每个\( ...

随机推荐

  1. Linux 的相关操作

    切换权限   在linux环境下,用户之前的切换使用 “su - name,若要切换到root下面,则使用sudo su 命令即可. 在linux下安装软件,经常就是装完后不知道装到哪里去了 (201 ...

  2. peewee 事物 回滚

    peewee 事物 回滚 #!/usr/bin/env python # coding=utf-8 from peewee import * db = MySQLDatabase(host='123. ...

  3. python try except else finally

    python  try except else finally 实例 class AError(Exception): """AError---exception&quo ...

  4. centOS 开机自启动自己的脚本

    centOS 开机自启动自己的脚本 1. 自己脚本 myservice 如下: #!/bin/bash # chkconfig: # description: myservice .... echo ...

  5. 在delphi中生成GUID

    什么是 GUID ? 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID. GUID 的格式为 ...

  6. git 提交的步骤

    1. git init //初始化仓库   2. git add .(文件name) //添加文件到本地仓库   3. git commit -m "first commit" / ...

  7. Python——Django-应用的models.py内容

    一.数据的相关配置 #数据库的相关配置 DATABASE = { 'default':{ #连接的数据库类型 'ENGINE':'django.db.backends.sqlite3', #连接数据库 ...

  8. excel 公式 insert 语句

    ="insert into tb_fdn_deviceaccount (zdmc,czmc,sbbh,sbmc,SZCS,SBFLMC,SBLXMC,SBGG,SBYZ,SBJZ,SBXH, ...

  9. AMS工作原理—— App启动概要

    说明: 1. 通过Launcher或者startActivity启动最终的流程都是和上面的一致的. 2. AMP是AMS在App端(client端)的代理, ATP是ApplicationThread ...

  10. 在一般类中通过XmlWebApplicationContext对象获取应用部署上下文Context

    XmlWebApplicationContext xwac = (XmlWebApplicationContext) ContextLoader.getCurrentWebApplicationCon ...