题目链接: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代码:

 #include <bits/stdc++.h>

 using namespace std;
typedef long long LL;
const int maxn = 1e7+;
bool check[maxn];
int prime[maxn],Mobius[maxn];
int tot;
int u[maxn]; void init(){
Mobius[]=;
tot=;
for(int i=;i<maxn;i++){
if(!check[i]){
prime[tot++]=i;
Mobius[i]=-;
}
for(int j=;j<tot&&i*prime[j]<maxn;j++){
check[i*prime[j]]=true;
if(i%prime[j]==){
Mobius[i*prime[j]]=;
break;
} else
Mobius[i*prime[j]]=-Mobius[i];
}
}
for(int i=;i<tot;i++){
for(int j=prime[i];j<maxn;j+=prime[i]){
u[j]+=Mobius[j/prime[i]]; // u[j] 代表分子为 j(对应式3中的d) 的和函数的值
}
}
for(int i=;i<maxn;i++) u[i]+=u[i-]; //处理出前缀和
}
int main(){
init();
int t,n,m;
scanf("%d",&t);
while(t--){
LL ans=;
int last;
scanf("%d%d",&n,&m);
for(int i=;i<=min(n,m);i=last+){
last=min(n/(n/i),m/(m/i)); // [i,last] 这一段的 u[] 对应 (N/d)(M/d) 相等,不难用实验验证
ans+=(LL)(n/i)*(m/i)*(u[last]-u[i-]);
}
printf("%lld\n",ans);
}
return ;
}

     

  

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. office 365 激活

    将以下代码复制到记事本 @echo off title Activate Microsoft Office ALL versions &echo - Microsoft Office Prof ...

  2. web 之 tomcat 8.5 和9.0如何进入manager?

    tomcat 8.5 和9.0如何进入manager? 第一步找到tomcat-user.xml文件 第二步添加如下代码 <role rolename="manager-gui&quo ...

  3. CentOS 7 网络优化(升级内核、开启 BBR)

    我之前介绍过关于 TCP 一些优化,包括安装使用 TCP 优化软件,这些适用于较低版本的 CentOS 系统,例如 CentOS 6,详细可参考<Linux 下的一些简单的 TCP 优化> ...

  4. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  6. 《C程序设计语言》 练习1-21

    问题描述 编写程序entab,将空格串替换为最少数量的制表符和空格,但要保持单词之间的间隔不变.假设制表符终止位的位置与练习1 - 20的detab程序的情况相同.当使用一个制表符或者一个空格都可以到 ...

  7. prufer编码学习笔记

    prufer 编码 对于一个无根树,他的 prufer 编码是这样确定的: 每次找到编号最小的一个叶子节点,也就是度数为\(1\)的节点,把和它相连的点,加入 prufer 编码序列的末尾,然后把这个 ...

  8. 一只简单的网络爬虫(基于linux C/C++)————线程相关

    爬虫里面采用了多线程的方式处理多个任务,以便支持并发的处理,把主函数那边算一个线程的话,加上一个DNS解析的线程,以及我们可以设置的max_job_num值,最多使用了1+1+max_job_num个 ...

  9. 04 全局局部配置 wxml数据绑定 事件 冒泡

    一. 配置介绍 一个小程序应用程序会包括最基本的两种配置文件.一种是全局的 app.json 和 页面自己的 page.json(index.json /test.json等) 注意:配置文件中不能出 ...

  10. 蓝色展开收缩悬浮QQ客服代码

    放在我的博客首页上的的预览图: 在文章区的预览图如下: 代码如下: <div class="scrollsidebar" id="scrollsidebar&quo ...