【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 ...
随机推荐
- Caffe2:python -m caffe2.python.operator_test.relu_op_test
1. 进行语句测试时候,出现问题, 设置环境变量CUDA_VISIBLE_DEVICES 参考: cuda设置指定可见方法 在/etc/profile文件或者-/.bashrc末尾添加以下行: exp ...
- Lazarus 1.44升级到1.6 UTF8处理发生变化了
首先这里真的要强调一下,由于Freepascal升级到3.0后,FPC的内部将整个代码处理由AnsiString改为了UTF8编码(RTL with default codepage UTF-8). ...
- Operation Queues 面向对象的封装
Operation Queues An operation queue is the Cocoa equivalent of a concurrent dispatch queue and is im ...
- bat配置JDK环境变量
最近总是部署服务器,总是要安装配置JDK,今天就想写个bat来配置JDK的环境变量,首先介绍点bat的小知识 @符号后面的命令不会显示在terminal上 例如: @echo运行时 隐藏命令(不在te ...
- lsof command not found 解决
有些centos 没有 lsof命令,需要安装 yum install lsof -y 使用: lsof -i:端口号
- .net 学习视频
http://www.iqiyi.com/a_19rrh9jx9p.html http://www.cnblogs.com/aarond/p/SQLDispatcher.html --读写分离 ht ...
- HDU-4055 Number String 动态规划 巧妙的转移
题目链接:https://cn.vjudge.net/problem/HDU-4055 题意 给一个序列相邻元素各个上升下降情况('I'上升'D'下降'?'随便),问有几种满足的排列. 例:ID 答: ...
- 洛谷 P1943 LocalMaxima_NOI导刊2009提高(1)
我们先考虑第i大数,比它大的数有(n-i)个,显然要使i是Local Maxima,比它大的数必须放在它后面,那么它是Local Maxima的期望是: 那么n个数中Local Maxima个数的期望 ...
- Linux 实用指令(4)
目录 实用指令 1.指定运行级别 2.切换到指定运行级别的指令 3.帮助指令 3.1man获得帮助信息 3.2help指令 4.文件目录类 4.1pwd指令 4.2 ls指令 4.3 cd指令 4.4 ...
- 06007_redis数据存储类型——hash
1.概述 (1)Redis中的Hash类型可以看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息,如Username.Password和Age等: ...