挺套路的题,然而一开始还是想错了……

$\sum_{i = 1}^{n}\sum_{j = 1}^{m}gcd(i, j) ^ {k} = \sum_{T = 1}^{min(n, m)}\left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor\sum_{d | T}\mu (\frac{T}{d}) * d^{k}$

我们设$h(i) =\sum_{d | T}\mu (\frac{T}{d}) * d^{k} $, 那么原式化为$ \sum_{T = 1}^{min(n, m)}\left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor h(i)$

我们做出$h(i)$的前缀和之后就可以整除分块了。

发现$h(i)$是一个积性函数,可以线性筛,具体筛法如下:

1、$h(1) = 1$

2、$h(p) = p^{k} - 1$($p$是一个质数)

3、考虑线性筛中的过程,$i * pri_{j}$被它的最小因子也就是$pri_{j}$筛掉,那么当$i$不是$pri_{j}$的倍数的时候,也就是说$h(i * pri_{j}) = h(i) * h(pri_{j})$。

  而当$pri_{j} | i$的时候,考虑分两步:($ i = \prod_{j = 1}^{m}p_{j}^{c_{j}}$)

  记$low_{i} =$ $i$的最小质因子被$i$包含的最大幂次积(即$p_{1} ^ {c_{1}}$)

  a、我们假设$low_{i}$不等于$i$,这句话等价于$i$不是一个质数的倍数,所以$\frac{i}{low_{i}}$ 与 $pri_{j} * low_{i}$互质,那么直接乘上就好了。

  b、当$low_{i}$等于$i$的时候,我们考虑一下$h(i)$函数的定义,展开式子之后发现$h(i) = pri_{j} ^ {mk} - pri_{j} ^ {(m - 1)k}$,而$h(i  * pri_{j}) = pri_{j} ^ {(m + 1)k} - pri_{j} ^ {mk}$,那么就相当于$h(i * pri_{j}) = h(i) * pri_{j}^{k}$。

时间复杂度$O(maxNlogmaxN + T\sqrt{n})$。

大时限题还感觉跑得挺快的。

Code:

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 5e6 + ;
const ll P = 1e9 + ; int testCase, pCnt = , pri[N];
ll k, h[N], low[N], sum[N];
bool np[N]; template <typename T>
inline void read(T &X) {
X = ;
char ch = ;
T op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline ll pow(ll a, ll b) {
ll res = 1LL;
for(; b > ; b >>= ) {
if(b & ) res = res * a % P;
a = a * a % P;
}
return res;
} inline void sieve() {
h[] = 1LL;
for(int i = ; i < N; i++) {
if(!np[i]) {
pri[++pCnt] = i;
low[i] = (ll)i;
h[i] =
(pow(i, k) - 1LL + P) % P;
}
for(int j = ; j <= pCnt && pri[j] * i < N; j++) {
np[i * pri[j]] = ;
if(i % pri[j] == ) {
low[i * pri[j]] = low[i] * pri[j];
if(low[i] == i) h[i * pri[j]] = h[i] * pow(pri[j], k) % P;
else h[i * pri[j]] = h[i / low[i]] * h[low[i] * pri[j]] % P;
break;
}
low[i * pri[j]] = pri[j];
h[i * pri[j]] = h[i] * h[pri[j]] % P;
}
} /* for(int i = 1; i < 20; i++)
printf("%lld ", phi[i]);
printf("\n");
for(int i = 1; i < 30; i++)
printf("%lld ", h[i]);
printf("\n"); */ /* for(int i = 1; i <= 20; i++)
printf("%lld ", h[i] % P); */ for(int i = ; i < N; i++)
sum[i] = (sum[i - ] + h[i] % P + P) % P;
} inline ll min(ll x, ll y) {
return x > y ? y : x;
} int main() {
read(testCase), read(k);
sieve();
for(ll n, m; testCase--; ) {
read(n), read(m);
ll ans = 0LL, rep = min(n, m);
for(ll l = , r; l <= rep; l = r + ) {
r = min((n / (n / l)), (m / (m / l)));
ans = (ans + (sum[r] - sum[l - ] + P) % P * (n / l) % P * (m / l) % P) % P;
}
printf("%lld\n", ans);
}
return ;
}

Luogu 4449 于神之怒加强版的更多相关文章

  1. 【BZOJ-4407】于神之怒加强版 莫比乌斯反演 + 线性筛

    4407: 于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 241  Solved: 119[Submit][Status][Discu ...

  2. 【BZOJ4407】于神之怒加强版(莫比乌斯反演)

    [BZOJ4407]于神之怒加强版(莫比乌斯反演) 题面 BZOJ 求: \[\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)^k\] 题解 根据惯用套路 把公约数提出来 \[\sum ...

  3. BZOJ 4407 于神之怒加强版 (莫比乌斯反演 + 分块)

    4407: 于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 1067  Solved: 494[Submit][Status][Disc ...

  4. bzoj 4407 于神之怒加强版 (反演+线性筛)

    于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 1184  Solved: 535[Submit][Status][Discuss] D ...

  5. 【BZOJ4407】于神之怒加强版 莫比乌斯反演

    [BZOJ4407]于神之怒加强版 Description 给下N,M,K.求 Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行, ...

  6. P4449 于神之怒加强版 (莫比乌斯反演)

    [题目链接] https://www.luogu.org/problemnew/show/P4449 给定n,m,k,计算 \(\sum_{i=1}^n \sum_{j=1}^m \mathrm{gc ...

  7. 洛谷 - P4449 - 于神之怒加强版 - 莫比乌斯反演

    https://www.luogu.org/problemnew/show/P4449 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{i=1}^{m} gcd(i, ...

  8. [BZOJ4407]于神之怒加强版

    BZOJ挂了... 先把程序放上来,如果A了在写题解吧. #include<cstdio> #include<algorithm> #define N 5000010 #def ...

  9. BZOJ 4407 于神之怒加强版

    http://www.lydsy.com/JudgeOnline/problem.php?id=4407 题意: 给下N,M,K.求 思路:  来自:http://blog.csdn.net/ws_y ...

随机推荐

  1. 利用xpath来解析douban电影相对应的信息

    from lxml import etree import requests url = "https://movie.douban.com/chart" headers = {& ...

  2. Linux软件包安装(rpm、yum、apt-get)

    转载自:https://blog.csdn.net/wzq__janeGreen_/article/details/81774924 rpm/yum适用于Redhat.CentOS.Suse等平台: ...

  3. 6-18 Two Stacks In One Array(20 分)

    Write routines to implement two stacks using only one array. Your stack routines should not declare ...

  4. 在VS2008中使用WSE 3.0【转】

    原文:http://www.cnblogs.com/chenxizhang/archive/2008/07/25/1251626.html 在VS2008中使用WSE 3.0 WSE 是微软推出的一套 ...

  5. ZeroClipboard.js兼容各种浏览器复制到剪切板上

    http://www.cnblogs.com/huijieoo/articles/5569990.html <script type="text/javascript" sr ...

  6. Spring集成Mybatis(Dao方式开发)

    Spring整成Mybatis注意事项:  1. 关键jar包不能少 2.可以单独整理好Mybatis框架,测试无误再集成Spring 3.集成时,参数级别的细节可以选择忽略,但思路必须清晰 代码如下 ...

  7. [CLPR] 用于加速训练神经网络的二阶方法

    本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...

  8. 【转】Visual Studio 2012常用快捷键总结

    原文网址:http://blog.csdn.net/yl2isoft/article/details/9886379 写在前面: 都知道,合理使用快捷键可以提高开发效率.但是Visual Studio ...

  9. Hive之 数据类型

    hive 目前支持的数据类型如下: -- 数值类型 Numeric TypesTINYINT (1-byte signed integer, from -128 to 127)SMALLINT (2- ...

  10. RabbitMQ 概念与Java例子

    RabbitMQ简介 目前RabbitMQ是AMQP 0-9-1(高级消息队列协议)的一个实现,使用Erlang语言编写,利用了Erlang的分布式特性. 概念介绍: Broker:简单来说就是消息队 ...