bzoj2820-GCD
题意
\(T\le 10^4\) 次询问 \(n,m\) ,求
\]
分析
这题还是很有趣的。设 \(n\le m\) 。
\sum _{i=1}^n\sum_{j=1}^m[gcd(i,j)\text { is prime}]&=\sum _{i=1}^n\sum _{j=1}^m\sum _k [k\text { is prime}][gcd(i,j)=k] \\
&=\sum _{i=1}^n\sum _{j=1}^m\sum _{k|i,k|j}[k\text { is prime}]\sum _{d|\frac{i}{k},d|\frac{j}{k}}\mu(d) \\
&=\sum _{d=1}^n\mu (d)\sum _{k=1}^n[k\text { is prime}]\sum _{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum _{j=1}^{\lfloor\frac{m}{k}\rfloor}[d|i,d|j] \\
&=\sum _{d=1}^n\mu (d)\sum _{k=1}^n[k\text { is prime}]\lfloor\frac{n}{kd}\rfloor \lfloor\frac{m}{kd}\rfloor \\
&=\sum _{i=1}^n\lfloor\frac{n}{i}\rfloor \lfloor\frac{m}{i}\rfloor\sum _{k|i,k\text { is prime}}\mu(\frac{i}{k})
\end{aligned}
\]
令 \(f(x)=\sum _{k|x,k\text {is prime }}\mu (x/k)\) ,我们有:
\]
\(f(x)\) 可以在线性筛的过程中顺便处理出来,求前缀和就可以做到每次询问 \(O(\sqrt n)\) 。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long giant;
inline int read() {
int x=0,f=1;
char c=getchar_unlocked();
for (;!isdigit(c);c=getchar_unlocked()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar_unlocked()) x=x*10+c-'0';
return x*f;
}
const int maxn=1e7+1;
bool np[maxn];
int p[maxn],ps=0,mu[maxn],f[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
mu[1]=1,f[1]=0;
for (int i=2;i<maxn;++i) {
if (!np[i]) p[++ps]=i,mu[i]=-1,f[i]=1;
for (int j=1,tmp;j<=ps && (tmp=i*p[j])<maxn;++j) {
np[tmp]=true;
if (i%p[j]) mu[tmp]=-mu[i],f[tmp]=mu[i]-f[i]; else {
mu[tmp]=0;
f[tmp]=mu[i];
break;
}
}
}
for (int i=2;i<maxn;++i) f[i]+=f[i-1];
int T=read();
while (T--) {
int n=read(),m=read();
if (n>m) swap(n,m);
giant ans=0;
for (int i=1,j;i<=n;i=j+1) {
j=min(n/(n/i),m/(m/i));
ans+=(giant)(f[j]-f[i-1])*(n/i)*(m/i);
}
printf("%lld\n",ans);
}
return 0;
}
bzoj2820-GCD的更多相关文章
- 【Learning】 莫比乌斯反演
莫比乌斯反演 对于两个定义域为非负整数的函数\(F(n)\)和\(f(n)\) 若满足:\(F(n)=\sum\limits_{d|n}f(d)\),则反演得到\(f(n)=\sum\limi ...
- [BZOJ2820]YY的GCD
[BZOJ2820]YY的GCD 试题描述 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- 【BZOJ2820】YY的GCD(莫比乌斯反演)
[BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...
- 【BZOJ2820】YY的GCD
[BZOJ2820]YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的( ...
- BZOJ2820 YY的GCD 【莫比乌斯反演】
BZOJ2820 YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, ...
- BZOJ2820 YY的GCD 莫比乌斯+系数前缀和
/** 题目:BZOJ2820 YY的GCD 链接:http://www.cogs.pro/cogs/problem/problem.php?pid=2165 题意:神犇YY虐完数论后给傻×kAc出了 ...
- 【反演复习计划】【bzoj2820】YY的GCD
这题跟2818一样的,只不过数据水一点,可以用多一个log的办法水过去…… 原题意思是求以下式子:$Ans=\sum\limits_{isprime(p)}\sum\limits_{i=1}^{a}\ ...
- BZOJ2820/LG2257 YY的GCD 莫比乌斯反演
问题描述 BZOJ2820 LG2257 题解 求 \(\sum\limits_{i=1}^{n}{\sum\limits_{j=1}^{m}{[gcd(i,j)==p]}}\) ,其中 \(p\)为 ...
- Bzoj-2820 YY的GCD Mobius反演,分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2820 题意:多次询问,求1<=x<=N, 1<=y<=M且gcd( ...
- [BZOJ2820][Luogu2257]YY的GCD
BZOJ权限题 Luogu 题意:给出n,m,求: \[\sum_{i=1}^{n}\sum_{j=1}^{m}[\gcd(i,j)\mbox{为质数}]\] 多组数据,\(n\le 10^7\) s ...
随机推荐
- 20155318 《Java程序设计》实验四 (Android程序设计)实验报告
20155318 <Java程序设计>实验四 (Android程序设计)实验报告 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android.组 ...
- 考研编程练习----Kruskal
#include <stdio.h> #include <stdlib.h> #define MAX 100 /* 定义边(x,y),权为w */ typedef st ...
- WPF RoutedEvent and HitTest - 简书
原文:WPF RoutedEvent and HitTest - 简书 学习的时候切忌心浮气躁,慢慢的过每一个知识点,不要漏掉任何细节.不然当遇到细节问题的时候,会恼,会闹,会悔不该当初--花一下午调 ...
- [BZOJ3218]a + b Problem-[主席树+网络流-最小割]
Description 传送门 Solution 此处我们按最小割的思路考虑. 暴力:S->i表示该点选黑色的权值b[i]:i->T表示该点选白色的权值w[i].考虑如果某个点i受点j为白 ...
- codevs 5429 多重背包
5429 多重背包 http://codevs.cn/problem/5429 分析: f[i]=g[j-k*siz[i]]+k*val[i]; 发现一个状态d只会更新,d+siz[i],d+2*si ...
- Scala中==,eq与equals的区别
根据官方API的定义: final def ==(arg0: Any): Boolean The expression x == that is equivalent to if (x eq null ...
- 详解UML图之类图
产品经理的必备技能之一是画UML图,本文就告诉你怎么画标准的类图吧.本文结合网络资料和个人心得所成,不当之处,请多指教. 1.为什么需要类图?类图的作用 我们做项目的需求分析,最开始往往得到的是一堆文 ...
- Codeforces Round #553 (Div. 2) C
C. Problem for Nazar time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
1. 摘要 训练深层的神经网络非常困难,因为在训练的过程中,随着前面层数参数的改变,每层输入的分布也会随之改变.这需要我们设置较小的学习率并且谨慎地对参数进行初始化,因此训练过程比较缓慢. 作者将这种 ...
- DevOps之六 shell以及pipeline 命令部署
一 使用shell命启动spring boot 项目 1. 使用shell停止当前项目 #!/bin/sh main() { clear pid=`ps -ef|grep xx.jar|grep -v ...