BZOJ 1101 Zap(莫比乌斯反演)
http://www.lydsy.com/JudgeOnline/problem.php?id=1101
给定a,b,d,求有多少gcd(x,y)==d(1<=x<=a&&1<=y<=b)
思路:
Σgcd(x,y)==d (1<=x<=a,1<=y<=b)
=
Σgcd(x,y)==1 (1<=x<=a/d,1<=y<=b/d)
令G(i)=num(i|gcd(x,y))=n/i*m/i
g(i)=num(i=gcd(x,y))
g(i)=ΣG(d)*u(d/i) (i|d)
则答案就是g(1)
g(1)=ΣG(i)*u(i) (1<=i<=min(n,m))
=Σ(n/i)*(m/i)*u(i)
因此做出u(i)的前缀和,这样就可以一起处理n/i和m/i相同的i
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
int mul[],mark[],sum[],p[];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void init(){
mul[]=;
for (int i=;i<=;i++){
if (!mark[i]){
p[++p[]]=i;
mul[i]=-;
}
for (int j=;j<=p[]&&i*p[j]<=;j++){
mark[p[j]*i]=;
if (i%p[j]) mul[p[j]*i]=mul[i]*(-);
else {
mul[p[j]*i]=;
break;
}
}
}
sum[]=;
for (int i=;i<=;i++)
sum[i]=sum[i-]+mul[i];
}
int cal(int a,int b){
if (a>b) std::swap(a,b);
int res=,n=a,m=b;
for (int i=,j;i<=a;i=j+){
j=std::min(n/(n/i),m/(m/i));
res+=(a/i)*(b/i)*(sum[j]-sum[i-]);
}
return res;
}
int main(){
int Q=read();
init();
while (Q--){
int a=read(),b=read(),d=read();
printf("%d\n",cal(a/d,b/d));
}
}
BZOJ 1101 Zap(莫比乌斯反演)的更多相关文章
- bzoj 1101 Zap —— 莫比乌斯反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 直接莫比乌斯反演. 代码如下: #include<cstdio> #inc ...
- bzoj 1101 zap 莫比乌斯
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给 ...
- 【题解】Zap(莫比乌斯反演)
[题解]Zap(莫比乌斯反演) 裸题... 直接化吧 [P3455 POI2007]ZAP-Queries 所有除法默认向下取整 \[ \Sigma_{i=1}^x\Sigma_{j=1}^y[(i, ...
- bzoj [SDOI2014]数表 莫比乌斯反演 BIT
bzoj [SDOI2014]数表 莫比乌斯反演 BIT 链接 bzoj luogu loj 思路 \[ \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}a*[f[ ...
- BZOJ 1101 Luogu P3455 POI 2007 Zap (莫比乌斯反演+数论分块)
手动博客搬家: 本文发表于20171216 13:34:20, 原地址https://blog.csdn.net/suncongbo/article/details/78819470 URL: (Lu ...
- 1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...
- BZOJ1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2951 Solved: 1293[Submit][Status ...
- Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...
- 【BZOJ1101】Zap [莫比乌斯反演]
Zap Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 对于给定的整数a,b和d,有多少正整 ...
随机推荐
- Oracle多行记录合并处理
1:效果如下图所示: 表T1: CREATE TABLE T1 ( WEEKWORKID VARCHAR2(20) , DD VARCHAR2(20) ) 表T2 CREATE TABLE T2 ( ...
- hdu 4302 Holedox Eating
http://acm.hdu.edu.cn/showproblem.php?pid=4302 #include <cstdio> #include <cstring> #inc ...
- Codeforces 414B Mashmokh and ACM
http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...
- LeetCode_Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Windows 1252和ISO 8859-1之间的区别(ISO 8859-1就是Latin-1,但1252与Latin1略有不同)
2.6.5. ANSI字符编码和Windows 1252 Windows为了支持英语和西欧字符,自己设计了一个编码,对应的在Code Page号是1252,被称为Windows 1252. Windo ...
- php将会话保存在数据库里
php默认把会话保存在临时文件中,保存在数据库中可以提高安全性,在共享主机服务器上,所有web站点都使用同一个临时目录,这意味着数十个程序都在同一位置进行文件读取的操作,我们很容易就编写一个脚本从这个 ...
- 匹配“is outside location”
<pre name="code" class="html">is outside location 怎么匹配? . 匹配除换行外的所有单个字符,通常 ...
- hmac_sha1 签名
use URI::Escape qw(uri_unescape); use MIME::Base64; use Digest::HMAC_SHA1; $str=Digest::HMAC_SHA1::h ...
- 系统中断与SA_RESTART
今天在调试程序时,sem_timedwait居然返回了一个Interrupted system call,错误码为EINTR.系统中断这东西我一向只闻其名,不见其"人",不想今天遇 ...
- ios 计算文字的尺寸
/** * 计算文字尺寸 * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - (CGSize)sizeW ...