【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 ...
随机推荐
- 通过offset值的设置使html元素对齐
今天是我第一次写这个随笔,为了记录我发现的一个jquery的offset的值的问题. 这个offset的值会因为页面标签是否处于隐藏状态而表现出不同的值,隐藏状态时,offset的值是相对于直接父亲的 ...
- Python 之lxml解析库
一.XPath常用规则 二.解析html文件 from lxml import etree # 读取HTML文件进行解析 def parse_html_file(): html = etree.par ...
- 关于Staltstack
saltstate服务搭建: cat /etc/hosts(master和minion都添加) 127.0.0.1 localhost localhost.localdomain localhos ...
- 【Flutter学习】基本组件之AppBar顶部导航栏
一,概述 AppBar 显示在app的顶部.AppBar包含5大部分,如下图: 二,构造函数及参数含义 构造函数 AppBar({ Key key, this.leading, //在标题前面显示的一 ...
- Linux内核源码特殊用法
崇拜并且转载的: http://ilinuxkernel.com/files/5/Linux_Kernel_Source_Code.htm Linux内核源码特殊用法 1 前言 Linux内核源码主要 ...
- Django CBV视图解决csrf认证
urls.py from django.conf.urls import url from appxx import views urlpatterns = [ url(r"^$" ...
- java归并排序
代码如下: public class MergeSort { public static void mergeSort(DataWrap [] data) { sort(data , 0 , data ...
- 15.most_fields策略进行cross-fields search
主要知识点: cross-fields 的使用场景 cross-fields 使用方法 cross-fields 的缺点 一.cross-fields 的使用场景 cross-fiel ...
- 【模板】可持久化Treap
洛谷3835 #include<cstdio> #include<algorithm> #include<cstdlib> #define ls (a[u].l) ...
- Codeforces Hello 2018 C - Party Lemonade
传送门:http://codeforces.com/contest/913/problem/C 有n类物品,第i(i=0,1,2,...,n-1)类物品的价值为2i,花费为ci.任意选择物品,使得总价 ...