【BZOJ2693】jzptab & 【BZOJ2154】Crash的数字表格
题目
弱化版题目的传送门(【BZOJ2154】Crash的数字表格)
思路&解法
题目是要求: \(\sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}lcm(i, j)\)
于是我们可以把式子化成这样:
\]
然后我们枚举gcd
\]
我们再把式子换一下
\]
\]
\]
反演一下
\]
\]
\]
其中$$F(n, m) = {nm(n+1)(m+1)\over 4}$$
继续优化
\]
后面的\(\sum\limits_{d|T}\mu(d)d^2{\frac{T}{d}}\)的前缀和很容易求
代码
【BZOJ2693】jzptab
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL mod = 100000009LL;
const int N = 10000010;
int p[N], total;
bool vis[N];
LL g[N];
void init()
{ g[1] = 1;
for (int i = 2; i <= 10000000; i++)
{ if (!vis[i]) p[++total] = i, g[i] = (LL) (1 - i + mod) % mod;
for (int j = 1; j <= total && i * (LL) p[j] <= 10000000; j++)
{ vis[i * p[j]] = 1;
if (i % p[j] == 0) { g[i * p[j]] = g[i]; break; }
else g[i * p[j]] = (g[i] * g[p[j]]) % mod;
}
}
for (int i = 2; i <= 10000000; i++) g[i] = (g[i] * i + g[i-1]) % mod;
}
LL Get(int n) { return ((LL) n * (LL) (n+1) / 2LL) % mod; }
LL Sum(int n, int m) { return (Get(n) * Get(m)) % mod; }
LL Calc(int n, int m)
{ int last = 0;
LL Ans = 0;
for (int i = 1; i <= min(n, m); i = last+1)
{ last = min(n / (n/i), m / (m/i));
Ans = (Ans + (Sum(n/i, m/i) * (g[last] - g[i-1])) % mod) % mod;
}
return (Ans + mod) % mod;
}
int main()
{ init();
int T;
scanf("%d", &T);
while (T--)
{ int n, m;
scanf("%d %d", &n, &m);
printf("%lld\n", Calc(n, m));
}
return 0;
}
【BZOJ2154】Crash的数字表格
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL mod = 20101009LL;
const int N = 10000010;
int p[N], total;
bool vis[N];
LL g[N];
void init()
{ g[1] = 1;
for (int i = 2; i <= 10000000; i++)
{ if (!vis[i]) p[++total] = i, g[i] = (LL) (1 - i + mod) % mod;
for (int j = 1; j <= total && i * (LL) p[j] <= 10000000; j++)
{ vis[i * p[j]] = 1;
if (i % p[j] == 0) { g[i * p[j]] = g[i]; break; }
else g[i * p[j]] = (g[i] * g[p[j]]) % mod;
}
}
for (int i = 2; i <= 10000000; i++) g[i] = (g[i] * i + g[i-1]) % mod;
}
LL Get(int n) { return ((LL) n * (LL) (n+1) / 2LL) % mod; }
LL Sum(int n, int m) { return (Get(n) * Get(m)) % mod; }
LL Calc(int n, int m)
{ int last = 0;
LL Ans = 0;
for (int i = 1; i <= min(n, m); i = last+1)
{ last = min(n / (n/i), m / (m/i));
Ans = (Ans + (Sum(n/i, m/i) * (g[last] - g[i-1])) % mod) % mod;
}
return (Ans + mod) % mod;
}
int main()
{ init();
int n, m;
scanf("%d %d", &n, &m);
printf("%lld\n", Calc(n, m));
return 0;
}
一些其他的东西
弱化版题目可以\(O(n)\)过, 然而我是用\(O(\sqrt{n})\)的算法做的, 而且达到了惊人的18s, 比\(O(n)\)的解法慢多了。我哪里写锉了。。。。。。
【BZOJ2693】jzptab & 【BZOJ2154】Crash的数字表格的更多相关文章
- BZOJ2154 Crash的数字表格 【莫比乌斯反演】
BZOJ2154 Crash的数字表格 Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b) ...
- BZOJ2154: Crash的数字表格 & BZOJ2693: jzptab
[传送门:BZOJ2154&BZOJ2693] 简要题意: 给出n,m,求$\sum_{i=1}^{n}\sum_{j=1}^{m}LCM(i,j)$ 题解: 莫比乌斯反演(因为BZOJ269 ...
- 莫比乌斯反演套路三、四--BZOJ2154: Crash的数字表格 && BZOJ2693: jzptab
t<=1e4个询问每次问n,m<=1e7,$\sum_{1\leqslant x \leqslant n,1 \leqslant y\leqslant m}lcm(x,y)$. 首先题目要 ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- BZOJ2154: Crash的数字表格
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2154 题意&&题解:http://www.cnblogs.com/jiangl ...
- 【莫比乌斯反演】BZOJ2154 Crash的数字表格
Description 求sigma lcm(x,y),x<=n,y<=m.n,m<=1e7. Solution lcm没有什么直接做的好方法,用lcm=x*y/gcd转成gcd来做 ...
- bzoj千题计划253:bzoj2154: Crash的数字表格
http://www.lydsy.com/JudgeOnline/problem.php?id=2154 #include<cstdio> #include<algorithm> ...
- bzoj2154: Crash的数字表格 莫比乌斯反演
题意:求\(\sum_{i=1}^n \sum_{j=1}^m\frac{i*j}{gcd(i,j)}\) 题解:\(ans=\sum_{i=1}^n\sum_{j=1}^m \frac{i*j}{g ...
- [bzoj2154]Crash的数字表格(mobius反演)
题意:$\sum\limits_{i = 1}^n {\sum\limits_{j = 1}^m {lcm(i,j)} } $ 解题关键: $\sum\limits_{i = 1}^n {\sum\l ...
随机推荐
- UICollectionViewFlowLayout & UICollectionViewDelegateFlowLayout
A concrete layout object that organizes items into a grid with optional header and footer views for ...
- Apache 在Linux上的安装
1.获取源码 wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz 2.卸载centos自带的apache 3.解压apach ...
- Python虚拟环境和requirements.txt文件的使用
参考: https://www.centos.bz/2018/05/centos-7-4-%E5%AE%89%E8%A3%85python3%E5%8F%8A%E8%99%9A%E6%8B%9F%E7 ...
- id 转 entity
object 是 entity原始的类 要使用id转化成entity要先将id.getobject 然后将这个值 (entity)转化成entity entity ent =id.getentity& ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
- 零基础学习Linux培训,应该选择哪个培训班?
云计算早已不是什么稀奇的概念,它的火爆让Linux运维工程师这个职业越来越重要.在当今各类云平台提供的系统中,Linux系统几乎毫无争议的独占鳌头,市场份额进一步扩张. 这也让Linux运维工程师职位 ...
- Centos7搭建ansible运维自动化工具
1)设置主机名和hosts文件 2)配置阿里云repo源 Wget -O /etc/yum.repos.d/aliyun.repo https://mirrors.aliyun.com/repo/Ce ...
- Bitvise ssh client+ chrome +SwitchyOmega *** (xjl456852原创)
首先这个比ss还要简单,ss还需要在vps上搭建服务器.这个不需要. 但是无论是ss 还是 bitvise 都需要有一个自己的vps才行. 首先打开Bitvise ssh client程序: ...
- 【hdu 2036】改革春风吹满地
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=2036 [题意] 中文题 [题解] 这里用的是叉积对应的求三角形的面积; 即 A×B=A*B*sin ...
- VScode使用简介
1.1 VSCode简介 VSCode官网:https://code.visualstudio.com/ 支持语音: 速度较快,对超大文件读写速度飞快(打开10M代码不到1s,Subline原生会卡近 ...