题目链接:https://vjudge.net/problem/SPOJ-PGCD

题目大意:

  给定 \(N\) 和 \(M\),求满足 \((1 \le x \le N), (1 \le y \le M)\),且 \(gcd(x,y)\) 为素数的 \((x,y)\) 的对数。

知识点:  莫比乌斯反演

解题思路:

  设 \(g(p)\) 表示满足 \((1 \le x \le N), (1 \le y \le M)\),且 \(gcd(x,y) = p\) 的 \((x,y)\) 的对数。直接求 \(g(p)\) 是不可行的,其时间复杂度为 \(O(NM)\).

  再设 \(f(p)\) 表示满足 \((1 \le x \le N), (1 \le y \le M)\),且 \(p|gcd(x,y)\) 的 \((x,y)\) 的对数,易知 \(f(p)=(N/p)*(M/p)\)。且不难推出 \(f(n) = \sum \limits_{n|d} g(d)\).    \((1)\)

  莫比乌斯反演公式:若满足 \(F(n) = \sum \limits_{n|d} f(d)\),则有 \(f(n) = \sum \limits_{n|d} \mu(\frac{d}{n})F(d)\).

  将其代入式 \((1)\) 得:

  \(g(n) = \sum \limits_{n|d} \mu(\frac{d}{n})f(d) = \sum \limits_{n|d} \mu(\frac{d}{n})(N/d)(M/d)\)  \((2)\)

  最终的答案为(\(p\) 代表质数):

  \(\sum \limits_{p} g(p) = \sum \limits_{p} \sum \limits_{p|d} \mu(\frac{d}{p})(N/d)(M/d)\)

      \(= \sum \limits_{d}^{min(M,N)} (N/d)(M/d) \sum \limits_{p|d} \mu(\frac{d}{p})\)  \((3)\)

第二个求和函数可以预处理,枚举每一个质数,更新对应的前缀和。

AC代码:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long LL;
  5. const int maxn = 1e7+;
  6. bool check[maxn];
  7. int prime[maxn],Mobius[maxn];
  8. int tot;
  9. int u[maxn];
  10.  
  11. void init(){
  12. Mobius[]=;
  13. tot=;
  14. for(int i=;i<maxn;i++){
  15. if(!check[i]){
  16. prime[tot++]=i;
  17. Mobius[i]=-;
  18. }
  19. for(int j=;j<tot&&i*prime[j]<maxn;j++){
  20. check[i*prime[j]]=true;
  21. if(i%prime[j]==){
  22. Mobius[i*prime[j]]=;
  23. break;
  24. } else
  25. Mobius[i*prime[j]]=-Mobius[i];
  26. }
  27. }
  28. for(int i=;i<tot;i++){
  29. for(int j=prime[i];j<maxn;j+=prime[i]){
  30. u[j]+=Mobius[j/prime[i]]; // u[j] 代表分子为 j(对应式3中的d) 的和函数的值
  31. }
  32. }
  33. for(int i=;i<maxn;i++) u[i]+=u[i-]; //处理出前缀和
  34. }
  35. int main(){
  36. init();
  37. int t,n,m;
  38. scanf("%d",&t);
  39. while(t--){
  40. LL ans=;
  41. int last;
  42. scanf("%d%d",&n,&m);
  43. for(int i=;i<=min(n,m);i=last+){
  44. last=min(n/(n/i),m/(m/i)); // [i,last] 这一段的 u[] 对应 (N/d)(M/d) 相等,不难用实验验证
  45. ans+=(LL)(n/i)*(m/i)*(u[last]-u[i-]);
  46. }
  47. printf("%lld\n",ans);
  48. }
  49. return ;
  50. }

     

  

SPOJ-PGCD Primes in GCD Table的更多相关文章

  1. SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)

    http://www.spoj.com/problems/PGCD/en/ 题意: 给出a,b区间,求该区间内满足gcd(x,y)=质数的个数. 思路: 设f(n)为 gcd(x,y)=p的个数,那么 ...

  2. * SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)

    题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演 ...

  3. 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 ...

  4. SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1&lt;=a&lt;=n,1&lt;=b&lt;=m))加强版

    SPOJ4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the result ...

  5. PGCD2 - Primes in GCD Table (Hard)

    这题肝了三四天,其他啥也没做... 传送门 然后...双倍经验 简单版 不知道为什么会脑抽去帮 LZ_101 大佬验题... 题目和被 A 穿的 PGCD 一样,数据范围变成大概 2e11 ... 于 ...

  6. SPOJ PGCD(莫比乌斯反演)

    传送门:Primes in GCD Table 题意:给定两个数和,其中,,求为质数的有多少对?其中和的范围是. 分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0). ...

  7. Codeforces Round #323 (Div. 2) C.GCD Table

    C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...

  8. Codeforces Round #323 (Div. 1) A. GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #323 (Div. 2) C. GCD Table 暴力

    C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...

随机推荐

  1. 关于bash shell的理解

    Bash Shell 基本特性 1.命令选项参数的补全 补全选项,需要安装 bash-completion yum install -y bash-completion 2.快捷键 Ctrl + a ...

  2. 【Linux网络基础】TCP/IP协议簇的详细介绍(三次握手四次断开,11种状态)

    一.TCP/IP协议簇(DoD参考模型) 用于简化OSI层次,以及相关的标准. 传输控制协议(tcp/ip)簇是相关国防部DoD所创建的,主要用来确保数据的完整性以及在毁灭性战争中维持通信 是由一组不 ...

  3. HR问了一句DB是啥?SQL是啥?DB是Database数据库,SQL是数据库语言! 然后呢? 数据库从入门到精通--入门必看!

    写在前面 本文的写作知识体系来源于我的数据库老师SDAU张承明,部分知识来自于网络,我呢对知识进行了细化和添加了自己的一些看法,并且加入了一些实例帮助理解,本文不是面向SQL高手写的,可以看作是数据库 ...

  4. HDU-1857 畅通工程再续

    畅通工程再续 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定 ...

  5. 聊聊select, poll 和 epoll_wait

    聊聊select, poll 和 epoll 假设项目上需要实现一个TCP的客户端和服务器从而进行跨机器的数据收发,我们很可能翻阅一些资料,然后写出如下的代码. 服务端 客户端 那么问题来了,如果有一 ...

  6. 题目分享R

    题意:有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最短时间和最长时间. 分析:(其实 ...

  7. python 安装模块之pip install +模块名的换源写法

    1.采用国内源,加速下载模块的速度2.常用pip源(上一篇博客介绍过):– 豆瓣:https://pypi.douban.com/simple– 阿里:https://mirrors.aliyun.c ...

  8. python地图投影转换

    一.投影包osr与proj4的使用 1.osr投影转换示例 from osgeo import osr,ogr#定义投影#wgs84source=osr.SpatialReference()sourc ...

  9. JUC之CountDownLatch源码分析

    CountDownLatch是AbstractQueuedSynchronizer中共享锁模式的一个的实现,是一个同步工具类,用来协调多个线程之间的同步.CountDownLatch能够使一个或多个线 ...

  10. 网络通信-在浏览器输入url,基于TCP/IP协议,浏览器渲染的解释

    知识点1: 网络模型 TCP/IP四层 和ISO七层模型 (统一省略后面层字.比如传输代表传输层) 知识点2: 在应用层中TCP建立连接,经历的三次握手协议 首先:,TCP协议是什么? 为什么要三次握 ...