怎么可能会有这么奇怪的筛法
前言
HDU 联赛防 AK 放了这个玩意,所以勉强记起来一点这个东西然后推了一下,感觉很有趣就挂上来了。貌似比较具有一般性?也算半个筛法了。
问题
考虑
快速算出指一些高效亚线性做法。
(其实前三个条件是等价的,但是
要求
做法
如果满足上面的条件,那么
接下来,记
由于
于是有:
后面那个限制很烦,但显然不能莫反,直接容斥。
于是得到一个奇怪递归式,每次直接上 Pollard-Rho 取最大的
注意到当
复杂度不会算,但是注意到每次
Example
求
考虑
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e6+9;
int pri[N];
int phi[N];
bool vis[N];
map<int,int>Phi;
void in(){
int cnt=0;
vis[0]=vis[1]=1;
phi[1]=1;
for(int i=2;i<N;i++){
if(!vis[i]){
pri[cnt++]=i;
phi[i]=i-1;
}
for(int j=0;j<cnt&&i*pri[j]<N;j++){
vis[i*pri[j]]=1;
if(i%pri[j]){
phi[i*pri[j]]=phi[i]*phi[pri[j]];
}
else{
phi[i*pri[j]]=phi[i]*pri[j];
break;
}
}
}
for(int i=1;i<N;i++){
phi[i]+=phi[i-1];
}
}
int get_Phi(int x){
if(x<N) return phi[x];
if(Phi[x]) return Phi[x];
int res=x*((int)(x+1))/2;
for(int l=2,r;l<=x;l=r+1){
r=x/(x/l);
res-=(r-l+1)*get_Phi(x/l);
}
return Phi[x]=res;
}
int Pi(int n){
int m=(int)sqrt(n+0.5);
int ans=n;
for(int i=2;i<=m;i++)
if(n%i==0){
ans=ans/i*(i-1);
while(n%i==0) n/=i;
}
if(n>1) ans=ans/n*(n-1);
return ans;
}
const int inf=2e18,step=127;
int qpow(int base,int p,int mod){
int res=1,tmp=base;
while(p){
if(p&1) res=((__int128)res*tmp)%mod;
tmp=((__int128)tmp*tmp)%mod;
p>>=1;
}
res%=mod;
return res;
}
const int primes[]={2,325,9375,28178,450775,9780504,1795265022};
bool MR(int n){
if(n<3||n%2==0) return n==2;
if(n%3==0) return n==3;
int u=n-1,t=0,a;
while(u%2==0) u/=2,t++;
for(int i=0;i<7;i++){
a=primes[i]%n;
if(a==0||a==1||a==n-1) continue;
int v=qpow(a,u,n);
if(v==1) continue;
int s;
for(s=0; s<t; s++){
if(v==n-1) break;
v=(__int128)v*v%n;
}
if(s==t) return 0;
}
return 1;
}
int f(int x,int c,int mod){
return ((__int128)x*x+c)%mod;
}
int gcd(int x,int y){
if(!y) return x;
return gcd(y,x%y);
}
double t1;
int rho(int x){
t1=(double)clock();
int s=0,t=0,c=(1ll*rand()*rand())%(x-1)+1;
int stp=0,goal=1;
int val=1;
for(goal=1;;goal<<=1,s=t,val=1){
for(stp=1;stp<=goal;stp++){
t=f(t,c,x);
val=(__int128)val*abs(t-s)%x;
if(stp%step==0){
int d=gcd(val,x);
if(d>1) return d;
}
}
int d=gcd(val,x);
if(d>1) return d;
}
return inf;
}
int Ans;
void maxpri(int p){
if(Ans>=p||p==1) return;
if(MR(p)){
Ans=max(Ans,p);
return;
}
int fnd=inf;
while(fnd>=p) fnd=rho(p);
while(p%fnd==0) p/=fnd;
maxpri(p),maxpri(fnd);
}
int n,m,mod;
int qpow(int x,int p){
int res=1;
while(p){
if(p&1) res=res*x%mod;
x=x*x%mod,p>>=1;
}
return res;
}
int F(int x,int y){
return (x-1)*(qpow(x,y-1))%mod;
}
const int C=1e7;
map<int,int>mp[C+9];
int S(int n,int m){
if(n<=C&&mp[n][m]) return mp[n][m];
if(!n||!m) return 0;
if(n==1) return get_Phi(m)%mod;
if(m==1) return Pi(n)%mod;
Ans=0,maxpri(n);
int p=Ans;
int tn=n,c=0,val=1;
while(tn%p==0) tn/=p,c++,val*=p;
int res=0;
int now=0,pw=1;
while(pw<=m){
res=(res+F(p,c+now)*(mod+S(n/val,m/pw)-S(n/(val/p),m/(p*pw))))%mod;
now++,pw*=p;
}
if(n<=C) mp[n][m]=res;
return res;
}
int T;
signed main(){
in();
cin>>T;
while(T--){
cin>>n>>m>>mod;
cout<<S(n,m)<<"\n";
}
return 0;
}
:::
实际只跑了 $500ms$,暴踩标程。