题目描述

FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。

分析

很明显的一道莫比乌斯反演,但是还没有写过学习笔记,之后一定补起来(flag)。

\[f(k)=\sum^a_{i=1}\sum^b_{j=1}[gcd(i,j)=k]\]
\[F(k) = \sum_{n|k}f(k)= \lfloor \frac{a}{n} \rfloor \lfloor \frac{b}{n} \rfloor \]
由反演退出以下的式子:
\[f(n) = \sum_{n|k} \mu (\lfloor \frac{k}{n}\rfloor) F(k)\]
那么答案就是\(f(d)\),
我们枚举整除分块$\lfloor \frac k d \rfloor $
递推式就是:
\[ans=\sum^{min(a,b)}_{t=1} \mu(t) \lfloor \frac{a}{td} \rfloor \lfloor \frac{b}{td} \rfloor \]
多组数据我们就用整除分块,差不多复杂度是\(O(t\sqrt{n})\)
ps.记得要开long long。

ac代码

  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ms(a, b) memset(a, b, sizeof(a))
  4. #define inf 0x3f3f3f3f
  5. using namespace std;
  6. template <typename T>
  7. inline void read(T &x) {
  8. x = 0; T fl = 1;
  9. char ch = 0;
  10. while (ch < '0' || ch > '9') {
  11. if (ch == '-') fl = -1;
  12. ch = getchar();
  13. }
  14. while (ch >= '0' && ch <= '9') {
  15. x = (x << 1) + (x << 3) + (ch ^ 48);
  16. ch = getchar();
  17. }
  18. x *= fl;
  19. }
  20. #define N 500005
  21. ll sum[N], mui[N], prime[N];
  22. int cnt;
  23. bool vis[N];
  24. void get_mui(ll MAXN) {
  25. mui[1] = 1;
  26. for (ll i = 2; i <= MAXN; i ++) {
  27. if (!vis[i]) {
  28. mui[i] = -1;
  29. prime[++ cnt] = i;
  30. }
  31. for (ll j = 1; j <= cnt && prime[j] * i <= MAXN; j ++) {
  32. vis[prime[j] * i] = 1;
  33. if (i % prime[j] == 0) break;
  34. else mui[prime[j] * i] = -mui[i];
  35. }
  36. }
  37. for (ll i = 1; i <= MAXN; i ++) sum[i] = sum[i - 1] + mui[i];
  38. }
  39. int main() {
  40. int cas;
  41. read(cas);
  42. get_mui(500000);
  43. while (cas --) {
  44. ll a, b, d;
  45. read(a); read(b); read(d);
  46. ll ans = 0;
  47. for (ll l = 1, r; l <= min(a, b); l = r + 1) {
  48. r = min(a / (a / l), b / (b / l));
  49. ans += (a / (l * d) * (b / (l * d))) * (sum[r] - sum[l - 1]);
  50. }
  51. printf("%lld\n", ans);
  52. }
  53. return 0;
  54. }

[luogu3455][POI2007]ZAP-Queries【莫比乌斯反演】的更多相关文章

  1. 【BZOJ】1101 [POI2007]Zap(莫比乌斯反演)

    题目 传送门:QWQ 分析 莫比乌斯反演. 还不是很熟练qwq 代码 //bzoj1101 //给出a,b,d,询问有多少对二元组(x,y)满足gcd(x,y)=d.x<=a,y<=b # ...

  2. BZOJ1101 POI2007 Zap 【莫比乌斯反演】

    BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...

  3. 【BZOJ1101】[POI2007] Zap(莫比乌斯反演)

    点此看题面 大致题意: 求\(\sum_{x=1}^N\sum_{y=1}^M[gcd(x,y)==d]\). 一道类似的题目 推荐先去做一下这道题:[洛谷2257]YY的GCD,来初步了解一下莫比乌 ...

  4. BZOJ 1101 [POI2007]Zap(莫比乌斯反演)

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

  5. ☆ [POI2007] ZAP-Queries 「莫比乌斯反演」

    题目类型:莫比乌斯反演 传送门:>Here< 题意:求有多少对正整数对\((a,b)\),满足\(0<a<A\),\(0<b<B\),\(gcd(a,b)=d\) ...

  6. 洛谷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}\ ...

  7. 【BZOJ】1101: [POI2007]Zap(莫比乌斯+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1101 无限膜拜数论和分块orz 首先莫比乌斯函数的一些性质可以看<初等数论>或<具 ...

  8. [POI2007]ZAP-Queries (莫比乌斯反演+整除分块)

    [POI2007]ZAP-Queries \(solution:\) 唉,数论实在有点烂了,昨天还会的,今天就不会了,周末刚证明的,今天全忘了,还不如早点写好题解. 这题首先我们可以列出来答案就是: ...

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

随机推荐

  1. Session帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. [JDBC]ORA-01000: 超出打开游标的最大数(ORA-01000: maximum open cursors exceeded)

    问题产生的原因: Java代码在执行conn.createStatement()和conn.prepareStatement()的时候,相当于在数据库中打开了一个cursor.由于oracle对打开的 ...

  3. Slurm任务调度系统部署和测试(源码)(1)

    1. 概述1.1 节点信息2. 节点准备3. 部署NTP服务器4. 部署LDAP服务器5. 部署Munge认证服务6. 部署Mysql数据库服务7. 部署slurm7.1 创建slurm用户7.2 挂 ...

  4. centos7下/etc/rc.local文件里配置的开机启动项不执行的解决办法

    习惯于在/etc/rc.local文件里配置我们需要开机启动的服务,这个在centos6系统下是正常生效的.但是到了centos7系统下,发现/etc/rc.local文件里的开机启动项不执行了!仔细 ...

  5. personal project

    words count program 统计文本文件的字符数,单词数和行数. 实现一个统计程序,他能正确的统计程序文件中的字符数,单词数和行数. 源码链接 https://github.com/sup ...

  6. 单工程搭建springmvc+spring+mybatis(maven,idea)

    单工程搭建springmvc+spring+mybatis(maven,idea) 1.pom.xml <properties> <project.build.sourceEncod ...

  7. PAT 1076 Wifi密码

    https://pintia.cn/problem-sets/994805260223102976/problems/994805262622244864 下面是微博上流传的一张照片:“各位亲爱的同学 ...

  8. Linux下运行Shell脚本或者可执行文件Executable方法

    绝对路径 /xxx/xxx/something.sh /xxx/xxx/executable 相对路径 ./something.sh ./executable 注意:前边得加./,可不是像window ...

  9. Eclipse:An internal error occurred during: "Building workspace". GC overhead limit exceeded

    http://blog.csdn.net/shaozhang872196/article/details/18552273 http://www.cnblogs.com/sonofelice/p/52 ...

  10. JQuery插件:图片上传本地预览插件,改进案例一则。

    /* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插 ...