并不对劲的p4449于神之怒加强版
题目大意
给定\(t,k(t\leq2000,k\leq5*10^6)\)
\(t\)组询问,每组给出\(n,m(n,m\leq5*10^6)\)求$\sum_{i=1}^n \sum_{j=1}^m \mathrm{gcd}(i,j)^k $
题解
假设\(n\)较小
枚举gcd
原式=\(\sum_{a=1}^{n}a^k\sum_{i=1}^{\lfloor\frac{n}{a}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{a}\rfloor} [gcd(i,j)=1]\)
=\(\sum_{a=1}^{n}a^k\sum_{i=1}^{\lfloor\frac{n}{a}\rfloor} \sum_{j=1}^{\lfloor\frac{m}{a}\rfloor}\sum_{d|i且d|j}\mu(d)\)
=\(\sum_{a=1}^{n}a^k\sum_{d=1}^{\lfloor\frac{n}{a}\rfloor}\mu(d)*{\lfloor\frac{n}{d*a}\rfloor}*{\lfloor\frac{m}{d*a}\rfloor}\)
设\(b=d*a\)
=\(\sum_{a=1}^{n}a^k\sum_{a|b}^{n}\mu(\frac{b}{a})*{\lfloor\frac{n}{b}\rfloor}*{\lfloor\frac{m}{b}\rfloor}\)
=\(\sum_{b=1}^{n}{\lfloor\frac{n}{b}\rfloor}*{\lfloor\frac{m}{b}\rfloor}\sum_{a|b}^{n}\mu(\frac{b}{a})*a^k\)
设\(f(x)=\sum_{a|x}^{n}\mu(\frac{x}{a})*a^k\)
发现\(f(x)\)是\(\mu\)卷\(x^k\),所以\(f(x)\)也是个积性函数,可以用筛法求
要考虑两件事:1.当\(p\)为质数时,\(f(p)=?\) 2.当\(p\)为质数且\(p|q\)时,\(f(p*q)=?\)
1.\(f(p)=1^k*\mu(p)+p^k*mu(1)=p^k-1\)
2.设\(q\)中\(p\)这个因子的指数为\(c\),则有\(f(q)=f(\frac{q}{p^c})*f(p^c),f(p*q)=f(\frac{q}{p^c})*f(p^{c+1})\),即\(f(p*q)=f(q)*(\frac{f(p^{c+1})}{f(p^c)})\)
那么只要知道\(\frac{f(p^{c+1})}{f(p^c)}\),就能用\(f(q)\)推出\(f(p*q)\)了
发现\(f(p^c)=\sum_{a|p^c}a^k*\mu(\frac{p^c}{a})\),其中\(\mu(\frac{p^c}{a})\)只在\(a=p^c\)时为1,在\(a=p^{c-1}\)时为-1,其余时刻都为0
那就有\(f(p^c)=p^{c*k}-p^{(c-1)*k}\)
同理可得\(f(p^{c+1})=p^{(c+1)*k}-p^{c*k}\)
所以\(\frac{f(p^{c+1})}{f(p^c)}=p^k\)
预处理筛出\(f(x)\)后,询问时整出分块就行了
代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 5000010
#define lim (maxn-10)
#define LL long long
#define add(x) (x>=mod?x-mod:x)
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
const LL mod=1000000007;
int no[maxn],p[maxn],cnt,kth[maxn],mu[maxn],f[maxn],n,m,k,t;
int mul(int x,int y){int res=1;while(y){if(y&1)res=(LL)res*(LL)x%mod;x=(LL)x*(LL)x%mod,y>>=1;}return res;}
int main()
{
t=read(),k=read();
no[1]=f[1]=1;
rep(i,2,lim)
{
if(!no[i])p[++cnt]=i,kth[i]=mul(i,k),f[i]=(-1+kth[i]+mod)%mod;
for(int j=1;j<=cnt&&i*p[j]<=lim;j++)
{
no[i*p[j]]=1;
if(i%p[j]==0){f[i*p[j]]=(LL)f[i]*(LL)kth[p[j]]%mod;break;}
f[i*p[j]]=(LL)f[p[j]]*(LL)f[i]%mod;
}
}
rep(i,1,lim)f[i]=add(f[i]+f[i-1]);
while(t--)
{
n=read(),m=read();if(n>m)swap(n,m);
int ans=0;
for(int l=1,r;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans=(ans+(LL)(n/l)*(LL)(m/l)%mod*(LL)((f[r]-f[l-1]+mod)%mod)%mod)%mod;
}
write(ans);
}
return 0;
}
并不对劲的p4449于神之怒加强版的更多相关文章
- 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, ...
- P4449 于神之怒加强版
\(\color{#0066ff}{ 题目描述 }\) 给定n,m,k,计算 \(\sum_{i=1}^n \sum_{j=1}^m \mathrm{gcd}(i,j)^k\) 对1000000007 ...
- 题解 P4449 于神之怒加强版
这道题算是我完完整整推的第一道题,写篇题解纪念一下. 题目 废话不多说,直接开始推式子(给新手准备,过程较详细,大佬可自行跳步),以下过程中均假设 \((n\le m)\),\([d=1]\) 类似于 ...
- 【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行, ...
随机推荐
- Day 4 Linux基础
Linux基础(指令篇) 一.Linux命令 1.Linux命令行的语法格式: 命令+选项+参数 命令:告诉Linux(UNIX)操作系统做(执行)什么. 选项:说明命令运行的方式(可以改变命令的功能 ...
- Ubuntu 安装PostgreSQL
安装最新版: sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres"的Linux用户 (2)创建名为"p ...
- LeetCode OJ--Partition List
http://oj.leetcode.com/problems/partition-list/ 链表的处理 #include <iostream> using namespace std; ...
- C++ 使用成员初始化列表的一个小坑
注意在成员列表中初始化的顺序并不是列表顺序 而是: 在类中声明的顺序! EventLoop::EventLoop() :looping(false), quit(false),_tid(curThre ...
- linux下查看隐藏文件
linux下查看隐藏文件的快捷键:Ctrl+H 命令:ls -a
- 【Todo】Java Callable和Future学习
参考这篇文章:http://blog.csdn.net/ghsau/article/details/7451464 还有一个系列<Java多线程>
- [React] Persist Form Data in React and Formik with formik-persist
It can be incredibly frustrating to spend a few minutes filling out a form only to accidentally lose ...
- Android Studio 一些方便使用的设置
相信非常多使用Eclipse的朋友,開始用Android Studio都是认为不是特别方便, a:比方怎样使鼠标放到方法上面,就有提示用法; b:怎样设置字体大小,和背景色; c:还有怎么查看Outl ...
- sublime 高速打开跳转至关联文件
在下一枚web前端,近期在用sublime text2编辑器写前端.因为页面较多,项目较大,所以难免出现非常多引用文件和一些js的teample模板. 问题:在Sublime Text编写代码过程中要 ...
- 处理页面载入图片js(等比例压缩图片)
第一页面html <div class="admin">${answer.content}</div> <div class="admin ...