P6810 「MCOI-02」Convex Hull 凸包
「MCOI-02」Convex Hull 凸包
题意即求:
\[\sum _ { i = 1 } ^ { n } \sum _ { j = 1 } ^ { m } \sigma _ 0 ( i ) \sigma _ 0 ( j ) \sigma _ 0 ( \gcd ( i , j ) ) \pmod { p }
\]
其中 \(T = 1\),\(n , m \le 2 \times 10 ^ 6\),$p \le 10 ^ 9 $。
被 long long
制裁了,诡异的常数差距。
剩下就是一道水题。
推导一下结束吧:
\[\begin{aligned}
&
\sum _ { i = 1 } ^ { n } \sum _ { j = 1 } ^ { m } \sigma _ 0 ( i ) \sigma _ 0 ( j ) \sigma _ 0 ( \gcd ( i , j ) )
\\
= &
\sum _ { d = 1 } \sum _ { i = 1 } ^ { n } \sum _ { j = 1 } ^ { m } \sigma _ 0 ( i ) \sigma _ 0 ( j ) \sigma _ 0 ( d ) \left[ \gcd ( i, j ) = d \right]
\\
= &
\sum _ { d = 1 } \sigma _ 0 ( d ) \sum _ { i = 1 } ^ { \left\lfloor n / d \right \rfloor } \sum _ { j = 1 } ^ { \left\lfloor m / d \right \rfloor } \sigma _ 0 ( i d ) \sigma _ 0 ( j d ) \left[ \gcd ( i, j ) = 1 \right]
\\
= &
\sum _ { d = 1 } \sigma _ 0 ( d ) \sum _ { i = 1 } ^ { \left\lfloor n / d \right \rfloor } \sum _ { j = 1 } ^ { \left\lfloor m / d \right \rfloor } \sigma _ 0 ( i d ) \sigma _ 0 ( j d ) \sum _ { k \mid i, k \mid j} \mu ( k )
\\
= &
\sum _ { d = 1 } \sigma _ 0 ( d ) \sum _ { k = 1} \mu ( k ) \sum _ { i = 1 } ^ { \left\lfloor n / kd \right \rfloor } \sigma _ 0 ( i kd ) \sum _ { j = 1 } ^ { \left\lfloor m / kd \right \rfloor } \sigma _ 0 ( j kd )
\\
= &
\sum _ { T = 1 } \sum _ { d \mid T } \sigma _ 0 ( d ) \mu \left(\dfrac{T}{d}\right) \sum _ { i = 1 } ^ { \left\lfloor n / T \right \rfloor } \sigma _ 0 ( i T ) \sum _ { j = 1 } ^ { \left\lfloor m / T \right \rfloor } \sigma _ 0 ( j T )
\end{aligned}
\]
然后随便做就好了。
时间复杂度 \(O(n \ln n)\)。
代码:
#include<iostream>
#include<cstdio>
#define ll int
using namespace std;
namespace Ehnaev{
inline ll read() {
ll ret=0,f=1;char ch=getchar();
while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();}
while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();}
return ret*f;
}
inline void write(ll x) {
static char buf[22];static ll len=-1;
if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);}
else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);}
while(len>=0) putchar(buf[len--]);
}
}using Ehnaev::read;using Ehnaev::write;
const ll N=2e6;
ll n,m,p,cnt,ans;
ll sgm0[N+5],prime[N+5],f[N+5],g[N+5];
bool ff[N+5];
inline void Init() {
for(ll i=1;i<=n;i++) {
for(ll j=i;j<=n;j+=i) {
sgm0[j]=(sgm0[j]+1)%p;
}
}
for(ll i=1;i<=n;i++) {
for(ll j=i,cn=1;j<=n;j+=i,cn++) {
f[i]=(f[i]+sgm0[j])%p;
}
}
for(ll i=1;i<=m;i++) {
for(ll j=i,cn=1;j<=m;j+=i,cn++) {
g[i]=(g[i]+sgm0[j])%p;
}
}
ff[1]=1;
for(ll i=2;i<=n;i++) {
if(!ff[i]) {prime[++cnt]=i;}
for(ll j=1;j<=cnt&&(long long)i*(long long)prime[j]<=(long long)n;j++) {
ff[i*prime[j]]=1;if(i%prime[j]==0) break;
}
}
for(ll i=1;i<=cnt;i++) {
for(ll j=n/prime[i];j;j--) {
sgm0[j*prime[i]]=(sgm0[j*prime[i]]-sgm0[j]+p)%p;
}
}
}
int main() {
n=read();m=read();p=read();if(n<m) swap(n,m);
Init();
for(ll i=1;i<=m;i++) {
ll tmp=((long long)sgm0[i]*(long long)f[i]%(long long)p)
*(long long)g[i]%(long long)p;
ans=(ans+tmp)%p;
}
write(ans);
return 0;
}