BZOJ3561 DZY Loves Math VI 数论 快速幂 莫比乌斯反演
原文链接http://www.cnblogs.com/zhouzhendong/p/8116330.html
UPD(2018-03-26):回来重新学数论啦。之前的博客版面放在更新之后的后面。
题目传送门 - BZOJ3561
题意概括
给出$n,m$,求$\Large\sum_{i=1}^n\sum_{j=1}^m lcm(i,j)^{\gcd(i, j)}$。
$1\leq n,m\leq 500000$
题解
先推式子:(假设$n\leq m$)
$$\sum_{i=1}^n\sum_{j=1}^m lcm(i,j)^{\gcd(i, j)}\\=\sum_{d=1}^{n}\sum_{i=1}^{\left\lfloor\frac nd\right\rfloor}\sum_{j=1}^{\left\lfloor\frac md\right\rfloor}(ijd)^d\cdot[\gcd(i,j)=1]\\=\sum_{d=1}^{n}\sum_{i=1}^{\left\lfloor\frac nd\right\rfloor}\sum_{j=1}^{\left\lfloor\frac md\right\rfloor}(ijd)^d\cdot\sum_{p|i,p|j}\mu(p)\\=\sum_{d=1}^{n}d^{d}\sum_{p=1}^{\left\lfloor\frac nd\right\rfloor}\mu(p)\sum_{i=1}^{\left\lfloor\frac{n}{pd}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{pd}\right\rfloor}(ijp^2)^d\\=\sum_{d=1}^{n}d^{d}\sum_{p=1}^{\left\lfloor\frac nd\right\rfloor}\mu(p)p^{2d}\sum_{i=1}^{\left\lfloor\frac {n}{pd}\right\rfloor}i^d\sum_{j=1}^{\left\lfloor\frac{m}{pd}\right\rfloor}j^d$$
然后发现对于$d^d$可以直接快速幂。对于某一个$d$,要枚举的$p$有$O(\frac nd)$个,对于后面的一堆数的幂和,只要前缀和预处理,要处理的个数也是$O(\frac md)$的。所以总复杂度为$O(n \log n)$。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=500005;
const LL mod=1e9+7;
int n,m,prime[N],u[N],pcnt=0;
bool f[N];
void init(int n){
memset(f,true,sizeof f);
f[0]=f[1]=0,u[1]=1;
for (int i=2;i<=n;i++){
if (f[i])
prime[++pcnt]=i,u[i]=-1;
for (int j=1;j<=pcnt&&i*prime[j]<=n;j++){
f[i*prime[j]]=0;
if (i%prime[j])
u[i*prime[j]]=-u[i];
else {
u[i*prime[j]]=0;
break;
}
}
}
}
LL Pow(LL x,LL y){
if (!y)
return 1LL;
LL xx=Pow(x,y/2);
xx=xx*xx%mod;
if (y&1LL)
xx=xx*x%mod;
return xx;
}
LL pows[N],sum[N];
int main(){
scanf("%d%d",&n,&m);
if (n>m)
swap(n,m);
init(n);
for (int i=1;i<=m;i++)
pows[i]=1;
LL ans=0;
for (int d=1;d<=n;d++){
LL now=0;
sum[0]=0;
for (int i=1;i<=m/d;i++)
pows[i]=pows[i]*i%mod,sum[i]=(sum[i-1]+pows[i])%mod;
for (int p=1;p<=n/d;p++)
now=(now+u[p]*pows[p]*pows[p]%mod*sum[n/p/d]%mod*sum[m/p/d])%mod;
now=(now%mod+mod)%mod;
ans=(ans+Pow(d,d)*now)%mod;
}
printf("%lld",ans);
return 0;
}
——————old——————
题意概括

题解
博主越来越懒了。
http://blog.csdn.net/lych_cys/article/details/50721642?locationNum=1&fps=1
代码
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=500005;
const LL mod=1e9+7;
LL n,m,u[N],prime[N],pcnt,v[N],sum[N];
bool isprime[N];
LL Pow(LL x,LL y){
if (y==0)
return 1LL;
LL xx=Pow(x,y/2);
xx=xx*xx%mod;
if (y&1LL)
xx=xx*x%mod;
return xx;
}
void Get_Mobius(){
memset(isprime,true,sizeof isprime);
isprime[0]=isprime[1]=pcnt=0;
u[1]=1;
for (LL i=2;i<=n;i++){
if (isprime[i])
u[i]=-1,prime[++pcnt]=i;
for (LL j=1;j<=pcnt&&i*prime[j]<=n;j++){
isprime[i*prime[j]]=0;
if (i%prime[j])
u[i*prime[j]]=-u[i];
else {
u[i*prime[j]]=0;
break;
}
}
}
}
int main(){
scanf("%lld%lld",&n,&m);
if (n<m)
swap(n,m);
Get_Mobius();
for (int i=1;i<=n;i++)
v[i]=1;
LL ans=0;
for (LL d=1;d<=m;d++){
sum[0]=0;
for (LL i=1;i<=(LL)(n/d);i++)
v[i]=v[i]*i%mod,sum[i]=(v[i]+sum[i-1])%mod;
LL res=0;
for (LL p=1;p<=(LL)(m/d);p++)
res=(res+v[p]*v[p]%mod*u[p]*sum[n/d/p]%mod*sum[m/d/p]%mod+mod)%mod;
ans=(ans+res*Pow(d,d))%mod;
}
printf("%lld",ans);
return 0;
}
BZOJ3561 DZY Loves Math VI 数论 快速幂 莫比乌斯反演的更多相关文章
- BZOJ3560 DZY Loves Math V 数论 快速幂
原文链接http://www.cnblogs.com/zhouzhendong/p/8111725.html UPD(2018-03-26):蒟蒻回来重新学数论了.更新了题解和代码.之前的怼到后面去了 ...
- [BZOJ3561] DZY Loves Math VI
(14.10.28改) 本来只想写BZOJ3739:DZY Loves Math VIII的,不过因为和VI有关系,而且也没别人写过VI的题解,那么写下. 不过我还不会插公式…… http://www ...
- BZOJ3561 DZY Loves Math VI 莫比乌斯反演
传送门 看到\(gcd\)相关先推式子(默认\(N \leq M\)): \(\begin{align*} \sum\limits_{i=1}^N \sum\limits_{j=1}^M (lcm(i ...
- BZOJ3561 DZY Loves Math VI 【莫比乌斯反演】
题目 给定正整数n,m.求 输入格式 一行两个整数n,m. 输出格式 一个整数,为答案模1000000007后的值. 输入样例 5 4 输出样例 424 提示 数据规模: 1<=n,m<= ...
- 【BZOJ3561】DZY Loves Math VI (数论)
[BZOJ3561]DZY Loves Math VI (数论) 题面 BZOJ 题解 \[\begin{aligned} ans&=\sum_{i=1}^n\sum_{j=1}^m\sum_ ...
- 【BZOJ 3561】 3561: DZY Loves Math VI (莫比乌斯,均摊log)
3561: DZY Loves Math VI Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 205 Solved: 141 Description ...
- BZOJ 3561 DZY Loves Math VI
BZOJ 3561 DZY Loves Math VI 求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\text{lcm}(i,j)^{\gcd(i,j)}\),钦定\(n\leq m ...
- 【bzoj3561】DZY Loves Math VI 莫比乌斯反演
题目描述 给定正整数n,m.求 输入 一行两个整数n,m. 输出 一个整数,为答案模1000000007后的值. 样例输入 5 4 样例输出 424 题解 莫比乌斯反演 (为了方便,以下公式默认$ ...
- 【BZOJ】3561: DZY Loves Math VI
题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...
随机推荐
- postgresql 触发器 更新操作
1 前言 功能需求:当一张表格某个字段变化,另一张表某个字段写入该值 2 代码 CREATE OR REPLACE FUNCTION "public"."synStatu ...
- [转]MySQL常用Json函数和MySQL常用字符串函数
MySQL常用Json函数:https://www.cnblogs.com/waterystone/p/5626098.html MySQL常用字符串函数:https://www.cnblogs.co ...
- 基于官方mysql镜像构建自己的mysql镜像
参考文章:https://www.jb51.net/article/115422.htm搭建步骤 1.首先创建Dckerfile: 1 2 3 4 5 6 7 8 9 10 11 12 FROM my ...
- iOS weak 内存释放问题
我们都知道weak 关键字可以解决内存不释放问题,但是使用上有些讲究. 看代码: import UIKit var str = "Hello, playground" class ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
- CentOS 7 连接ssh方法
自己在VMware中装了CentOS 6.3,然后主机(或者说xshell)与里面的虚拟机连不通,刚学习,一头雾水,查了半天,也不知道怎么弄. 经指点,找到下面这篇文章,感谢博主: http://bl ...
- swift 学习- 25 -- 协议 02
// 通过扩展添加协议一致性 // 即便无法修改源代码, 依然可以通过扩展 令已有类型遵循并符合协议, 扩展可以为已有类型添加属性, 方法, 下标 以及构造器, 因此可以符合协议中的相应要求 // 注 ...
- JS 实现的浏览器系统通知 iNotify.js
注:本分非原创:信息来源 oschina 授权协议:MIT 开发语言:JavaScript 操作系统:跨平台 软件作者:同一种调调 iNotify.js 详细介绍 JS 实现浏览器的 title 闪烁 ...
- SpringData使用与整合
SpringData 整合源码:链接: https://pan.baidu.com/s/1_dDEEJoqaBTfXs2ZWsvKvA 提取码: cp6s(jar包自行寻找) author:Simpl ...
- 使用Spring配置数据源JdbcTemplate
c3p0作为演示 1.编写资源文件(db.properties) jdbc.user=root jdbc.password=root jdbc.jdbcUrl=jdbc:mysql://localho ...