题链:

http://www.lydsy.com/JudgeOnline/problem.php?id=3309

题解:

莫比乌斯反演,线筛

化一化式子:

f(x)表示x的质因子分解中的最大幂指数

$\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))$

$\quad\quad=\sum_{g=1}^{n}f(g)\sum_{d=1}^{\lfloor \frac{n}{g} \rfloor} \mu(d)\lfloor \frac{n}{gd} \rfloor\lfloor \frac{m}{gd} \rfloor$

$\quad\quad=\sum_{D=gd=1}^{n}(\lfloor \frac{n}{D} \rfloor\lfloor \frac{m}{D} \rfloor)\sum_{g|D} f(g)u(\frac{D}{g})$

令 $w[D]=\sum_{g|D} f(g)u(\frac{D}{g})$

然后如果能够预处理出w[D],那么这个题的每个询问就可以在$O(\sqrt N)$的复杂度内解决。

虽然w[D]不是积性函数,但仍可以在线筛时求出,详见BZOJ 3309: DZY Loves Math [莫比乌斯反演 线性筛]

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 10000007
using namespace std;
int g[MAXN];
void Sieve(){
static bool np[MAXN];
static int prime[MAXN],idx[MAXN],hav[MAXN],pnt;
for(int i=2,tmp,d;i<=10000000;i++){
if(!np[i]) prime[++pnt]=i,hav[i]=1,idx[i]=1,g[i]=1;
for(int j=1;j<=pnt&&i<=10000000/prime[j];j++){
np[i*prime[j]]=1; hav[i*prime[j]]=hav[i]+(i%prime[j]!=0); d=1; tmp=i; while(tmp%prime[j]==0) d++,tmp/=prime[j];
if(idx[tmp]==d||tmp==1) idx[i*prime[j]]=d;
if(tmp==1) g[i*prime[j]]=1;
else if(idx[i*prime[j]]) g[i*prime[j]]=-1*(hav[i*prime[j]]&1?-1:1); if(i%prime[j]==0) break;
}
}
for(int i=1;i<=10000000;i++) g[i]+=g[i-1];
}
int main(){
Sieve();
int Case,n,m,mini; long long ans;
scanf("%d",&Case);
while(Case--){
scanf("%d%d",&n,&m);
ans=0; mini=min(n,m);
for(int D=1,last;D<=mini;D=last+1){
last=min(n/(n/D),m/(m/D));
ans+=1ll*(g[last]-g[D-1])*(n/D)*(m/D);
}
printf("%lld\n",ans);
}
return 0;
}

  

●BZOJ 3309 DZY Loves Math的更多相关文章

  1. BZOJ 3309: DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 761  Solved: 401[Submit][Status ...

  2. bzoj 3309 DZY Loves Math 莫比乌斯反演

    DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1303  Solved: 819[Submit][Status][Dis ...

  3. bzoj 3309 DZY Loves Math——反演+线性筛

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 像这种数据范围,一般是线性预处理,每个询问 sqrt (数论分块)做. 先反演一番.然 ...

  4. bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...

  5. BZOJ 3309: DZY Loves Math [莫比乌斯反演 线性筛]

    题意:\(f(n)\)为n的质因子分解中的最大幂指数,求\(\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d| ...

  6. BZOJ 3309 DZY Loves Math ——莫比乌斯反演

    枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...

  7. BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表

    有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...

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

  9. BZOJ 3561 DZY Loves Math VI

    BZOJ 3561 DZY Loves Math VI 求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\text{lcm}(i,j)^{\gcd(i,j)}\),钦定\(n\leq m ...

随机推荐

  1. Python多线程案例

    from time import ctime,sleep import threading def music(): for i in range(2): print ("I was lis ...

  2. 【iOS】OC-UTC日期字符串格式化

    NSLog(@"%@",[NSDate date]); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init ...

  3. DML数据操作语言之查询(二)

    当我们查询出了N条记录之后 ,我们知道一共是几条记录,或者这些记录某一字段(列值)的最大值,最小值,平均值等,就可以使用聚合函数. 1.聚合函数 聚合函数会将null 排除在外.但是count(*)例 ...

  4. 《javascript设计模式与开发实践》阅读笔记(12)—— 享元模式

    享元模式 享元(flyweight)模式是一种用于性能优化的模式,"fly"在这里是苍蝇的意思,意为蝇量级.享元模式的核心是运用共享技术来有效支持大量细粒度的对象. 享元模式的核心 ...

  5. DES加密实现的思想及代码

    感谢: http://blog.csdn.net/yxstars/article/details/38424021 上面的日志非常清晰的写出了这个DES加密的过程,主要存在初始IP置换,然后中间存在8 ...

  6. LAMP 搭建

    p { margin-bottom: 0.25cm; line-height: 120% } LAMP 搭建 承 Ubuntu 17.10.1安装, 定制. 参考 电子工业出版社, Ubuntu完美应 ...

  7. Python内置函数(22)——list

    英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...

  8. ajax中设置contentType: “application/json”的作用

    最近在做项目交互的时候,刚开始向后台传递数据返回415,后来百度添加了 contentType:"application/json"之后返回400,然后把传输的数据格式改为json ...

  9. Spring Boot面试题

    Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...

  10. 如何在pycharm中使用配置好的virtualenv环境

    1.手动建立: 第一步 建立虚拟环境 Windows cmd: pip install virtualenv 创建虚拟环境目录 env 激活虚拟环境 C:\Python27\Scripts\env\S ...