传送门: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);
}
}

最新文章

  1. swift-闭包(代码块)
  2. cURL函数
  3. 第一周:Java基础知识总结(1)
  4. U盘centos7系统安装http://www.augsky.com/599.html
  5. iOS UIButton 图片文字上下垂直布局 解决方案
  6. (整理)SQLServer_DBA 工具
  7. ajax该什么时候用
  8. MHA的几种死法-叶良辰
  9. Objective-C:Foundation框架-常用类-NSMutableString
  10. linq中的GroupBy总结
  11. swizzle method 和消息转发机制的实际使用
  12. 如何为图片添加热点链接?(map + area)
  13. MyBatis(五)select返回list数据
  14. Java 序列化 返序列化
  15. pygame 笔记-1 按键控制方块移动
  16. github与本地电脑关联配置
  17. 扩展中国剩余定理(扩展CRT)详解
  18. 剑指offer四十八之不用加减乘除做加法
  19. 【黑金原创教程】【TimeQuest】【第六章】物理时钟与外部模型
  20. [转]如何在 .Net Framework 4.0 项目上使用 OData?

热门文章

  1. SonicUI在MFC中的使用
  2. 使用tmux [FreeBSDChina Wiki]
  3. Android 计时与倒计时
  4. MSSQL - 根据时间倒序删除第一行数据
  5. Delphi中拖动无边框窗口的5种方法
  6. 译文:前端性能的重要性 The Importance of Frontend Performance
  7. 快速排序原理、复杂度分析及C语言实现
  8. asp.net 检查文件夹和文件是否存在
  9. Element.Event
  10. Android 百度地图 SDK v3.0.0 (三) 加入覆盖Marker与InfoWindow使用