题目

已知 \(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. [转帖]glib gslibc libc 的关系与区别

    https://blog.csdn.net/Com_ma/article/details/78692092 [glibc 和 libc] glibc 和 libc 都是 Linux 下的 C 函数库. ...

  2. 【C++】A trick I learned:put boilerplate code into constructor of a struct

    I learned this trick from hitonanode's submission on AtCoder. The trick is like struct fast_ios { fa ...

  3. 在Docker中部署ASP.NET Core 2.2

    ⒈新建一个ASP.NET Core2.2 Web程序 因为Windows的Docker和Linux的Docker有所不同,本次测试采用的是Linux的Docker,因此没有勾选启用Docker支持. ...

  4. myeclipse中jpa的安装以及jpa reverse engining

    myeclipse中jpa的安装以及jpa reverse engining 安装 右击 Configure Facets, Install JPA jpa reverse engining 1.db ...

  5. HYSBZ 1797 Mincut 最小割

    Descrption A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路.设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站, ...

  6. NOIP2017普及组比赛总结

    期中考总结&NOIP2017总结 2017年11月11日,我第二次参加NOIP普及组复赛.上一年,我的得分是250分,只拿到了二等奖.我便把目标定为拿到一等奖,考到300分以上. 早上8点多, ...

  7. centos7 上pip install mysqlclient的时候报错OSError: mysql_config not found,

    yum install mysql-devel gcc gcc-devel python-devel

  8. Codeforces 1201D. Treasure Hunting

    传送门 看一眼感觉就是 $dp$,但是似乎状态太多了 考虑推推性质 首先每到一行都要把所有宝藏都走到,那么一定会走到最左边的和最右边的宝藏 注意到一旦走完所有宝藏时肯定是在最左边或者最右边的宝藏位置 ...

  9. Java集合框架中的元素

    之前有一篇笔记,讲的是集合和泛型,这几天看Java集合中几个接口的文档,思绪非常混乱,直到看到Oracle的“The Collections Framwork”的页面,条理才清晰些,现在进行整理. 一 ...

  10. 在Powershell中使用Group-Object和-GroupBy

    使用Group-Object(group)按组统计 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group ...