原文链接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——————

题意概括

给定正整数n,m。求
 

题解

博主越来越懒了。

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 数论 快速幂 莫比乌斯反演的更多相关文章

  1. BZOJ3560 DZY Loves Math V 数论 快速幂

    原文链接http://www.cnblogs.com/zhouzhendong/p/8111725.html UPD(2018-03-26):蒟蒻回来重新学数论了.更新了题解和代码.之前的怼到后面去了 ...

  2. [BZOJ3561] DZY Loves Math VI

    (14.10.28改) 本来只想写BZOJ3739:DZY Loves Math VIII的,不过因为和VI有关系,而且也没别人写过VI的题解,那么写下. 不过我还不会插公式…… http://www ...

  3. BZOJ3561 DZY Loves Math VI 莫比乌斯反演

    传送门 看到\(gcd\)相关先推式子(默认\(N \leq M\)): \(\begin{align*} \sum\limits_{i=1}^N \sum\limits_{j=1}^M (lcm(i ...

  4. BZOJ3561 DZY Loves Math VI 【莫比乌斯反演】

    题目 给定正整数n,m.求 输入格式 一行两个整数n,m. 输出格式 一个整数,为答案模1000000007后的值. 输入样例 5 4 输出样例 424 提示 数据规模: 1<=n,m<= ...

  5. 【BZOJ3561】DZY Loves Math VI (数论)

    [BZOJ3561]DZY Loves Math VI (数论) 题面 BZOJ 题解 \[\begin{aligned} ans&=\sum_{i=1}^n\sum_{j=1}^m\sum_ ...

  6. 【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 ...

  7. 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 ...

  8. 【bzoj3561】DZY Loves Math VI 莫比乌斯反演

    题目描述 给定正整数n,m.求   输入 一行两个整数n,m. 输出 一个整数,为答案模1000000007后的值. 样例输入 5 4 样例输出 424 题解 莫比乌斯反演 (为了方便,以下公式默认$ ...

  9. 【BZOJ】3561: DZY Loves Math VI

    题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...

随机推荐

  1. MVC、MVP、MVVM模式

    MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...

  2. Android apk动态加载机制

    参考链接:http://blog.csdn.net/singwhatiwanna/article/details/22597587

  3. SQL Server代码段

    1.cast和convert ' as int) -- 123 ') -- 123 select CAST(123.4 as int) -- 123 select CONVERT(int, 123.4 ...

  4. SSH localhost免密不成功 + 集群状态显示Configured Capacity: 0 (0 KB)

    前一天运行hadoop一切安好,今天重新运行出现BUG.下面对遇到的bug.产生原因以及解决方法进行一下简单总结记录. [bug1]用ssh localhost免密登录时提示要输入密码. 原因分析:之 ...

  5. 前端 ---BOM的介绍

    BOM的介绍 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象模型,操作网页上的 ...

  6. Laravel 5.2 错误-----ReflectionException in compiled.php line 8572: Class App\Http\Controllers\Apih5\ZhaoshangController does not exist

    测试的时候,报错了!想不到找了半天的问题,居然是个低级错误. <?php namespace App\Http\Controllers\Apih5; use Illuminate\Http\Re ...

  7. js获取当前星期几

    使用Date对象的getDay方法可以获取当前日期的星期数. getDay() 方法可返回表示星期的某一天的数字. 示例: var date = new Date(); alert(date.getD ...

  8. Python-多表关联 外键 级联

    分表为什么分表 多表关联多表关系 ****** 表之间的关系 为什么要分表 多对一 一个外键 多对多 一个中间表 两个外键 一对一 一个外键加一个唯一约束外键约束 ****** foreign key ...

  9. IntellJ IDEA下写JUnit

     安装配置JUnit  File->Settings->Plugins->Browse Repositories->在右侧搜索框输入"junit"-> ...

  10. sleep()和wait()的区别及wait方法的一点注意事项

    一.查看API sleep是Thread类的方法,导致此线程暂停执行指定时间,给其他线程执行机会,但是依然保持着监控状态,过了指定时间会自动恢复,调用sleep方法不会释放锁对象. 当调用sleep方 ...