题目大意

有不超过\(50000\)个询问,每次询问有多少正整数对\(x\),\(y\),满足\(x\leqslant a\),\(y \leqslant b\),并且\(gcd(x,y)=c\)。其中\(a,b,c\leqslant 50000\)

解题思路

我们发现

\[Ans=f(n)=\sum_{x=1}^{a}\sum_{y = 1}^{b}[gcd( i, j)=c]
\]

当括号内表达式为真时,值为\(1\),否则为\(0\)。

同时,我们设

\[F(n)=\sum_{n|d}f(d)=\lfloor\frac{a}{n}\rfloor\lfloor\frac{b}{n}\rfloor
\]

由莫比乌斯反演,我们得

\[f(d)=\sum_{d|n}\mu(\frac{n}{d})F(n)=\sum_{d|n}\mu(\frac{n}{d})\lfloor\frac{a}{n}\rfloor\lfloor\frac{b}{n}\rfloor=\sum_{i=1}^{min(\lfloor\frac{a}{n}\rfloor,\lfloor\frac{b}{n}\rfloor)}\mu(i)\lfloor\frac{a}{ni}\rfloor\lfloor\frac{b}{ni}\rfloor
\]

到这里,我们通过预处理\(\mu\)的前缀和和整除分块,就可以在\(O(T*\sqrt{n})\)解决。

参考程序

// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define LL long long
using namespace std; const LL Maxn = 50010;
LL Mu[ Maxn ], Sum[ Maxn ];
int Vis[ Maxn ];
LL Num;
LL Prime[ Maxn ]; void Init() {
Mu[ 1 ] = 1;
for( LL i = 2; i <= Maxn; ++i ) {
if( !Vis[ i ] ) Mu[ i ] = -1, Prime[ ++Num ] = i;
for( LL j = 1; j <= Num && Prime[ j ] * i <= Maxn; ++j ) {
Vis[ Prime[ j ] * i ] = 1;
if( i % Prime[ j ] == 0 ) break;
Mu[ i * Prime[ j ] ] = -Mu[ i ];
}
}
for( LL i = 1; i <= Maxn; ++i ) Sum[ i ] = Sum[ i - 1 ] + Mu[ i ];
return;
} void Work() {
LL a, b, c;
scanf( "%lld%lld%lld", &a, &b, &c );
if( a > b ) swap( a, b );
LL Ans = 0;
for( LL x = 1, y; x <= a / c; x = y + 1 ) {
y = min( ( a / c ) / ( ( a / c ) / x ), ( b / c ) / ( ( b / c ) / x ) );
Ans += a / c / x * ( b / c / x ) * ( Sum[ y ] - Sum[ x - 1 ] );
}
printf( "%lld\n", Ans );
return;
} int main() {
Init();
LL t; scanf( "%lld", &t );
for( ; t; --t ) Work();
return 0;
}

ZAP-Queries【luogu3455】的更多相关文章

  1. 【Luogu3455】【POI2007】ZAP-Queries(莫比乌斯反演)

    [Luogu3455][POI2007]ZAP-Queries(莫比乌斯反演) 题面 题目描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x ...

  2. 【BZOJ2301】【HAOI2011】Problem B(莫比乌斯反演)

    [BZOJ2301][HAOI2011]Problem B(莫比乌斯反演) 题面 Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y ...

  3. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  4. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  5. 【CF710F】String Set Queries(二进制分组,AC自动机)

    [CF710F]String Set Queries(二进制分组,AC自动机) 题面 洛谷 CF 翻译: 你有一个字符集合\(D\),初始为空, 有三种操作: 往\(D\)中加入一个串:从\(D\)中 ...

  6. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  7. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

  8. 【题解】Zap(莫比乌斯反演)

    [题解]Zap(莫比乌斯反演) 裸题... 直接化吧 [P3455 POI2007]ZAP-Queries 所有除法默认向下取整 \[ \Sigma_{i=1}^x\Sigma_{j=1}^y[(i, ...

  9. 【Bootstrap】2.作品展示站点

    假设我们已经想好了要给自己的作品弄一个在线站点.一如既往,时间紧迫.我们需要快一点,但作品展示效果又必须专业.当然,站点还得是响应式的,能够在各种设备上正常浏览,因为这是我们向目标客户推销时的卖点.这 ...

随机推荐

  1. the specified service is marked as deletion,can not find the file specified

    使用命令注册windows service sc create CCGSQueueService binpath= "D:\DKX4003\services\xxx.xx.xx\xxx.ex ...

  2. spring boot 学习笔记(三)之 配置

    一:概述 在Spring boot 中根据业务需求,我们往往会在不同地方配置我们所需的key-value 配置项,配置文件存在不同的地方的场景如下: (1) 默认存在 application.prop ...

  3. ZOOKEEPER进阶

    集群个数: 2n+1,因为集群当宕机大于等于二分之一的机子时,集群选举会失败.故 2n+1台机器和3n台机器可靠性相同 Leader的作用: 为了实现各个节点数据的一致性,需要一个负责协调数据同步的操 ...

  4. 15条SQLite3语句

    15条SQLite3语句   转自:http://www.thegeekstuff.com/2012/09/sqlite-command-examples/ SQLite3 is very light ...

  5. pthread 笔记

    1.创建线程 res = pthread_create(&a_thread, NULL, thread_function1, NULL); if (res != 0) { perror(&qu ...

  6. Centos7:Redis3.0集群搭建

    Redis集群中至少应该有三个节点.要保证集群的高可用,需要每个节点有一个备份机.Redis集群至少需要6台服务器. 搭建伪分布式.可以使用一台虚拟机运行6个redis实例. 修改redis的端口号7 ...

  7. 快速写个node命令行工具

    1.package.json-bin配置 [创建bat文件,把bat路径添加到PATH中]这些固定的工作可以由npm帮我们完成.package.json中有个bin字段配置: bin: { " ...

  8. 一点css 基础

    css 行内样式优先度最高 margin 属性 为声明外边距 如图 顺序依次为上右下左

  9. yii2-cache组件第三个参数Dependency $dependency的作用浅析

    用法如下: $cache->set($key, $result, Configs::instance()->cacheDuration, new TagDependency([ 'tags ...

  10. Delphi Android拍照报错

    打开拍照提示以上错误,解决方式