【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 (数论分块)做. 先反演一番.然 ...
随机推荐
- css 图片垂直居中总结
1.利用vertical-align:middle: 父级元素设置成display:table-cell; 同级元素设置一个span标签 设置display:inline-block:图片样式设置ve ...
- 在ie浏览器,360浏览器下,margin:0 auto;不居中的原因
转自 http://blog.sina.com.cn/s/blog_6eef6bf60100nn4m.html margin:0 auto:不居中可能有以下两个的原因 没有设置宽度 看看上面的代码,根 ...
- php数组函数分析--array_column
array_column 官方地址:array_column array_column 只能在 PHP版本5.5以上的运行,5.3是不支持这个函数的.如果5.3使用会报: Fatal error: C ...
- tyvj1294 小v的舞会
背景 "梦中伊人,断我男儿几寸柔肠,于断桥,不知西风自憔悴那姑娘."小v的梦中伊人要带领一大帮姐妹MM们来小v家举办舞会,然而怎么安排跳舞的顺序成了大问题,你能帮他么? 描述 有n ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- Linux设置Memcached开机启动
Memcached开机启动方式 方法一: 在 /etc/rc.d/rc.local 文件中追加启动命令 /usr/local/memcached/bin/memcached -u root -d - ...
- Mac Pro 软件安装/个性化配置 汇总
苹果产品维修 一.Spotlight 搜索程序和文档 Spotlight是最最常用的东西, 类似Windows开始菜单中的搜索. 可以用来搜索文档,也可以搜索本机的程序, 这样可以快速启动. 点击右 ...
- Android-做个性化的进度条
1.案例效果图 2.准备素材 progress1.png(78*78) progress2.png(78*78) ...
- html用户注册界面
html用户注册界面 先上一张简约的界面的效果图 这里是style里面的内容 <style> input[type]{ border: 1px solid darkorange; ba ...
- JavaScript深入浅出5-数组
慕课网教程视频地址:Javascript深入浅出 数组:值的有序集合 创建数组:字面量,构造器new array() 数组的读写:push() 尾部加入新元素 unshift() 头部加入新元素 po ...