[POI2007] ZAP-Queries

题目描述

Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He has alreadyfound out that whilst deciphering a message he will have to answer multiple queries of the form"for givenintegers aa, bb and dd, find the number of integer pairs (x,y)(x,y) satisfying the following conditions:

1\le x\le a1≤x≤a,1\le y\le b1≤y≤b,gcd(x,y)=dgcd(x,y)=d, where gcd(x,y)gcd(x,y) is the greatest common divisor of xx and yy".

Byteasar would like to automate his work, so he has asked for your help.

TaskWrite a programme which:

reads from the standard input a list of queries, which the Byteasar has to give answer to, calculates answers to the queries, writes the outcome to the standard output.

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

输入输出格式

输入格式:

The first line of the standard input contains one integer nn (1\le n\le 50\ 0001≤n≤50 000),denoting the number of queries.

The following nn lines contain three integers each: aa, bb and dd(1\le d\le a,b\le 50\ 0001≤d≤a,b≤50 000), separated by single spaces.

Each triplet denotes a single query.

输出格式:

Your programme should write nn lines to the standard output. The ii'th line should contain a single integer: theanswer to the ii'th query from the standard input.

输入输出样例

输入样例#1:

2

4 5 2

6 4 3

输出样例#1:

3

2

Solution

预备知识:莫比乌斯反演,整除分块

不会的看这位dalao的博客莫比乌斯反演

本蒟蒻的整除分块

根据题意

\[ans=\sum_{i=1}^a \sum_{j=1}^b [{gcd(i,j)=d}]
\]

\[ans=\sum_{i=1}^{a/d} \sum_{j=1}^{b/d}[gcd(i,j)=1]
\]

下面就是反演

\[ans=\sum_{i=1}^{a/d} \sum_{j=1}^{b/d} \sum_{p|gcd(i,j)}\mu(p)
\]

但是这样枚举还是\(O(n^2)\),所以我们换一个变量枚举,把最后一个求和提到前面,因为p既是i的因子又是j的因子,所以枚举范围就是\(min(a/d,b/d)\),那么继续推公式

\[ans=\sum_{p=1}^{min(a/d,b/d)}{\mu(p)} \sum_{i=1}^{a/d} \sum_{j=1}^{b/d} \lfloor\frac{a}{p\times d} \rfloor \lfloor\frac{b}{p\times d}\rfloor
\]

如果对于后面的式子不理解,可以这么看,令\(x=a/d,y=b/d\)

\(p\)是\(x,y\)的一个因子,在\(x\)的范围内有\(\lfloor\frac{x}{p}\rfloor\)个\(p\)的倍数,对于\(y\)同理,所以每个因子\(p\)都有\(\lfloor\frac{x}{p}\rfloor\lfloor\frac{y}{p}\rfloor\)的贡献

而对于后面的两个求和我们是可以用前缀和预处理出来的,这个时候是可以做到\(O(n)\)了,但是由于多组数据,所以我们发现,对于一段连续的p,因为a和b的值是确定的,所以\(\lfloor\frac{a}{p\times d}\rfloor\lfloor\frac{b}{p\times d}\rfloor\)的值也是确定的,这中间有许多重复的值,那么我们就可以使用整除分块优化到\(O(\sqrt n)\)

(有错误欢迎指出)

Code

  1. #include<bits/stdc++.h>
  2. #define lol long long
  3. #define il inline
  4. #define rg register
  5. #define Min(a,b) (a)<(b)?(a):(b)
  6. #define Max(a,b) (a)>(b)?(a):(b)
  7. using namespace std;
  8. const int N=5e4+10;
  9. void in(int &ans)
  10. {
  11. ans=0; int f=1; char i=getchar();
  12. while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
  13. while(i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+i-'0', i=getchar();
  14. ans*=f;
  15. }
  16. int n,m,d,tot,ans,T;
  17. int mu[N],sum[N],prime[N];
  18. bool vis[N];
  19. il void get_mu() {
  20. mu[1]=1;
  21. for(int i=2;i<=N-10;i++) {
  22. if(!vis[i]) prime[++tot]=i,mu[i]=-1;
  23. for(int j=1;j<=tot && prime[j]*i<=N-10;j++) {
  24. vis[i*prime[j]]=1;
  25. if(i%prime[j]==0) break;
  26. else mu[i*prime[j]]=-mu[i];
  27. }
  28. }
  29. for(int i=1;i<=N-10;i++) sum[i]=sum[i-1]+mu[i];
  30. }
  31. int main()
  32. {
  33. in(T); get_mu();
  34. while(T--) {
  35. in(n),in(m),in(d); int nn=n/=d,mm=m/=d,ans=0;
  36. for(rg int i=1,pos,p=Min(n,m);i<=p;i=pos+1) {
  37. pos=Min(n/(n/i),m/(m/i));
  38. ans+=(sum[pos]-sum[i-1])*(nn/i)*(mm/i);
  39. }
  40. printf("%d\n",ans);
  41. }
  42. return 0;
  43. }

博主蒟蒻,随意转载.但必须附上原文链接

http://www.cnblogs.com/real-l/

[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. [luogu3455][POI2007]ZAP-Queries【莫比乌斯反演】

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

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

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

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

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

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

  10. 洛谷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. Java应用基础微专业-进阶篇

    第1章--使用对象 1.1 字符类型 char c = 65; // char --> int char c = '\u0041'; // \u: unicode + (Hex 41--> ...

  2. 【JSON类】使用说明

    理解键名路径 键名路径(keyPath)用于定位json的键,比如:{book: {title:”中国人”} },键名路径 book.title 表定位到book下的title键. 对于值是数组类型的 ...

  3. 【rich-text】 富文本组件说明

    [rich-text] 富文本组件可以显示HTML代码样式. 1)支持事件:tap.touchstart.touchmove.touchcancel.touchend和longtap 2)信任的HTM ...

  4. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

  5. 水管工游戏:dfs(递归)

    添柴网这题好想不能评测,所以不确保代码的正确性 题目描述: 这小节有点难,看不太懂可以跳过哦.最近小哼又迷上一个叫做水管工的游戏.游戏的大致规则是这样的.一块矩形土地被分为N * M的单位正方形,现在 ...

  6. 提升方法-AdaBoost

    提升方法通过改变训练样本的权重,学习多个分类器(弱分类器/基分类器)并将这些分类器进行线性组合,提高分类的性能. AdaBoost算法的特点是不改变所给的训练数据,而不断改变训练数据权值的分布,使得训 ...

  7. LINUX监控一:监控命令

    简单的整理一下常用的linux监控命令 本篇参考了:http://www.cnblogs.com/JemBai/archive/2010/07/30/1788484.html的内容 1.top top ...

  8. 业务迁移---redis

    以前也没怎么搞过redis 只知道他是一个nosql数据库很强大,这次迁移用到了~  正好熟练一下并记录过程,还挺繁琐.. 记录一下在学习中的几个问题,总结加深一下印象,有可能会漏掉或者有误差的地方~ ...

  9. 如何做好FAE工作及FAE职位发展

    此文较长,是作者对于半导体FAE职业的一些总结,码字不容易,耐心的阅读,欢迎点赞. 曾经认识一位做电源研发的工程师,转行在一家代理商做FAE,做了一年半以后,就提出了离职请求,他老板问他是什么原因,他 ...

  10. Thunder团队第一次Scrum会议

    Scrum会议1 小组名称:Thunder 项目名称:待定 参会成员: 王航(Master):http://www.cnblogs.com/wangh013/ 李传康:http://www.cnblo ...