【51nod】2026 Gcd and Lcm
题解
话说LOJ说我今天宜学数论= =看到小迪学了杜教筛去蹭了一波小迪做的题
标解的杜教筛的函数不懂啊,怎么推的毫无思路= =
所以写了个复杂度稍微高一点的??
首先,我们发现f是个积性函数,那么我们就有……
\(\prod_{i = 1}^{k}f(p_{i}^{a_{i}})\)
我们发现,对于每个质因子,gcd是取较小值,lcm取较大值
\(f(lcm(x,y)) * f(gcd(x,y)) = \prod_{i = 1}^{k} f(p_{i}^{max(a_{i},b_{i}) + min(a_{i},b_{i})})\)
\(max(a,b) + min(a,b) = a + b\)
那么就有
\(f(lcm(x,y)) * f(gcd(x,y)) = \prod_{i = 1}^{k} f(p_{i}^{max(a_{i},b_{i}) + min(a_{i},b_{i})}) = f(x) * f(y)\)
所以我们只要求出\([\sum_{i = 1}^{n} f(i)]^2\)就是答案了!
怎么求呢
\(S(n) = \sum_{i = 1}^{n}\sum_{d | i} \mu(d)\cdot d\)
\(S(n) = \sum_{d = 1}^{n}\sum_{d | i} \mu(d)\cdot d\)
\(S(n) = \sum_{d = 1}^{n} \mu(d)\cdot d \cdot \lfloor \frac{n}{d} \rfloor\)
我们可以数论分块处理\(\lfloor \frac{n}{d} \rfloor\)
那么我们考虑计算\(\sum_{d = 1}^{n} \mu(d)\cdot d\)
我们发现这个函数卷上一个\(Id(x)\)等于\(e\)
\(\sum_{i = 1}^{n} [i = 1] = \sum_{i = 1}^{n} \sum_{d |i} \mu(d) \cdot d \cdot \frac{i}{d} = \sum_{k = 1}^{n} k \sum_{d}^{\frac{n}{k}} \mu(d) \cdot d\)
所以最后就是
\(S(n) = 1 - \sum_{i = 2}^{n} S(\lfloor \frac{n}{i}\rfloor)\)
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
//#define ivorysi
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mo 974711
#define MAXN 1000000
#define RG register
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
const int MOD = 1000000007;
struct node {
int x,v,next;
}E[2000006];
int head[mo + 5],sumE;
int prime[MAXN + 5],tot,S[MAXN + 5],mu[MAXN + 5];
bool nonprime[MAXN + 5];
int inc(int a,int b) {
a = a + b;
if(a >= MOD) a -= MOD;
return a;
}
void add(int u,int x,int v) {
E[++sumE].x = x;E[sumE].v = v;E[sumE].next = head[u];
head[u] = sumE;
}
void Insert(int x,int v) {
add(x % mo,x,v);
}
int Query(int x) {
int u = x % mo;
for(int i = head[u] ; i ; i = E[i].next) {
if(E[i].x == x) return E[i].v;
}
return -1;
}
int f(int x) {
if(x <= MAXN) return S[x];
int c = Query(x);
if(c != -1) return c;
int res = 0;
for(int i = 2 ; i <= x ; ++i) {
int r = x / (x / i);
res = inc(res,1LL * (r - i + 1) * (r + i) / 2 % MOD * f(x / i) % MOD);
i = r;
}
res = inc(1,MOD - res);
Insert(x,res);
return res;
}
void Solve() {
mu[1] = 1;S[1] = 1;
for(int i = 2 ; i <= MAXN ; ++i) {
if(!nonprime[i]) {
mu[i] = -1;
prime[++tot] = i;
}
for(int j = 1 ; j <= tot ; ++j) {
if(prime[j] > MAXN / i) break;
nonprime[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
else mu[i * prime[j]] = -mu[i];
}
S[i] = (S[i - 1] + mu[i] * i + MOD) % MOD;
}
read(N);
int res = 0;
for(int i = 1 ; i <= N ; ++i) {
int r = N / (N / i);
res = inc(1LL * (f(r) + MOD - f(i - 1)) * (N / i) % MOD,res);
i = r;
}
out(1LL * res * res % MOD);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
【51nod】2026 Gcd and Lcm的更多相关文章
- 【51nod】1594 Gcd and Phi
题解 跟随小迪学姐的步伐,学习一下数论 小迪学姐太巨了! 这道题的式子很好推嘛 \(\sum_{i = 1}^{n} \sum_{j = 1}^{n} \sum_{d|\phi(i),\phi(j)} ...
- 【51nod】1602 矩阵方程的解
[51nod]1602 矩阵方程的解 这个行向量显然就是莫比乌斯函数啦,好蠢的隐藏方法= = 然后我们尝试二分,二分的话要求一个这个东西 \(H(n) = \sum_{i = 1}^{n} \mu(i ...
- 【51nod】1634 刚体图
[51nod]1634 刚体图 给一个左边n个点右边m个点二分图求合法的连通图个数,每条边选了之后会带来价值乘2的贡献 类似城市规划那道题的计数 设\(g[i][j]\)为左边\(i\)个点,右边\( ...
- 【51nod】1407 与与与与
[51nod]1407 与与与与 设\(f(x)\) 为\(A_{i} \& x == x\)的\(A_{i}\)的个数 设\(g(x)\)为\(x\)里1的个数 \(\sum_{i = 0} ...
- 【51nod】1776 路径计数
[51nod]1776 路径计数 我们先把前两种数给排好,排好之后会有\(a + b + 1\)个空隙可以填数,我们计算有\(k\)个空隙两端都是相同字母的方案数 可以用枚举把第二种数分成几段插进去来 ...
- 【51nod】2622 围绕着我们的圆环
[51nod] 2622 围绕着我们的圆环 kcz出的一道比赛题 第一次写带修改的线性基 ps:我觉得我计数计的好麻烦 首先是这个可以认为第二个矩阵是\(q\)个\(s\)位数,如果这\(q\)个数的 ...
- 【51nod】2564 格子染色
[51nod]2564 格子染色 这道题原来是网络流-- 感觉我网络流水平不行-- 这种只有两种选择的可以源点向该点连一条容量为b的边,该点向汇点连一条容量为w的边,如果割掉了b证明选w,如果割掉了w ...
- 【51nod】2027 期望问题
[51nod]2027 期望问题 %%%zsy 看不懂题解的垃圾选手在zsy大佬的讲解下终于知道了这道题咋做-- 先把所有\(a\)从大到小排序 设\(f_{i}\)为前\(i\)个数组成的排列的值, ...
- 【51nod】2591 最终讨伐
[51nod]2591 最终讨伐 敲51nod是啥评测机啊,好几次都编译超时然后同一份代码莫名奇妙在众多0ms中忽然超时 这道题很简单就是\(M\)名既被诅咒也有石头的人,要么就把石头给没有石头被诅咒 ...
随机推荐
- 2017 Multi-University Training Contest - 1
hdu 6033 pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inc ...
- 题解 P3153 【[CQOI2009]跳舞】
P3153 [CQOI2009]跳舞 题目描述 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢 ...
- VisualSVN 5.1.4破解
1. 备份visualSVNbin目录 2. 打开VS命令提示工具,反编译VisualSVN.Core.L.dll 运行命令 ildasam "VisualSVN安装目录\bin\Visua ...
- OpenCV---图像梯度
图像梯度 推文:[OpenCV入门教程之十二]OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑 图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个 ...
- JS中的匿名函数自执行、函数声明与函数表达式
先看一段jQuery源码中匿名函数自执行的例子: (function( window, undefined ) { // jquery code })(window); 另外一种常见的写法: +fun ...
- Eclipse错误笔记!
1.ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error ...
- Google Map API 应用实例说明
目录 Google Map API 1基础知识 1.1 Google 地图 API 概念 1.2 Google 地图的"Hello, World" 1.2.1 加载 Google ...
- Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- 周末发福利了!26个免费的HTML5模版
本期文章我们为大家搜集了很多专业且高质量的HTML5模版,而且还是免费的呦.如果你对编码很熟悉,那么从这些网站里你可以学到很多新技能.来这些国际范的案例中挑选您喜欢的模版学习起来吧:) Zeences ...
- JSDom
什么是Dom? 1.简介 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.Document Object Model的历史可 ...