传送门: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. boost::property_tree读取解析.xml文件

    boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径  boost::property_tree::wptree wpt;    std::locale:: ...

  2. VC 对话框背景颜色、控件颜色

    系统环境:Windows 7软件环境:Visual C++ 2008 SP1本次目的:为对话框设置背景颜色.控件颜色 既然MFC对话框不好开发,那么现在我们来开始美化我们的对话框.为对话框设置背景颜色 ...

  3. windows live writer插件说明文档(附录网盘地址)

    百度云地址:http://pan.baidu.com/s/1hqnjzjY 1.Screen Capture tool 用于直接在WLWriter中进行截图的一个插件,要配合SnagIt 这个软件使用 ...

  4. perl eval

    eval 表达式: eval 块: eval 在第一种形式,通常称为一个字符串eval EXPR 返回值是被解析的和被执行的作为一个小小的Perl程序. 表达式的值(是它本身决定的在标量上下文环境)是 ...

  5. <2>集腋成裘

    标量项: [root@wx03 2]# cat a1.pl unshift(@INC,"/root/big/2"); use Horse;; print $Horse::days; ...

  6. 一些实用的mysql语句(不断积累更新)

    1.数据表里仅仅有生日字段,想计算出其年龄的mysql语句: SELECT *,DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW( ))-TO_DAYS(生日字段)),'%Y')+0 ...

  7. Android学习笔记(九)——更复杂的进度对话框

    显示操作进度的对话框 1.使用上一篇创建的同一项目.在activity_main.xml文件里加入一个Button: <Button android:id="@+id/btn_dial ...

  8. CSDN改版问题多多

    刚刚上CSDN,发现改版了,推出C币功能. 然后看了2分钟,发现了一个Bug,于是准备提交到论坛.但是--居然提交Bug的论坛也出现Bug.印象中,每次CSDN更新版本号Bug都非常多,这,作为程序猿 ...

  9. C++学习之路—const用法总结

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) C++为什么要引入const?它允许你指定一个语 ...

  10. Mac 终端提示You have not agreed to the Xcode license agreements

    在mac 终端运行命令的时候会被提示没有同意xcode 证书 ,这个时候需要在Terminal中同意license 打开终端输入xcodebuild -license,会看到一大堆的license说明 ...