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*/ 线性筛求莫比乌 ...
随机推荐
- node中的对象
1. class的概念 定义一个class,属性都是private,方法都是public. Hello.js: 使用class index.js: 2. 单例类 使用exports而不是module. ...
- 【BZOJ4282】慎二的随机数列 乱搞
[BZOJ4282]慎二的随机数列 Description 间桐慎二是间桐家著名的废柴,有一天,他在学校随机了一组随机数列, 准备使用他那强大的人工智能求出其最长上升子序列,但是天有不测风云,人有旦夕 ...
- ios 添加朦层
@interface RootViewController : UIViewController { UIView *view; } -(void)createBackgroundView { vie ...
- netty的解码器与粘包和拆包
tcp是一个“流”的协议,一个完整的包可能会被TCP拆分成多个包进行发送,也可能把小的封装成一个大的数据包发送,这就是所谓的TCP粘包和拆包问题. 假设客户端分别发送数据包D1和D2给服务端,由于服务 ...
- Comparable 与 Comparator的区别
Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序 ...
- EasyUI +MVC +EF实现增删改查
OA项目的框架已经搭建好了,接下来就是在这个框架下完成相应的业务的编码,接下来实现UserInfo页面的增删改查. 1.首先先谈一下遇到的一个框架上的问题:提示EF版本不一致之类的问题,主要是解决方案 ...
- FZU 1064 教授的测试(卡特兰数,递归)
Problem 1064 教授的测试 Accept: 149 Submit: 364 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Des ...
- proxy,https,git,tortoise git,ssh-agent,ssh-add,ssh,ssl,rsync
看具体应用了,一般的文件复制使用scp,增量同步使用rsync.rsync的认证可以使用ssh,还可以是rsync自己的密码文件. ssh-keygen -l 察看 fineprint 5.1 通过p ...
- 不登录到MySQL执行SQL语句
mysql -e 不登录到MySQL执行SQL语句 mysql -u root -p -e "SHOW DATABASES"
- Linux下编译安装MySQL5.6
[准备工作] 所有操作需要在root用户下 本机测试案例系统信息:centos7.3 安装路径:/usr/local/mysql [安装MySQL] 先安装如下依赖包: $ yum -y instal ...