Luogu 4449 于神之怒加强版
挺套路的题,然而一开始还是想错了……
$\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 于神之怒加强版的更多相关文章
- 【BZOJ-4407】于神之怒加强版 莫比乌斯反演 + 线性筛
4407: 于神之怒加强版 Time Limit: 80 Sec Memory Limit: 512 MBSubmit: 241 Solved: 119[Submit][Status][Discu ...
- 【BZOJ4407】于神之怒加强版(莫比乌斯反演)
[BZOJ4407]于神之怒加强版(莫比乌斯反演) 题面 BZOJ 求: \[\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)^k\] 题解 根据惯用套路 把公约数提出来 \[\sum ...
- BZOJ 4407 于神之怒加强版 (莫比乌斯反演 + 分块)
4407: 于神之怒加强版 Time Limit: 80 Sec Memory Limit: 512 MBSubmit: 1067 Solved: 494[Submit][Status][Disc ...
- bzoj 4407 于神之怒加强版 (反演+线性筛)
于神之怒加强版 Time Limit: 80 Sec Memory Limit: 512 MBSubmit: 1184 Solved: 535[Submit][Status][Discuss] D ...
- 【BZOJ4407】于神之怒加强版 莫比乌斯反演
[BZOJ4407]于神之怒加强版 Description 给下N,M,K.求 Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行, ...
- P4449 于神之怒加强版 (莫比乌斯反演)
[题目链接] https://www.luogu.org/problemnew/show/P4449 给定n,m,k,计算 \(\sum_{i=1}^n \sum_{j=1}^m \mathrm{gc ...
- 洛谷 - P4449 - 于神之怒加强版 - 莫比乌斯反演
https://www.luogu.org/problemnew/show/P4449 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{i=1}^{m} gcd(i, ...
- [BZOJ4407]于神之怒加强版
BZOJ挂了... 先把程序放上来,如果A了在写题解吧. #include<cstdio> #include<algorithm> #define N 5000010 #def ...
- BZOJ 4407 于神之怒加强版
http://www.lydsy.com/JudgeOnline/problem.php?id=4407 题意: 给下N,M,K.求 思路: 来自:http://blog.csdn.net/ws_y ...
随机推荐
- [QT_FFMPEG]学习问题: 刚开始移植ffmpeg,测试时出现 undefined reference to `avcodec_configuration()'
使用环境: window: win7 x64 QT: qt5.8.0 MinGW530 移植的教程: 流若浅 Qt ffmpeg环境搭建 : http://www.cnblogs.com/liuru ...
- ubuntu 通过ssh上传/下载服务器文件
1.用ssh登录远程ubuntu主机 (主机ip为:1.2.3.4;用户名:username) ssh username@1.2.3.4 2.从远程ubuntu主机copy文件/文件夹到本地(scp) ...
- test20181020 B君的第二题
题意 分析 考场70分 一看就是裸的kmp,直接打上去. #include<cstdlib> #include<cstdio> #include<cmath> #i ...
- 转 How do GraphQL remote schemas work
文章转自 prisma 官方博客,写的很不错 In this article, we want to understand how we can use any existing GraphQL AP ...
- bzoj2957楼房重建
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树.每个点记录斜率,要一个单增的序列长度(从1开始). 线段树每个点记录自己区间的 ...
- sublime Text 3安装sublimecodeIntel插件
下载:https://github.com/SublimeCodeIntel/SublimeCodeIntel 解压到: data/pacakges目录 安装 Package Control插件 ...
- 【python】 Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
刚在机器上windows环境下装上pip方便以后安装包的时候使用,谁知道第一次使用pip安装asyncio的时候就报错. 在Windows7x64下使用pip安装包的时候提示报错:Microsoft ...
- python命令行中import caffe报错的解决方案
1.ImportError: No module named skimage.io >>> import caffe Traceback (most recent call last ...
- Autofac Property Injection and Method Injection
While constructor parameter injection is the preferred method of passing values to a component being ...
- oracle 11g RAC 的一些基本概念(四)
RAC 在Grid Infrastructure安装完以后,我们把注意力转移到集群上的Oracle软件的安装上来.我们看到,Grid Infrasctructure提供了运行RAC的框架,包括集群 ...