「题解」sdfzoj contest5 A. 亚log欧拉函数求和问题 (HDU 5728 共加五个 0)
给定 \(n,m\),保证 \(\mu^2(n)=1\),求:
\[\sum_{i=1}^m\varphi(in)
\]
模 \(10^9+7\),\(n\leq 10^{10},m\leq 10^9\).
对于把 \(\varphi(in)\) 拆开,比较经典的是考虑每个质因子 \(p\) 的贡献,则有:
\[\begin{aligned}
&\sum_i^m\varphi(in)
\\
=&\sum_i^m\frac{\varphi(i)\varphi(n)}{\varphi(gcd(i,n))}gcd(i,n)
\end{aligned}
\]
然后把各种常规套路塞进去硬推:
\[\begin{aligned}
=&\sum_{i}^m\frac{\varphi(i)\varphi(n)}{\varphi(gcd(i,n))}gcd(i,n)
\\
=&\varphi(n)\sum_{d|n}\frac{d}{\varphi(d)}\sum_i^{\left\lfloor\frac{m}{d}\right\rfloor}[gcd(i,\frac{n}{d})=1]\varphi(id)
\\
=&\varphi(n)\sum_{d|n}\frac{d}{\varphi(d)}\sum_i^{\left\lfloor\frac{m}{d}\right\rfloor}\varphi(id)\sum_{g|i,g|\frac{n}{d}}\mu(g)
\\
=&\varphi(n)\sum_{d|n}\frac{d}{\varphi(d)}\sum_{g|\frac{n}{d}}\mu(g)\sum_i^{\left\lfloor\frac{m}{gd}\right\rfloor}\varphi(idg)
\end{aligned}
\]
枚举 \(T=dg\):
\[\begin{aligned}
\varphi(n)\sum_{T|n}\left (\sum_{d|T}\frac{d}{\varphi(d)}\mu\left(\frac{T}{d}\right)\right )\times\left(\sum_i^{\left\lfloor\frac{m}{T}\right\rfloor}\varphi(iT)\right)
\end{aligned}
\]
由于 \(\mu^2(n)=1\),所以 \(n\) 的因子数很少,而既然没有平方因子,那么可以提前状压预处理出 \(n\) 每个因子 \(d\) 的值,与 \(\frac{d}{\varphi(d)}\) 和 \(\mu(d)\).
这样左边那一坨直接暴力预处理就可以做到 \(\mathcal{O}(3^{\omega(n)})\),右边直接递归下去算即可,递归到 \(n=1\) 的时候用杜教筛暴力求出来。
复杂度不是很会算,但是跑得很快。这个链接是 HDU 5728 的代码,还多了一个算扩展欧拉定理,不过是平凡的。
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<assert.h>
#include<random>
#include<unordered_map>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x, T2& ...y){read(x);read(y...);}
const int N=10000010;
const ll mod=1000000007;
inline void cadd(ll &x,ll y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(ll &x,ll y){x=(x-y<0)?(x-y+mod):(x-y);}
inline ll Mod(ll x){return x<0?x+mod:(x>=mod?(x-mod):x);}
ll qpow(ll x,ll y){
x%=mod;
ll s=1;
while(y){
if(y&1)s=s*x%mod;
x=x*x%mod;
y>>=1;
}
return s;
}
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
inline int lowbit(int x){return x&(-x);}
const int M=1664510;
unordered_map<ll,ll>sphi;
int vis[M+10],pct;
ll prime[M];
//1 -> No 0 -> Yes
ll ophi[N+10];
void pre(){
vis[1]=1;sphi[1]=ophi[1]=1;
for(ll i=2;i<=M;++i) {
if(!vis[i]){
prime[++pct]=i;
ophi[i]=i-1;
}
for(int j=1;j<=pct&&i*prime[j]<=M;++j) {
vis[i*prime[j]]=1;
if(i%prime[j]==0){
ophi[i*prime[j]]=ophi[i]*prime[j];
break ;
}
ophi[i*prime[j]]=ophi[i]*(prime[j]-1);
}
}
for(ll i=2;i<=M;++i)
ophi[i]+=ophi[i-1];
}
ll Sphi(ll n){
if(n<=M)return ophi[n];
if(sphi.count(n))return sphi[n];
ll sumq=n*(n+1)/2;
for(ll l=2,r;l<=n;l=r+1){
r=n/(n/l);
sumq-=(r-l+1)*Sphi(n/l);
}
return sphi[n]=sumq;
}
ll n;
int m,ct;
vll a;
ll v[201000],phi[201000],mu[201000],g[201000];
ll f[201000];
int lg[201000];
ll solve(int S,int m){
if(S==0)return Sphi(m)%mod;
if(!m)return 0;
ll sum=0;
for(int T=S;T;T=(T-1)&S){
cadd(sum,f[T]*solve(T,m/v[T])%mod);
}
cadd(sum,f[0]*solve(0,m/v[0])%mod);
return phi[S]*sum%mod;
}
signed main(){
pre();
read(n,m);
{
ll t=n;
for(ll i=2;i*i<=t;i++)
if(t%i==0)
a.pb(i),t/=i;
if(t>1)a.pb(t);
sort(a.begin(),a.end());
ct=a.size();
}
for(int i=0;i<ct;i++)lg[1<<i]=i;
v[0]=1;phi[0]=1;mu[0]=1;
for(int i=1;i<(1<<ct);i++){
v[i]=v[i-lowbit(i)]*a[lg[lowbit(i)]];
phi[i]=phi[i-lowbit(i)]*(a[lg[lowbit(i)]]-1);
mu[i]=-mu[i-lowbit(i)];
}
for(int i=0;i<(1<<ct);i++){
g[i]=qpow(phi[i],mod-2)*(v[i]%mod)%mod;
mu[i]=Mod(mu[i]);
}
for(int i=0;i<(1<<ct);i++){
for(int j=i;j;j=(j-1)&i){
cadd(f[i],g[j]*mu[i^j]%mod);
}
cadd(f[i],g[0]*mu[i]%mod);
}
cout << solve((1<<ct)-1,m); << '\n';
return 0;
}