SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)
http://www.spoj.com/problems/PGCD/en/
给出a,b区间,求该区间内满足gcd(x,y)=质数的个数。
思路:
设f(n)为 gcd(x,y)=p的个数,那么F(n)为 p | gcd(x,y)的个数,显然可得F(n)=(x/p)*(y/p)。
这道题目因为可以是不同的质数,所以需要枚举质数,
但是这样枚举太耗时,所以在这里令t=pk,

这样一来的话,我们只需要预处理u(t/p)的前缀和,之后像之前的题一样分块处理就可以了。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e7 + ; int a, b; bool check[maxn];
int prime[maxn];
int mu[maxn];
ll sum[maxn]; void Mobius()
{
memset(check, false, sizeof(check));
mu[] = ;
int tot = ;
for (int i = ; i <= maxn; i++)
{
if (!check[i])
{
prime[tot++] = i;
mu[i] = -;
}
for (int j = ; j < tot; j++)
{
if (i * prime[j] > maxn)
{
break;
}
check[i * prime[j]] = true;
if (i % prime[j] == )
{
mu[i * prime[j]] = ;
break;
}
else
{
mu[i * prime[j]] = -mu[i];
}
}
} sum[]=;
for(int i=;i<tot;i++)
{
for(int j=prime[i];j<maxn;j+=prime[i])
{
sum[j]+=mu[j/prime[i]];
}
}
for(int i=;i<maxn;i++)
sum[i]+=sum[i-];
return ;
} ll solve(int n, int m)
{
if(n>m) swap(n,m);
ll ans=; for(int i=,last=;i<=n;i=last+)
{
last=min(n/(n/i),m/(m/i));
ans+=(sum[last]-sum[i-])*(n/i)*(m/i);
}
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
Mobius(); scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
ll ans = solve(a,b);
printf("%lld\n",ans);
}
return ;
}
SPOJ - PGCD Primes in GCD Table(莫比乌斯反演)的更多相关文章
- * SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)
题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演 ...
- 【HDU4947】GCD Array (莫比乌斯反演+树状数组)
BUPT2017 wintertraining(15) #5H HDU- 4947 题意 有一个长度为l的数组,现在有m个操作,第1种为1 n d v,给下标x 满足gcd(x,n)=d的\(a_x\ ...
- 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 ...
- 【BZOJ2820】YY的GCD(莫比乌斯反演)
[BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...
- 【BZOJ2818】Gcd(莫比乌斯反演)
[BZOJ2818]Gcd(莫比乌斯反演) 题面 Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Ou ...
- 【HDU1695】GCD(莫比乌斯反演)
[HDU1695]GCD(莫比乌斯反演) 题面 题目大意 求\(a<=x<=b,c<=y<=d\) 且\(gcd(x,y)=k\)的无序数对的个数 其中,你可以假定\(a=c= ...
- spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演
SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...
- hdu1695 GCD(莫比乌斯反演)
题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...
随机推荐
- Listview Section 多个标题以及内容
其中日期标题部分视图布局: 带图片的条目布局部分: 问题在于,如何在ListView中既有标题条目又有内容条目. 这里用到了设计模式中的Iterator模式.在java代码中示例有Iterator,可 ...
- 无法远程访问Mysql
1.故障状态 [root@server02 ~]# mysql -utuser -h192. -p Enter password: ERROR (HY000): Can't connect to My ...
- EditPlus轻量级编辑器配置常用的语法规则!
打开EditPlus编辑器:工具 ---- 参数设置 ---- 文件 ---- 设置&语法: 先配置简单的CSS语法: 勾选下面的 “自动完成” ,加载对应的ACP文件,配置一些常用的语法: ...
- pandas处理日期时间,按照时间筛选
pandas有着强大的日期数据处理功能,本期我们来了解下pandas处理日期数据的一些基本功能,主要包括以下三个方面: 按日期筛选数据 按日期显示数据 按日期统计数据 运行环境为 windows系统, ...
- 浙江工业大学校赛 画图游戏 BugZhu抽抽抽!!
BugZhu抽抽抽!! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- htop 分析 进程对资源的消耗
[root@d ~]# htop -hhtop 2.2.0 - (C) 2004-2018 Hisham MuhammadReleased under the GNU GPL. -C --no-col ...
- 如何打造高性能Web应用
Sean Hull是Heavyweight Internet Group的创始人兼高级顾问,拥有20年以上技术顾问相关经验,曾为多家知名机构提供咨询,其中包括The Hollywood Reporte ...
- 【剑指Offer】俯视50题之1-10题
面试题1赋值运算符函数 面试题2 实现Singleton模式 面试题3 二维数组中的查找 面试题4 替换空格 面试题5 从头到尾打印链表 面试题6 重建二叉树 面试题7 用两个栈实 ...
- centos LAMP第四部分mysql操作 忘记root密码 skip-innodb 配置慢查询日志 mysql常用操作 mysql常用操作 mysql备份与恢复 第二十二节课
centos LAMP第四部分mysql操作 忘记root密码 skip-innodb 配置慢查询日志 mysql常用操作 mysql常用操作 mysql备份与恢复 第二十二节课 mysq ...
- C++ vector错误(1)
在用C++的vector的时候,要保证访问的下标不能超过vector的size.否则出现msvcp60.dll 访问禁止.