传送门:Primes in GCD Table

题意:给定两个数,其中,求为质数的有多少对?其中的范围是

分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0).

  1. #pragma comment(linker,"/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <cmath>
  6. #include <limits.h>
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <queue>
  10. #include <cstdlib>
  11. #include <stack>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #define LL long long
  16. #define mod 100000000
  17. #define inf 0x3f3f3f3f
  18. #define eps 1e-6
  19. #define N 10000000
  20. #define lson l,m,rt<<1
  21. #define rson m+1,r,rt<<1|1
  22. #define PII pair<int,int>
  23. using namespace std;
  24. inline int read()
  25. {
  26. char ch=getchar();int x=,f=;
  27. while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
  28. while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
  29. return x*f;
  30. }
  31. bool vis[N+];
  32. int mu[N+],prime[N+],sum[N+],num[N+];
  33. void Mobius()
  34. {
  35. memset(vis,false,sizeof(vis));
  36. mu[]=;
  37. int tot=;
  38. for(int i=;i<=N;i++)
  39. {
  40. if(!vis[i])
  41. {
  42. prime[tot++]=i;
  43. mu[i]=-;
  44. }
  45. for(int j=;j<tot;j++)
  46. {
  47. if(i*prime[j]>N)break;
  48. vis[i*prime[j]]=true;
  49. if(i%prime[j]==)
  50. {
  51. mu[i*prime[j]]=;
  52. break;
  53. }
  54. else
  55. {
  56. mu[i*prime[j]]=-mu[i];
  57. }
  58. }
  59. }
  60. for(int i=;i<tot;i++)
  61. for(int j=prime[i];j<=N;j+=prime[i])
  62. num[j]+=mu[j/prime[i]];//预处理出对于所有质数p,sigma(f(p))对应的F(i)的系数,用num[i]表示
  63. for(int i=;i<=N;i++)sum[i]=sum[i-]+num[i];
  64. }
  65. LL solve(int n,int m)
  66. {
  67. LL res=;
  68. if(n>m)swap(n,m);
  69. for(int i=,last=;i<=n;i=last+)
  70. {
  71. last=min(n/(n/i),m/(m/i));
  72. res+=(LL)(sum[last]-sum[i-])*(n/i)*(m/i);
  73. }
  74. return res;
  75. }
  76.  
  77. int main()
  78. {
  79. int T,n,m;
  80. Mobius();
  81. T=read();
  82. while(T--)
  83. {
  84. n=read();m=read();
  85. LL ans=solve(n,m);
  86. printf("%lld\n",ans);
  87. }
  88. }

SPOJ PGCD(莫比乌斯反演)的更多相关文章

  1. bzoj 2820 / SPOJ PGCD 莫比乌斯反演

    那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...

  2. SPOJ 7001(莫比乌斯反演)

    传送门:Visible Lattice Points 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : ...

  3. SPOJ - VLATTICE (莫比乌斯反演)

    Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many latt ...

  4. SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)

    4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...

  5. spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演

    SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...

  6. SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)

    题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1    a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...

  7. SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3

    http://www.spoj.com/problems/VLATTICE/ 明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x ...

  8. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  9. SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)

    Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...

随机推荐

  1. android怎样实现自动点击功能

    一个按钮之类的控件的自动点击的话,可以定时调用 button.performClick();

  2. 1030 - Image Is Everything

    Your new company is building a robot that can hold small lightweight objects. The robot will have th ...

  3. android 播放音乐-进度条

    今天学渣研究了一下使用MediaPlayer播放音乐时加入进度条,进度条如今用的是android自带的seekbar,后期会跟换UI的,在之前可以播放音乐的基础上,如今加入的主要功能有两个: 1实时显 ...

  4. 内部框架——axure线框图部件库介绍

    网页框架代码<iframe border=0 name=lantk src="要嵌入的网页地址" width=400 height=400 allowTransparency ...

  5. Storm流计算从入门到精通之技术篇(高并发策略、批处理事务、Trident精解、运维监控、企业场景)

    1.Storm全面.系统.深入讲解,采用最新的稳定版本Storm 0.9.0.1 :   2.注重实践,对较抽象难懂的技术点如Grouping策略.并发度及线程安全.批处理事务.DRPC.Storm ...

  6. SRM 583 Div Level Two:IDNumberVerification

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12610 这道题比较有意思,估计是中国人出的吧,以前都不知道身份 ...

  7. QT---系统托盘图标不显示原因

    很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...

  8. Deep Learning深入研究整理学习笔记五

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  9. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  10. HBASE完全分布式模式的安装

    1集群环境下hadoop.1.1.2已经安装成功 2配置hosts,及环境变量 3编辑hbase-env.xml 4编辑hbase-site.xml 5编辑regionservers文件 6把Hbas ...