传送门:Primes in GCD Table

题意:给定两个数,其中,求为质数的有多少对?其中的范围是

分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0).

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 10000000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
bool vis[N+];
int mu[N+],prime[N+],sum[N+],num[N+];
void Mobius()
{
memset(vis,false,sizeof(vis));
mu[]=;
int tot=;
for(int i=;i<=N;i++)
{
if(!vis[i])
{
prime[tot++]=i;
mu[i]=-;
}
for(int j=;j<tot;j++)
{
if(i*prime[j]>N)break;
vis[i*prime[j]]=true;
if(i%prime[j]==)
{
mu[i*prime[j]]=;
break;
}
else
{
mu[i*prime[j]]=-mu[i];
}
}
}
for(int i=;i<tot;i++)
for(int j=prime[i];j<=N;j+=prime[i])
num[j]+=mu[j/prime[i]];//预处理出对于所有质数p,sigma(f(p))对应的F(i)的系数,用num[i]表示
for(int i=;i<=N;i++)sum[i]=sum[i-]+num[i];
}
LL solve(int n,int m)
{
LL res=;
if(n>m)swap(n,m);
for(int i=,last=;i<=n;i=last+)
{
last=min(n/(n/i),m/(m/i));
res+=(LL)(sum[last]-sum[i-])*(n/i)*(m/i);
}
return res;
} int main()
{
int T,n,m;
Mobius();
T=read();
while(T--)
{
n=read();m=read();
LL ans=solve(n,m);
printf("%lld\n",ans);
}
}

SPOJ PGCD(莫比乌斯反演)的更多相关文章

  1. bzoj 2820 / SPOJ PGCD 莫比乌斯反演

    那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...

  2. SPOJ 7001(莫比乌斯反演)

    传送门:Visible Lattice Points 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : ...

  3. SPOJ - VLATTICE (莫比乌斯反演)

    Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many latt ...

  4. 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 ...

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

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

  6. SPOJ 7001 VLATTICE - Visible Lattice Points(莫比乌斯反演)

    题目链接:http://www.spoj.com/problems/VLATTICE/ 题意:求gcd(a, b, c) = 1    a,b,c <=N 的对数. 思路:我们令函数g(x)为g ...

  7. SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3

    http://www.spoj.com/problems/VLATTICE/ 明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x ...

  8. 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

    [BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...

  9. SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)

    Visible Lattice Points Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at ...

随机推荐

  1. 基于visual Studio2013解决面试题之1503最大公约数最小公倍数

     题目

  2. 继承Application实现Android数据共享

         Application类 在Android中,启动一个应用,首先会初始化Application,然后再通过它检查AndroidManifest.xml清单文件,选择须要首先启动的Activi ...

  3. CSS未知div高度垂直居中代码_层和布局特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. COM实现过程

    前言 COM已经成为一个必需的东西了.在我们周围,可以说处处充满了COM – 如果你是在使用WINDOWS,并在其下面编写程序的话.然而,无论你是用VC,还是使用DELPHI进行COM编程时,在大多数 ...

  5. Spring boot jar 后台运行

    start(){ now=`date "+%Y%m%d%H%M%S"` exec java -Xms256m -Xmx512m -jar /alidata/server/webap ...

  6. perl 匿名函数传参

    $subref=sub { my $a=shift; return $a; }; print $subref->("xxyyzz");

  7. android调用音乐播放器,三种方

    小弟想请问一下.怎样在自己写的程序中调用系统的音乐播放器呢. 我在google上搜索了.主要是有两种方法,可是都不是我想要的. 第一种是.使用mp3音乐文件的uri.和intent,进行调用.可是这样 ...

  8. <转载>如何解决子级用float浮动父级div高度不能自适应的问题

    转载:http://www.kwstu.com/ArticleView/divcss_2013101582430202 解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内 ...

  9. 【翻译】ASP.NET Web API是什么?

    原文 [翻译]ASP.NET Web API是什么? 说明:随微软ASP.NET MVC 4一起发布的还有一个框架,叫做ASP.NET Web API.目前国内关注这项技术的人似乎还很少,这方面的文章 ...

  10. 14.9.4 COMPACT and REDUNDANT Row Formats

    14.9.4 COMPACT and REDUNDANT Row Formats InnoDB 早期的版本 使用一种未命名的文件格式(现在称为Antelope(羚羊)) 对于数据库文件 在这种文件格式 ...