【BZOJ】【2820】YY的GCD
莫比乌斯反演
PoPoQQQ讲义第二题。
暴力枚举每个质数,然后去更新它的倍数即可,那个g[x]看不懂就算了……
为什么去掉了一个memset就不T了→_→……
/**************************************************************
Problem: 2820
User: Tunix
Language: C++
Result: Accepted
Time:4368 ms
Memory:167304 kb
****************************************************************/ //BZOJ 2820
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std; int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>'') {if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<='') {v=v*+ch-''; ch=getchar();}
return v*=sign;
}
/*******************tamplate********************/
const int N=;
typedef long long LL;
int mu[N],prime[N],g[N],sum[N];
bool check[N]; void getmu(){
int tot=;
mu[]=;
for(int i=;i<N;i++){
if (!check[i]){
prime[tot++]=i;
mu[i]=-;
}
rep(j,tot){
if(i*prime[j]>=N)break;
check[i*prime[j]]=;
if(i%prime[j]==){
mu[i*prime[j]]=;
break;
}
else
mu[i*prime[j]]=-mu[i];
}
}
rep(j,tot)
for(int i=prime[j];i<N;i+=prime[j])
sum[i]+=mu[i/prime[j]];//对于每个质数,枚举它的倍数
for(int i=;i<N;i++) sum[i]+=sum[i-];
} int main(){
getmu();
int T=getint();
LL ans;
while(T--){
int n=getint(),m=getint();
if(n>m) swap(n,m);
ans=;
for(int i=,last;i<=n;i=last+){
last=min(n/(n/i),m/(m/i));
ans+=(LL)(n/i)*(m/i)*(sum[last]-sum[i-]);
}
printf("%lld\n",ans);
}
return ;
}
2820: YY的GCD
Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 918 Solved: 467
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
10 10
100 100
Sample Output
2791
HINT
T = 10000
N, M <= 10000000
Source
【BZOJ】【2820】YY的GCD的更多相关文章
- 【莫比乌斯反演】关于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( ...
- [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)有多少对. ...
- BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1624 Solved: 853[Submit][Status][Discu ...
- 【刷题】BZOJ 2820 YY的GCD
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种傻×必然 ...
- Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- BZOJ 2820 YY的GCD(莫比乌斯函数)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2820 题意:给定n,m.求1<=x<=n, 1<=y<=m且Gc ...
- bzoj 2820 YY的GCD(莫比乌斯反演)
Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 kAc这种傻× ...
- ●BZOJ 2820 YY的GCD
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2820 题解: 莫比乌斯反演 先看看这个题:HDU 1695 GCD(本题简化版) HDU 1 ...
随机推荐
- Exchange之证书申请
1. 打开EMC,选择服务器配置,然后右击新建证书 2. 输入一个好记的名称 3. 这里申请通配符证书 4. 填 ...
- Collection 和 Collections的区别。
Collection 和 Collections的区别. Collections是个java.util下的类,它包含有各种有关集合操作的静态方法. Collection是个java.util下的接口, ...
- OC1-XML文件解析
<?xml version = "1.0" encoding="utf-8"?> <Users> <User id="0 ...
- css z-index属性
原文地址:http://www.neoease.com/css-z-index-property-and-layering-tree/ CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有 ...
- js密码的校验(判断字符类型、统计字符类型个数)
/** *判断字符类型 */ function CharMode(iN) { if (iN >= 48 && iN <= 57) //数字 return 1; if (iN ...
- Ant打jar包指定MainClass
一般用ant打jar的时候不用指定程序的入口!这个jar一般是给其他app引用的. 但是如果该jar就是程序的启动jar.例如: java -jar abc.jar 这个时候需要指定jar的入口类! ...
- Nginx在嵌入式系统中的应用
-----------------本文转载自 http://blog.csdn.net/xteda/article/details/39708009 ------------------------- ...
- php下载文件,添加响应头
//下载,添加响应头信息 header('Content-type:application/octet-stream'); header('Content-Disposition:attachment ...
- Uncaught SyntaxError: Unexpected token ILLEGAL【js错误】
应该是逗号的中英文状态错了,应该是英文状态的逗号.还有百度应用后面的逗号.college后面的冒号
- JavaIO和JavaNIO
BIO和NIO BIO在之前的服务器处理模型中,在调用ServerSocket.accept()方法时,会一直阻塞到有客户端连接才会返回,每个客户端连接过来后,服务端都会accept一个新连接,接着启 ...