【BZOJ】3309: DZY Loves Math
题意
\(T(T \le 10000)\)次询问,每次给出\(a, b(1 \le a, b \le 10^7)\),求
\]
其中\(f(n)\)表示\(n\)所含质因子的最大幂指数。\(f(1)=0\)。
分析
以下默认\(a \le b\)
$$
\begin{align}
& \sum_{i=1}^{a} \sum_{j=1}^{b} f((i, j)) \\
= & \sum_{d=1}^{a} f(d) \sum_{i=1}^{a'} \sum_{j=1}^{b'} [(i, j)=1] & \left( a' = \left \lfloor \frac{a}{d} \right \rfloor, b' = \left \lfloor \frac{b}{d} \right \rfloor \right) \\
= & \sum_{d=1}^{a} f(d) \sum_{i=1}^{a'} \sum_{j=1}^{b'} \sum_{k|(i, j)} \mu(k) \\
= & \sum_{d=1}^{a} f(d) \sum_{k=1}^{\left \lfloor \frac{a}{d} \right \rfloor} \mu(k) \left \lfloor \frac{a}{kd} \right \rfloor \left \lfloor \frac{b}{kd} \right \rfloor \\
= & \sum_{T=1}^{a} \left \lfloor \frac{a}{T} \right \rfloor \left \lfloor \frac{b}{T} \right \rfloor \sum_{d|T} \mu(d) f(\frac{T}{d}) \\
\end{align}
$$
考虑\(g(n) = \sum_{i|n} \mu(i) f(\frac{n}{i})\):
令\(S = \\{ p_i \\}\),\(p_i\)为\(n\)的质因子,则只有当\(i=\prod_{j \in S' \subseteq S} j\)时才对\(g(n)\)有贡献。
令\(A \subseteq S\)表示指数最大(假设为\(y\))的质因子集合,\(B=S-A\)。则\(i\)可以看做从\(A\)中选出一些质因子再从\(B\)中选出一些质因子组合一下。
由于\(f(\frac{n}{i})\)的取值只取决于\(i\)中从\(A\)集合取的子集(可以发现\(f\)要么是\(a\),要么是\(a-1\),由\(A\)来决定的),所以我们只需考虑\(A\)的子集。
当\(B \neq \varnothing\)时,当选的\(A\)的子集确定后即\(f(\frac{n}{i})\)相同时,由于从\(B\)中奇偶大小子集的个数相同,因此奇数个质因子的\(i\)和偶数个质因子的\(i\)的个数相同,所以\(\mu(i)\)的和为\(0\)。所以\(g(n)=0\)。
当\(B = \varnothing\)时,则只有当\(i=\prod_{j \in S} j\)时\(f(\frac{n}{i})=y-1\),否则\(f(\frac{n}{i})=y\)。而当\(f(\frac{n}{i})=y\)时,对于这个非全集的所有子集,奇数大小的子集和偶数大小的子集个数相差为1,计算一下就知道这种情况的贡献是\((-1)^{|S|+1} a\)。对于全集,贡献是\((-1)^{|S|} (a-1)\)。所以\(g(n) = (-1)^{|S|} (a-1) + (-1)^{|S|+1} a = (-1)^{|S|+1}\)
于是线性筛筛出\(g(n)\)即可。
题解
至于查询,分块就行了。
复杂度\(O(b+Tb^{0.5})\)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Lim=10000005;
int N, x[10005], y[10005], f[Lim], p[Lim], mu[Lim], cnt;
bool np[Lim];
ll sum[Lim];
void init() {
mu[1]=1;
for(int i=2; i<=N; ++i) {
if(!np[i]) p[cnt++]=i, mu[i]=-1;
for(int j=0; j<cnt; ++j) {
int t=p[j]*i; if(t>N) break;
np[t]=1;
if(i%p[j]==0) break;
mu[t]=-mu[i];
}
}
for(int i=2; i<=N; ++i) {
if(mu[i]) {
sum[i]=-mu[i];
}
}
for(int i=N; i>=2; --i) {
if(sum[i]) {
for(ll j=(ll)i*i; j<=N; j*=i) {
sum[j]=sum[i];
}
}
}
for(int i=2; i<=N; ++i) {
sum[i]+=sum[i-1];
}
}
int main() {
int T; scanf("%d", &T);
for(int i=1; i<=T; ++i) scanf("%d%d", &x[i], &y[i]), N=max(N, x[i]), N=max(N, y[i]);
init();
for(int tt=1; tt<=T; ++tt) {
int a=x[tt], b=y[tt];
if(a>b) swap(a, b);
int now=1;
ll ans=0;
for(int i=1; i<=a; i=now+1) {
now=min(a/(a/i), b/(b/i));
ans+=(ll)(a/i)*(ll)(b/i)*(sum[now]-sum[i-1]);
}
printf("%lld\n", ans);
}
return 0;
}
【BZOJ】3309: DZY Loves Math的更多相关文章
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- 【BZOJ】3561: DZY Loves Math VI
题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...
- 【BZOJ】3542: DZY Loves March
题意 \(m * m\)的网格,有\(n\)个点.\(t\)个询问:操作一:第\(x\)个点向四个方向移动了\(d\)个单位.操作二:询问同行同列其他点到这个点的曼哈顿距离和.强制在线.(\(n \l ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
- ●BZOJ 3309 DZY Loves Math
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...
- 【BZOJ 3561】 3561: DZY Loves Math VI (莫比乌斯,均摊log)
3561: DZY Loves Math VI Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 205 Solved: 141 Description ...
- 【BZOJ 3560】 3560: DZY Loves Math V (欧拉函数)
3560: DZY Loves Math V Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 241 Solved: 133 Description ...
- bzoj 3309 DZY Loves Math 莫比乌斯反演
DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1303 Solved: 819[Submit][Status][Dis ...
- bzoj 3309 DZY Loves Math——反演+线性筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 像这种数据范围,一般是线性预处理,每个询问 sqrt (数论分块)做. 先反演一番.然 ...
随机推荐
- 双守护进程(不死service)-5.0系统以下
上链接: http://files.cnblogs.com/files/andlp/DaemonProcess.zip 5.0以上 参考marsDaemon
- [Unity] 3D数学基础 - 2D旋转矩阵
2D矩阵的旋转: NewX = X * Cos(α) - Y * Sin(α) NewY = X * Sin(α) + Y * Cos(α) 一般在三角函数中使用的是弧度,我们可以通过下面的公式将角度 ...
- API接口:分页
// 查询满足要求的总记录数 $count = M("back")->where($back_map)->count(); $pagecount = ceil($cou ...
- todoList使用教程
网页链接:http://www.cnblogs.com/sunada2005/articles/2663030.html
- Android ViewPager sharedpreferences
http://www.cnblogs.com/dwinter/archive/2012/02/27/AndroidViewPager%E5%A4%9A%E9%A1%B5%E9%9D%A2%E6%BB% ...
- Excel 实用技巧之一
1.在单元格内换行: Alt+Enter 2.合并其他单元格文字并换行: A1&char(10)&B1 3.Excel计算样本估算总体方差:STDEV/STDEVA(),分母为n-1. ...
- jaxb
一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...
- mysql的DISABLE/ENABLE KEYS
有一个表 tbl1 的结构如下: CREATE TABLE `tbl1` ( `id` int(10) unsigned NOT NULL auto_increment, `name` char(20 ...
- BZOJ2007——[Noi2010]海拔
1.题意:一个裸的最小割 2.分析:直接转成对偶图最短路就好了,水爆了!(雾) #include <queue> #include <cstdio> #include < ...
- 正则表达式工具RegexBuddy使用教程
1. 界面介绍 (1)初始界面选项介绍 (2)如何使用匹配 (3)如何使用正则替换 (4)如何使用Debug http://www.cnblogs.com/tsql/p/5860893.html