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(莫比乌斯反演)的更多相关文章

  1. * SPOJ PGCD Primes in GCD Table (需要自己推线性筛函数,好题)

    题目大意: 给定n,m,求有多少组(a,b) 0<a<=n , 0<b<=m , 使得gcd(a,b)= p , p是一个素数 这里本来利用枚举一个个素数,然后利用莫比乌斯反演 ...

  2. 【HDU4947】GCD Array (莫比乌斯反演+树状数组)

    BUPT2017 wintertraining(15) #5H HDU- 4947 题意 有一个长度为l的数组,现在有m个操作,第1种为1 n d v,给下标x 满足gcd(x,n)=d的\(a_x\ ...

  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. 【BZOJ2820】YY的GCD(莫比乌斯反演)

    [BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...

  6. 【BZOJ2818】Gcd(莫比乌斯反演)

    [BZOJ2818]Gcd(莫比乌斯反演) 题面 Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Ou ...

  7. 【HDU1695】GCD(莫比乌斯反演)

    [HDU1695]GCD(莫比乌斯反演) 题面 题目大意 求\(a<=x<=b,c<=y<=d\) 且\(gcd(x,y)=k\)的无序数对的个数 其中,你可以假定\(a=c= ...

  8. spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演

    SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...

  9. hdu1695 GCD(莫比乌斯反演)

    题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...

随机推荐

  1. PHP关于验证

    验证身份证号码 /** * 验证身份证号 * @param $vStr * @return bool */ private function _isCreditNo($vStr) { $vCity = ...

  2. intellijIDEA常用快捷键以及和Eclipse快捷键的对照

    ​CTRL + D 复制当前行到下一行  相当于eclipse中的 CTRL + SHIFT + 下 CTRL + /  单行注释和反注释 CTRL +SHIFT + / 块儿注释和反注释 CTRL ...

  3. 优雅的go语言--入门篇

    1.特点 1.静态类型,编译型的开源语言 2.脚本华的语法,支持多种编程范式(函数式&面向对象) 3.原生,给力的并发编程的支持 2.优势 1.脚本化的语法 2.静态类型+编译型,程序运行速度 ...

  4. jquery插件方式实现table查询功能

    1.写插件部分,如下: ;(function($){ $.fn.plugin = function(options){ var defaults = { //各种属性,各种参数 } var optio ...

  5. mariadb安装配置

    CentOS 7安装MariaDB 详解以及相关配置 第一步:添加 MariaDB yum 仓库 首先在CentOS操作系统中/etc/yum.repos.d/目录下添加 MariaDB 的YUM配置 ...

  6. Visual Studio的“Waiting for a required operation to complete...”问题

    自从使用Visual Studio 2013之后,多次遇到这个恼人的“Waiting for a required operation to complete...”问题. 问题发生于在Visual ...

  7. 《Git权威指南》读书笔记

    这本书一直在拿SVN和CVS 与Git进行对比.对于有过SVN和CVS经验的开发者来讲,这种方法很好,能够通过对比去了解各种的优缺点,从而更快地掌握Git的使用方法,更加欣赏Git.而对于刚刚接触源码 ...

  8. python安装whl文件的注意事项(windows系统)

    首先给大家来一波福利,在没有连接外网(互联网)的情况下,只有公司内网或者断网情况下,需要安装python的一些依赖,不会操作的同学可能就会遇到麻烦.这里教大家离线安装python依赖. 方法:使用.w ...

  9. Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转

    小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...

  10. python修改镜像源

    pip升级:python -m pip install --upgrade pip https://www.cnblogs.com/andy9468/p/10319442.html 1.在命令中临时修 ...