【51nod 2026】Gcd and Lcm
题目
已知 \(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)\)
\]
\]
现在考虑如何求\(\sum_{i=1}^{n}f(i)\)
\]
\]
\]
设\(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])
于是
\]
设\(T=\lfloor \dfrac{i}{d} \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的更多相关文章
- 【Aizu - 0005 】GCD and LCM
GCD and LCM Descriptions: Write a program which computes the greatest common divisor (GCD) and the l ...
- 51NOD 2026:Gcd and Lcm——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...
- 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)
[51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...
- 【51Nod 1769】Clarke and math2
[51Nod 1769]Clarke and math2 题面 51Nod 题解 对于一个数论函数\(f\),\(\sum_{d|n}f(d)=(f\times 1)(n)\). 其实题目就是要求\( ...
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【BZOJ 2818】 GCD
[题目链接] 点击打开链接 [算法] 线性筛出不大于N的所有素数,枚举gcd(x,y)(设为p),问题转化为求(x,y)=p的个数 设x=x'p, y=y'p,那么有(x,y)=1且 ...
- 【Codeforces 582A】 GCD Table
[题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数. G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...
- 【HDU 5382】 GCD?LCM! (数论、积性函数)
GCD?LCM! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 【51nod2026】Gcd and Lcm(杜教筛)
题目传送门:51nod 我们可以先观察一下这个$f(x)=\sum_{d|x}\mu(d) \cdot d$. 首先它是个积性函数,并且$f(p^k)=1-p \ (k>0)$,这说明函数$f( ...
随机推荐
- Ciso三层交换 上vlan间互通, 端口映射到vlan
路由器2911配置: !hostname router interface GigabitEthernet0/0 ip address 10.0.0.2 255.0.0.0 ip nat outsid ...
- 让mysql的id字段变成表的主键
1.#已经加主键 desc szdj.sys_message_user;alter table sys_message_user add constraint pk_mess_user primary ...
- MSF魔鬼训练营-3.5.3 MSF中常用的关于数据库的命令
渗透测试数据库用来保存渗透测试过程中获取的各种数据,很多时候你重启了kali发现连接不上数据库了,其实就是因为这个服务没开 MSF所使用的数据库时postgresql root@kali:/# ser ...
- PostgreSQL unlogged表
PostgreSQL有一种介于正常表和临时表之间的类型表,称之为unlogged表,在该表新建的索引也属于unlogged,该表在写入数据时候并不将数据写入到持久的write-ahead log文件中 ...
- [转帖]2015年时的新闻:Debian GNU/Hurd 2015 发布
Debian GNU/Hurd 2015 发布 oschina 发布于 2015年04月30日 https://www.oschina.net/news/62004/debian-gnu-hurd-2 ...
- postman测试webservice接口
- java使用顺序数组实现二叉树
顺序数组实现二叉树 实现原理 对于下标为index的节点其满足 1.左孩子节点的下标为2index+1 2.右孩子节点的下标为2index+2 代码实现 package tree; public cl ...
- MySQL中文正常而mybatis查询出现乱码的解决方案
解决方案是在spring-mvc.xml文件中,加入 <mvc:annotation-driven> <mvc:message-converters> <bean cla ...
- java中内部类
package com.xt.instanceoftest; import com.xt.instanceoftest.Body.Heart; public class StaticInnerClas ...
- js制作秒表
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...