题目

已知 \(f(x)=\sum_{d|x}μ(d)∗d\)

现在请求出下面式子的值

\(\sum_{i=1}^{n}\sum_{j=1}^{n}f(gcd(i,j))∗f(lcm(i,j))\)

由于值可能过大所以请对 10^9+7 取模

\(n≤10^9\)

分析

\(f\)为积性函数,

因为\(lcm(i,j)\)、\(gcd(i,j)\)的任意一个质因子的指数和\(i\)、\(j\)其中一个的相同。

对于每个质因子分开考虑, \(f(gcd(i,j))f(lcm(i,j))=f(i)f(j)\)

\[ans=\sum_{i=1}^{n}\sum_{j=1}^{n}f(i)f(j)
\]

\[=(\sum_{i=1}^{n}f(i))^2
\]

现在考虑如何求\(\sum_{i=1}^{n}f(i)\)

\[=\sum_{i=1}^{n}\sum_{d|i}μ(d)∗d
\]

\[=\sum_{i=1}^{n}\sum_{d=1}^{\lfloor \frac{n}{i} \rfloor}μ(d)∗d
\]

\[=\sum_{d=1}^{n}μ(d)∗d\lfloor \frac{n}{d} \rfloor
\]

设\(F(d)=μ(d)∗d,S(n)=\sum_{i=1}^{n}F(i)\)

发现\(F*id=e\)(因为\(\sum_{d|n}μ(d)∗d*\lfloor \dfrac{n}{d} \rfloor\)=[n=1])

于是

\[1=\sum_{i=1}^{n}\sum_{d|i}μ(d)∗d*\lfloor \dfrac{i}{d} \rfloor
\]

设\(T=\lfloor \dfrac{i}{d} \rfloor\)

\[=\sum_{T=1}^{n}T\sum_{d|T}μ(d)∗d
\]

\[=\sum_{T=1}^{n}T\sum_{d=1}^{\lfloor \frac{n}{T} \rfloor}μ(d)∗d
\]

\[=\sum_{T=1}^{n}TS(\lfloor \frac{n}{T} \rfloor)
\]

于是

\[S(n)=1-\sum_{T=2}^{n}TS(\lfloor \frac{n}{T} \rfloor)
\]

杜教筛一下.

\(ans=S(n)^2\)

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
const int maxlongint=2147483647;
const long long mo=1e9+7;
const int lim=1e5+7;
const int N=10000005;
using namespace std;
#define sqr(x) (1ll*(x)*(x)%mo)
#define val(x,y) (1ll*(y-x+1)*(x+y)/2%mo)
int p[N],mu[N],n,ha[lim+5][2],s[N],ans;
bool bz[N];
int get(int v)
{
int x;
for(x=v%lim;ha[x][0] && ha[x][0]!=v;(++x)-=x>=lim?lim:0);
return x;
}
int S(int m)
{
if(m<=N-5) return s[m];
int pos=get(m);
if(ha[pos][0]) return ha[pos][1];
ha[pos][0]=m;
int la=0,sum=0;
for(int i=2;i<=m;i=la+1)
{
la=m/(m/i);
sum=(1ll*sum+1ll*val(i,la)*S(m/i))%mo;
}
return ha[pos][1]=(1-sum+mo)%mo;
}
int main()
{
freopen("2026.in","r",stdin);
//freopen("2026.out","w",stdout);
scanf("%d",&n);
mu[1]=s[1]=1;
for(int i=2;i<=N-5;i++)
{
if(!bz[i]) mu[p[++p[0]]=i]=-1;
s[i]=(s[i-1]+mu[i]*i+mo)%mo;
for(int j=1,k;j<=p[0] && (k=i*p[j])<=N-5;j++)
{
bz[k]=true;
if(i%p[j]==0) break;
mu[k]=-mu[i];
}
}
int la=1;
for(int i=1;i<=n;i=la+1)
{
la=n/(n/i);
ans=(1ll*ans+1ll*(S(la)-S(i-1)+mo)*(n/i))%mo;
}
printf("%lld",sqr(ans));
}

【51nod 2026】Gcd and Lcm的更多相关文章

  1. 【Aizu - 0005 】GCD and LCM

    GCD and LCM Descriptions: Write a program which computes the greatest common divisor (GCD) and the l ...

  2. 51NOD 2026:Gcd and Lcm——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...

  3. 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)

    [51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...

  4. 【51Nod 1769】Clarke and math2

    [51Nod 1769]Clarke and math2 题面 51Nod 题解 对于一个数论函数\(f\),\(\sum_{d|n}f(d)=(f\times 1)(n)\). 其实题目就是要求\( ...

  5. 【CF#338D】GCD Table

    [题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...

  6. 【BZOJ 2818】 GCD

    [题目链接] 点击打开链接 [算法] 线性筛出不大于N的所有素数,枚举gcd(x,y)(设为p),问题转化为求(x,y)=p的个数          设x=x'p, y=y'p,那么有(x,y)=1且 ...

  7. 【Codeforces 582A】 GCD Table

    [题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数.          G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...

  8. 【HDU 5382】 GCD?LCM! (数论、积性函数)

    GCD?LCM! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  9. 【51nod2026】Gcd and Lcm(杜教筛)

    题目传送门:51nod 我们可以先观察一下这个$f(x)=\sum_{d|x}\mu(d) \cdot d$. 首先它是个积性函数,并且$f(p^k)=1-p \ (k>0)$,这说明函数$f( ...

随机推荐

  1. 快速排序基本思想,递归写法,python和java编写快速排序

    1.基本思想 快速排序有很多种编写方法,递归和分递归,分而治之法属于非递归,比递归简单多了.在这不使用代码演示.下面我们来探讨一下快速排序的递归写法思想吧. 设要排序的数组是A[0]……A[N-1], ...

  2. Java中对比单继承与多继承的优劣,以及java的解决方案

    继承是一种面相对象的基本特征之一,但在具体语言的语法设计中有两种方式:单继承和多继承. 所谓多继承是指一个子类可以拥有多个父类:单继承则是一个子类只拥有一个父类. 单继承与多继承的优劣: 多继承优点在 ...

  3. gz文件的压缩和解压

    gz文件的压缩和解压 压缩: gzip filename 解压: gunzip filename.gz

  4. 最短meeting路线(树的直径)--牛客第四场(meeting)

    题意: 给你一棵树,树上有些点是有人的,问你选一个点,最短的(最远的那个人的距离)是多少. 思路: 其实就是树的直径,两遍dfs,dfs第二遍的时候遇到人就更新直径就行了,ans是/2,奇数的话+1. ...

  5. C++练习 | 模板与泛式编程练习(2)

    #include <iostream> #include <cmath> #include <cstring> #include <string> #i ...

  6. 如何用纯 CSS 创作出平滑的层叠海浪特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/JvmBdE 可交互视频教 ...

  7. 进程之multiprocessing模块代码篇

    这里再把之前的内容总结和补充一下: 并发和并行: 你在干坏事,来了一个电话,干完坏事再去接电话,说明你既不是并发也不是并行. 你在干坏事,来了一个电话,你接完电话接着干坏事,说明你支持并发 你在干坏事 ...

  8. python网络爬虫(9)构建基础爬虫思路

    目的意义 基础爬虫分5个模块,使用多个文件相互配合,实现一个相对完善的数据爬取方案,便于以后更完善的爬虫做准备. 这里目的是爬取200条百度百科信息,并生成一个html文件,存储爬取的站点,词条,解释 ...

  9. Entity Framework Core Relationship的学习笔记

    说明 此例筛选了感兴趣及常用部分 参考文献 https://docs.microsoft.com/en-us/ef/core/modeling/relationships One to Many Ma ...

  10. spring boot技术干货

    Spring Boot2 系列教程(一)纯 Java 搭建 SSM 项目 Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式 Spring Boot2 系列教程(三) ...