SPOJ-PGCD Primes in GCD Table
题目链接: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的更多相关文章
- SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)
http://www.spoj.com/problems/PGCD/en/ 题意: 给出a,b区间,求该区间内满足gcd(x,y)=质数的个数. 思路: 设f(n)为 gcd(x,y)=p的个数,那么 ...
- * SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)
题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演 ...
- 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 ...
- SPOJ4491. Primes in GCD Table(gcd(a,b)=d素数,(1<=a<=n,1<=b<=m))加强版
SPOJ4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the result ...
- PGCD2 - Primes in GCD Table (Hard)
这题肝了三四天,其他啥也没做... 传送门 然后...双倍经验 简单版 不知道为什么会脑抽去帮 LZ_101 大佬验题... 题目和被 A 穿的 PGCD 一样,数据范围变成大概 2e11 ... 于 ...
- SPOJ PGCD(莫比乌斯反演)
传送门:Primes in GCD Table 题意:给定两个数和,其中,,求为质数的有多少对?其中和的范围是. 分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0). ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 曹工力荐:调试 jdk 中 rt.jar 包部分的源码(可自由增加注释,修改代码并debug)
背景 大家知道,jdk安装的目录下,一般会有个src.zip包,这个包基本对应了rt.jar这个包.rt.jar这个包里面,就放了jdk中,jdk采用java实现的那部分类库代码,比如java.lan ...
- SQLCommand 相关应用
SQL command 对象:执行指定数据库中相应的操作 (执行相应的SQL语句)string str1 = "select * from tb_student "Create: ...
- 如何设计scalable 的系统 (转载)
Design a Scalable System Design a system that scales to millions of users (AWS based) Step 1: Outlin ...
- RMI原理揭秘之远程对象
讨论开始之前,我们先看看网上的一个例子,这个例子我腾抄了一分,没有用链接的方式,只是为了让大家看得方便,如有侵权,我立马***. 定义远程接口: 1 2 3 4 5 6 package com.guo ...
- Java自动装箱与缓存
自动装箱与缓存 现象 有以下代码: 1 public class Main { 2 public static void main(String[] args) { 3 Integer i1 = 12 ...
- python的unittest框架中的assert断言
unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言
- 【Hexo】使用Hexo+github pages+travis ci 实现自动化部署
目录 一.说明 二.成品展示 三.前期准备 本地安装 node.js 本地安装 git github 账号 创建仓库 travis ci 账号 四.安装 Hexo 五.使用 hexo 搭建博客 六.部 ...
- P3983 赛斯石(双背包)
这题不算难的,但是脑子真的特别乱.....传送门 \(Ⅰ.物品可以拆开来但船不能拆开来,所以1-10载重船的最大收益完全可以用背包求出来.\) \(Ⅱ.最后一定是选一些船走,而船的收益已经固定.所以用 ...
- 剑指offer--(根据前序遍历和中序遍历)重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- FOC 算法基础之欧拉公式
文章目录 欧拉公式 几何意义 复数平面 动态过程 加法 FOC电压矢量的推导 总结 参考 FOC中电压矢量合成的推导,对于欧拉公式的几何意义做了一个全面的回顾. 欧拉公式 欧拉是一个天才,欧拉公式甚至 ...