【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 ...
随机推荐
- Flask 框架构建
Flask 框架构建,目标构建成Django类似的结构 一. 先看看构建后的效果 # 第一次初始化 python manage.py db init # 生成数据库版本 python manage.p ...
- SYN(synchronous)TCP/IP
SYN(synchronous)是TCP/IP建立连接时使用的握手信号.在客户机和服务器之间建立正常的TCP网络连接时,客户机首先发出一个SYN消息,服务器使用SYN+ACK应答表示接收到了这个消息, ...
- cocos creator destroy方法
node.destroy(),Node.destroyAllChildren并不会立即销毁,实际销毁操作会延迟到当前帧渲染前执行. 这段话可能不明白,但是在Node.destroyAllChildre ...
- 常见的Xshell运行命令
最近接触到了Xshell这个软件,使用这个软件我们来进行连接Linux系统,进去之后我们可能会两眼一抹黑,小编就带大家来学些常见的shell命令. 首先我们要跟大家从最简单的聊起,我们进入Xshell ...
- Echarts特效散点图全解
mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...
- 环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)
长度为n的环状串有n种表示法,分别为从某 个位置开始顺时针得到.例如,图3-4的环状串 有10种表示: CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等. 在这些表示法中,字典序最小 ...
- Linux自动化之基于http的pxe安装服务
PXE: Preboot Excution Environment 预启动执行环境 Intel公司研发 基于Client/Server的网络模式,支持远程主机通过网络从远端服务 ...
- python爬虫14 | 就这么说吧,如果你不懂python多线程和线程池,那就去河边摸鱼!
你知道吗? 在我的心里 你是多么的重要 就像 恩 请允许我来一段 freestyle 你们准备好了妹油 你看 这个碗 它又大又圆 就像 这条面 它又长又宽 你们 在这里 看文章 觉得 很开心 就像 我 ...
- Keil-MDK编译完成后代码大小
Code 代表执行的代码,程序中所有的函数都位于此处. RO-data 代表只读数据,程序中所定义的全局常量数据和字符串都位于此处. RW-data 代表已初始化的读写数据,程序中定义并且初始化的全局 ...
- 8.1.3 Row对象
假设数据以下面的方式创建并插入数据: import sqlite3 conn = sqlite3.connect(r'D:\test.db') c = conn.cursor() c.execute( ...