题目:

BZOJ 3309

分析:

首先,经过一番非常套路的莫比乌斯反演(实在懒得写了),我们得到:

\[\sum_{T=1}^n \sum_{d|T}f(d)\mu(\frac{T}{d})\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\]

那么,我们现在如果预处理出 \(g(n)=\sum_{d|n}f(d)\mu(\frac{n}{d})\) 的前缀和,就可以数论分块 \(O(\sqrt{n})\) 处理每次询问了。

\(\mu\) 函数有一个重要的性质:当 \(n\) 是某个数平方的倍数时(即某个质因子的次数不小于 \(2\) ), \(\mu(n)=0\) 。所以,真正有贡献的项只能是 \(\frac{n}{d}\) 的质因子互不相同的项,共有 \(2^k\) 项,其中 \(k\) 是 \(n\) 的质因子种数(相当于每种质因子的次数要么为 \(0\) ,要么为 \(1\) )。

设这 \(k\) 个质因子中有 \(a\) 个的次数为 \(f(n)\) ,则这 \(2^k\) 项中有 \(2^{k-a}\) 项的 \(f(d)\) 为 \(f(n)-1\) (即这 \(a\) 个质因子 全部 选入 \(\frac{n}{d}\) 的情况),其余情况为 \(f(n)\) 。根据 \(\mu\) 的定义,每一项乘上的系数与选了多少个质因子进入 \(\frac{n}{d}\) 有关,奇数为 \(-1\) ,偶数为 \(1\) 。而这 \(2^{k-a}\) 项中(暂定 \(k>a\) ),选的质因子数为奇数、偶数的项数相等,所以和为 \(0\) 。同理,剩下 \(2^k-2^{k-a}\) 项之和也是 \(0\)

但是,当 \(k=a\) 时,\(f(d)=f(n)-1\) 的只有 \(2^{k-a}=1\) 项,绝对值为 \(f(n)-1\) 而不是 \(0\) 。同时,此时 \(f(d)=f(n)\) 的项也是奇数个,和的绝对值为 \(f(n)\) 。这两项具体的符号与 \(k\) 的奇偶性有关(并且一定符号相反)。稍微推一下可得,当 \(k\) 为奇数, \(g(n)=1\) ;当 \(k\) 为偶数, \(g(n)=-1\) 。

综上可得

\[g(n)=\begin{cases} 0\ (k\neq a)\\ 1\ (k=a且k是奇数)\\ -1\ (k=a且k是偶数) \end{cases}\]

代码:

#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;namespace zyt
{template<typename T>inline bool read(T &x){char c;bool f = false;x = 0;doc = getchar();while (c != EOF && c != '-' && !isdigit(c));if (c == EOF)return false;if (c == '-')f = true, c = getchar();dox = x * 10 + c - '0', c = getchar();while (isdigit(c));if (f)x = -x;return true;}template<typename T>inline void write(T x){static char buf[20];char *pos = buf;if (x < 0)putchar('-'), x = -x;do*pos++ = x % 10 + '0';while (x /= 10);while (pos > buf)putchar(*--pos);}typedef long long ll;const int N = 1e7 + 10;int prime[N], f[N], g[N], num[N], cnt[N], tot[N], pcnt;bool mark[N];void init(){f[1] = 0;for (int i = 2; i < N; i++){if (!mark[i])prime[++pcnt] = i, f[i] = num[i] = cnt[i] = tot[i] = 1;for (int j = 1; j <= pcnt && (ll)i * prime[j] < N; j++){int k = i * prime[j];mark[k] = true;if (i % prime[j] == 0){num[k] = num[i] + 1;f[k] = max(f[i], num[k]);tot[k] = tot[i];if (f[i] == num[k])cnt[k] = cnt[i] + 1;else if (f[i] > num[k])cnt[k] = cnt[i];elsecnt[k] = 1;break;}else{num[k] = 1;f[k] = max(f[i], 1);tot[k] = tot[i] + 1;if (f[i] == 1)cnt[k] = cnt[i] + 1;elsecnt[k] = cnt[i];}}}g[1] = 0;for (int i = 2; i < N; i++)g[i] = (cnt[i] == tot[i] ? ((tot[i] & 1) ? 1 : -1) : 0) + g[i - 1];}int work(){int T;read(T);init();while (T--){int n, m;read(n), read(m);if (n > m)swap(n, m);int pos = 1;ll ans = 0;while (pos <= n){int tmp = min(n / (n / pos), m / (m / pos));ans += ll(g[tmp] - g[pos - 1]) * (n / pos) * (m / pos);pos = tmp + 1;}write(ans), putchar('\n');}return 0;}
}
int main()
{
#ifdef BlueSpiritfreopen("3309.in", "r", stdin);
#endifreturn zyt::work();
}

转载于:https://www.cnblogs.com/zyt1253679098/p/10917006.html

【BZOJ3309】DZY Loves Math(线性筛)相关推荐

  1. BZOJ3309 DZY Loves Math(莫比乌斯反演+线性筛)

    一通正常的莫比乌斯反演后,我们只需要求出g(n)=Σf(d)*μ(n/d)的前缀和就好了. 考虑怎么求g(n).当然是打表啊.设n=∏piai,n/d=∏pibi .显然若存在bi>1则这个d没 ...

  2. bzoj3309: DZY Loves Math

    这显然要用莫比乌斯反演: Ans=∑ai=1∑bj=1f(gcd(i,j))=∑df(d)∑⌊ad⌋i=1∑⌊bd⌋j=1[gcd(i,j)=1]=∑df(d)∑⌊ad⌋i=1∑⌊bd⌋j=1∑p|T ...

  3. DZY Loves Math系列

    link 好久没写数学题了,再这样下去吃枣药丸啊. 找一套应该还比较有意思的数学题来做. [bzoj3309]DZY Loves Math 简单推一下. \[\sum_{i=1}^n\sum_{j=1 ...

  4. 【BZOJ3309】DZY Loves Math 解题报告

    [BZOJ3309]DZY Loves Math Description 对于正整数\(n\),定义\(f(n)\)为\(n\)所含质因子的最大幂指数.例如\(f(1960)=f(2^3×5^1×7^ ...

  5. 【BZOJ3512】DZY Loves Math IV(杜教筛)

    [BZOJ3512]DZY Loves Math IV(杜教筛) https://www.cnblogs.com/cjyyb/p/10165338.html

  6. 【BZOJ3309】DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MB Submit: 411 Solved: 161 [Submit][Status ...

  7. 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) ...

  8. DZY Loves Math 系列详细题解

    BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...

  9. bzoj 3739 DZY loves math VIII

    3739: DZY loves math VIII Time Limit: 25 Sec Memory Limit: 512 MB Submit: 318 Solved: 50 [Submit][St ...

  10. BZOJ3560 DZY Loves Math V

    原题链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3560 DZY Loves Math V Description 给定n个正整数a1,a2 ...

最新文章

  1. navicat对mysql的备份
  2. 两所顶尖大学,签约落地深圳!
  3. HTML5 drag drop 拖拽与拖放简介
  4. 24.内存操作Copy-Move-Clone.rs
  5. 基于JAVA+SpringMVC+MYSQL的小说管理系统
  6. java基本操作-2
  7. 网络编程(三)sqlist轻量级数据库的简单应用
  8. Remoting服务实例
  9. 浅谈MFC中BitBlt与StretchDIBits的区别
  10. 大话机器人之云端架构
  11. 十进制转化为十二进制
  12. Amazon 新手要知道 100 个问题
  13. Excel - 单元格设置斜线 并 添加文字
  14. rap格式鸿蒙,你,想要成为rap star吗?
  15. 解决数据库日期返回格式不是yyyy-MM-dd HH:mm:ss 的问题
  16. 一辈子不用考试?你可能是个假程序员
  17. Day1_Python基础
  18. 基于24位Δ-ΣADC和FPGA的高精度数据采集系统开发
  19. shienblink 振动/敲击检测传感器详解
  20. 英文SEO移动端搜索优化指南

热门文章

  1. 百度echart柱图、折线图、饼图、Map类型等类似视图的动态加载数据
  2. Intellij idea 2021安装黑色主题
  3. 热门、爆款必备8个标题套路,再也不愁写标题了(2)
  4. 最常用计算机机箱,电脑机箱的常用材质是什么
  5. android手机太卡怎么办,安卓手机卡顿反应慢怎么办?学会了这些就可以轻松解决...
  6. 动态字符串数组相关定义,c++,c
  7. _beginthreadex创建多线程的用法
  8. 在 Hyperledger Fabric v1.4 下添加 PBFT 共识算法
  9. 喜大普奔!PC内存今年Q4终于要降价了!
  10. 2021美通社媒体趋势云沙龙 – 破圈的新切入点到底是什么?