题目描述

  给你\(n,p\),求

\[\sum_{i=1}^n\sum_{j=1}^i\sum_{k=1}^i\gcd(i,j,k)\mod p
\]

  \(n\leq {10}^9\)

题解

\[\begin{align}
ans&=\sum_{i=1}^n\sum_{j=1}^i\sum_{k=1}^i\gcd(i,j,k)\\
&=\sum_{i=1}^n\sum_{j=1}^i\sum_{k=1}^i\sum_{d|\gcd(i,j,k)}\varphi(d)\\
&=\sum_{d=1}^n\varphi(d)\sum_{d|i}^n\sum_{d|j}^i\sum_{d|k}^i1\\
&=\sum_{i=1}^n\varphi(i)S_2(\lfloor\frac{n}{i}\rfloor)\\
\end{align}
\]

  其中\(S_2(n)=\sum_{i=1}^ni^2=\frac{n(n+1)(2n+1)}{6}\)

  直接杜教筛。

  时间复杂度:\(O(n^\frac{2}{3})\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll p;
int n;
ll fp(ll a,ll b)
{
ll s=1;
for(;b;b>>=1,a=a*a%p)
if(b&1)
s=s*a%p;
return s;
}
ll inv6;
ll S(ll n)
{
return n*(n+1)%p*(2*n+1)%p*inv6%p;
}
ll g(ll x)
{
return S(n/x);
}
int b[10000010];
int s[10000010];
int pri[1000010];
int cnt;
int b2[10000010];
ll s2[10000010];
const int maxn=10000000;
ll f(ll x)
{
if(x<=maxn)
return s[x];
if(b2[n/x])
return s2[n/x];
b2[n/x]=1;
ll res=x*(x+1)/2%p;
for(int i=2,j;i<=x;i=j+1)
{
j=x/(x/i);
res=(res-(j-i+1)*f(x/i))%p;
}
return s2[n/x]=res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
#endif
scanf("%d%lld",&n,&p);
inv6=fp(6,p-2);
s[1]=1;
for(int i=2;i<=maxn;i++)
{
if(!b[i])
{
pri[++cnt]=i;
s[i]=i-1;
}
for(int j=1;j<=cnt&&i*pri[j]<=maxn;j++)
{
b[i*pri[j]]=1;
if(i%pri[j]==0)
{
s[i*pri[j]]=s[i]*pri[j];
break;
}
s[i*pri[j]]=s[i]*s[pri[j]];
}
}
for(int i=2;i<=maxn;i++)
s[i]=(s[i-1]+s[i])%p;
ll ans=0;
memset(b2,0,sizeof b2);
for(int i=1,j;i<=n;i=j+1)
{
j=n/(n/i);
ans=(ans+(f(j)-f(i-1))*g(i))%p;
}
ans=(ans+p)%p;
printf("%lld\n",ans);
return 0;
}

最新文章

  1. 【原创】自己动手写工具----XSmartNote [Beta 2.0]
  2. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
  3. atitit.web 推送实现方案集合
  4. 反人类的MyEclipse之-MyEclipse代码自动补全
  5. MVC返回图片
  6. 一些纯css3写的公司logo
  7. BLOCK专题
  8. 面试题总结之JAVA
  9. css清除浮动的几种方法整理
  10. angularjs服务常见用法
  11. css学习笔记二
  12. awstats 日志分析
  13. 条形码--JsBarcode
  14. 总结一下现在关于Design Support Library的几个博客
  15. 转 - mybatis中${}、 #{}区别及应用场景
  16. AS报:Manifest merger failed with multiple errors, see logs
  17. sql server 高级查询
  18. P - Air Raid
  19. wps表格开发C#
  20. show出相应单据列表

热门文章

  1. 针对2017年淘宝开放平台应用整改被封停或强制入塔政策实现不入塔不模糊正常调用API的解决方案
  2. logstash安装及基础入门
  3. Beta阶段团队成员贡献分分配规则
  4. H5上传图片之canvas
  5. 使用mysql,sql语言删除冗余信息
  6. Column &#39;parent_id&#39; specified twice
  7. EF内容记录_EF连接Mysql版本问题
  8. [转帖]Ipvsadm参数详解(常用命令)
  9. centOS7搭建NFS服务器
  10. select、poll、epoll之间的区别(搜狗面试)