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*/ 线性筛求莫比乌 ...
随机推荐
- salt-stack更换主机名
author:headsen chen date: 2018-09-30 11:22:40 1,建立master端和client端的正常连接 #master yum -y install epel ...
- Windows 2008 server R2安装.NET Framework4时提示“灾难性故障”
报错信息: 安装.NET Framework 4时,提示安装未成功,“灾难性故障”.服务器的操作系统是windows server 2008 R2. 查看系统日志时显示“无法安装 Windows 更 ...
- ios 图片拉伸不变形的方法
如果一个椭圆图片,原图大小为30*30,而我们让它显示100*30,那么这个图片就会被拉伸,而且效果很难看.用下边的方法可以创建一个局部不被拉伸的图片. UIImage * buttonBg = [[ ...
- Shell脚步之监控iostat数据
在性能测试中,进行iostat监控数据显示太不美观,看起来很痛苦,如下图 为了显示美观,写个shell脚本进行改造,如下: #! /bin/bash interval= ]; then interva ...
- jvm原理之内存机制
转自:https://www.cnblogs.com/dreamowneryong/p/6381633.html JVM栈由堆.方法区,栈.本地方法栈.程序计数器等部分组成,结构图如下所示: 还有一张 ...
- Gitlab备份和恢复操作
参考:https://www.cnblogs.com/kevingrace/p/7821529.html 一,设置开启备份以及备份路径 /etc/gitlab/gitlab.rb gitlab_rai ...
- WebSocket学习记录
参考资料: Java后端WebSocket的Tomcat实现 基于Java的WebSocket推送 java WebSocket的实现以及Spring WebSocket 利用spring-webso ...
- Why do some system users have /usr/bin/false as their shell? What's the difference between /sbin/nologin and /bin/false
https://www.quora.com/How-can-bin-true-and-bin-false-Linux-utilities-be-used MySQL :: MySQL 8.0 Refe ...
- django的cache
使用文件缓存 #settings.py CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.F ...
- 《图解HTTP》书摘
图解HTTP 上野宣.于均良 1.3 网络基础 TCP/IP 2016-03-03 相互通信,双方就必须基于相同的方法.比如,如何探测到通信目标.由哪一边先发起通信.使用哪种语言进行通信.怎样结束通信 ...