[POI2007]ZAP-Queries

题意简述:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。

Solution

很显然这是一个莫比乌斯反演题。

\[ans=\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=d]
\]

然后我们设

\[f(d)=ans=\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=d]\\
g(x)=\sum_{x|d}f(d)
\]

\[f(x)=\sum_{x|d}\mu(\frac{d}{x})g(d)
\]

因为

\[g(x)=\sum_{i=1}^{a}\sum_{i=1}^{b}[x|gcd(i,j)]=\sum_{i=1}^{a/x}\sum_{i=1}^{b/x}[1|gcd(i,j)]=\frac{a}{x}\frac{b}{x}
\]

然后可以\(f(x)\)可以变成这样

\[f(x)=\sum_{x|d}\mu(\frac{d}{x})\frac{a}{d}\frac{b}{d}
\]

我们设\(t=\frac{d}{x}\),\(f(x)\)就成了这样

\[f(x)=\sum_{t=1}^{min(a,b)}\mu(t)\frac{a}{dx}\frac{b}{dx}
\]

此时\(f(x)\)已经可以\(O(n)\)计算了,但是由于多组询问,还需要采取数论分块的方式将时间复杂度优化到\(O(\sqrt{n})\)

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=5e4;long long ans;
int n,m,d,mu[maxn],prime[maxn],T,tot;bool vis[maxn];
void prepare()
{
mu[1]=1;
for(rg int i=2;i<=maxn;i++)
{
if(!vis[i])prime[++tot]=i,mu[i]=-1;
for(rg int j=1;j<=tot&&prime[j]*i<=maxn;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else {mu[i*prime[j]]=0;break;}
}
}
for(rg int i=1;i<=maxn;i++)mu[i]+=mu[i-1];
}
int main()
{
read(T);prepare();
while(T--)
{
read(n),read(m),read(d);if(n>m)swap(n,m);
ans=0;
for(rg int i=1,j;i<=n;i=j+1)
{
j=min(n/(n/i),m/(m/i));
long long t=1ll*(n/i/d)*(m/i/d);
ans+=t*(mu[j]-mu[i-1]);
}
printf("%lld\n",ans);
}
}

bzoj1101:[POI2007]ZAP-Queries的更多相关文章

  1. [BZOJ1101][POI2007]Zap

    [BZOJ1101][POI2007]Zap 试题描述 FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd ...

  2. BZOJ1101 POI2007 Zap 【莫比乌斯反演】

    BZOJ1101 POI2007 Zap Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b, ...

  3. BZOJ1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2951  Solved: 1293[Submit][Status ...

  4. Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...

  5. BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers

    Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...

  6. 【莫比乌斯反演】BZOJ1101 [POI2007]zap

    Description 回答T组询问,有多少组gcd(x,y)=d,x<=a, y<=b.T, a, b<=4e5. Solution 显然对于gcd=d的,应该把a/d b/d,然 ...

  7. 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)

    先看一道例题:[POI2007]Zap BZOJ 洛谷 题目大意:$T$ 组数据,求 $\sum^n_{i=1}\sum^m_{j=1}[gcd(i,j)=k]$ $1\leq T\leq 50000 ...

  8. BZOJ 1101: [POI2007]Zap

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2262  Solved: 895[Submit][Status] ...

  9. BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )

    求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...

  10. [POI2007]Zap

    bzoj 1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Descriptio ...

随机推荐

  1. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  2. animation steps属性实现帧动画

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  3. html5--5-12 渐变色

    html5--5-12 渐变色 学习要点 掌握渐变色的绘制方法 渐变色绘制方法 createLinearGradient() 创建线性渐变 createLinearGradient(x1,y1,x2, ...

  4. POJ1201 Intervals (差分约束)

    You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: ...

  5. Jmeter 在什么情况下定义多个thread group?

    Jmeter里面有三种线程组:setUp thread group, TearDown thread group, thread group. 如果想定义100个用户登录系统,60个用户做A操作,40 ...

  6. P4147玉蟾宫——最大子矩阵

    悬线法裸题. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace ...

  7. kafka数据可靠性深度解读【转】

    1 概述 Kakfa起初是由LinkedIn公司开发的一个分布式的消息系统,后成为Apache的一部分,它使用Scala编写,以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cl ...

  8. KVM虚拟机内无agent情况下的监控方法

    KVM虚拟机内无agent情况下的监控(ceilometer实现) 今天看到大家在群里讨论KVM虚拟机的监控问题,而且是要求VM内无agent情况下的监控.这方面确实没有深入研究,但尚有些openst ...

  9. POJ 1182 食物链 (破题)

    题意:中文题. 析:对POJ 真是无语,有的题用G++过不了,C++能过,有的题不写EOF(题目明明说就一组的数据的)不过,有的题写EOF也不过, 这个题就是写EOF就过不了... 这个题用的是加权并 ...

  10. 洛谷1601 A+B Problem(高精) 解题报告

    洛谷1601 A+B Problem(高精) 本题地址:http://www.luogu.org/problem/show?pid=1601 题目背景 无 题目描述 高精度加法,x相当于a+b pro ...