2820: YY的GCD

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 1693  Solved: 901
[Submit][Status][Discuss]

Description

神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种
傻×必然不会了,于是向你来请教……多组输入

Input

第一行一个整数T 表述数据组数接下来T行,每行两个正整数,表示N, M

Output

T行,每行一个整数表示第i组数据的结果

Sample Input

2
10 10
100 100

Sample Output

30
2791

HINT

T = 10000

N, M <= 10000000

Source

/*
参考:http://blog.csdn.net/acdreamers/article/details/8542292
*/
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1e7+;
int T,n,m,tot,mu[N],g[N],sum[N],prime[N/];
bool check[N];
void pre(){
mu[]=;n=1e7+;
for(int i=;i<=n;i++){
if(!check[i]) prime[++tot]=i,mu[i]=-,g[i]=;
for(int j=;j<=tot&&i*prime[j]<=n;j++){
check[i*prime[j]]=;
if(!(i%prime[j])){mu[i*prime[j]]=;g[i*prime[j]]=mu[i];break;}
else mu[i*prime[j]]=-mu[i],g[i*prime[j]]=mu[i]-g[i];
}
}
for(int i=;i<=n;i++) sum[i]=sum[i-]+g[i];
}
ll calc(int n,int m){
if(n>m) swap(n,m);
ll ans=;int pos=;
for(int i=;i<=n;i=pos+){
pos=min(n/(n/i),m/(m/i));
ans+=(ll)(n/i)*(m/i)*(sum[pos]-sum[i-]);
}
return ans;
}
int main(){
pre();
for(scanf("%d",&T);T--;){
scanf("%d%d",&n,&m);
printf("%lld\n",calc(n,m));
}
return ;
}

2820: YY的GCD的更多相关文章

  1. BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  2. 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)

    首先我们来看一道题  BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...

  3. 【刷题】BZOJ 2820 YY的GCD

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种傻×必然 ...

  4. Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)

    2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  5. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  6. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  7. 【BZOJ】2820: YY的GCD(莫比乌斯)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2820 此题非常神! 下文中均默认n<m 首先根据bzoj1101的推理,我们易得对于一个数d使 ...

  8. bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  9. BZOJ 2820 YY的GCD(莫比乌斯函数)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2820 题意:给定n,m.求1<=x<=n, 1<=y<=m且Gc ...

随机推荐

  1. 1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise

    题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B On a broken keyboard, some of ...

  2. 解决Mac安装M2Crypto提示无法找到openssl头文件问题

    大概是这种问题 running build running build_py running build_ext building'M2Crypto.__m2crypto' extension swi ...

  3. POJ 1144 Network(无向图连通分量求割点)

    题目地址:id=1144">POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u ...

  4. Angular 学习笔记——$provider

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. Unix环境部署

    http://www.cnblogs.com/chuyuhuashi/p/4423699.html 分布式javahttp://www.cnblogs.com/tangyanbo/p/4499485. ...

  6. Unhandled event loop exception No more handles 解决办法

    1 http://stackoverflow.com/questions/9074189/unhandled-event-loop-exception-in-plugin-org-eclipse-ui ...

  7. 安装Reshaper后Intellisense失效

    安装Reshaper后Intellisense失效或希望用vs2017的Intellisense功能 安装完毕后,IDE 的智能提示(Intellisense)便会默认使用 Resharper 的提示 ...

  8. nginx限速

    nginx 要实现限速,完整的实现是要限制客户端请求频率,并发连接和传输速度 1.请求限制 Nginx 请求限制的功能来自于 ngx_http_limit_req_module 模块.使用它需要首先在 ...

  9. day14—JQuery编程基础

    JQuery 1.什么是jQuery jQuery是一个优秀的JavaScript框架.一个轻量级的JavaScript类库.jQuery的核心理念是Write less.Do more. 使用jQu ...

  10. linux 登陆key生成

    1.登录A机器 2.ssh-keygen -t rsa,将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub Generating public/priv ...