常见结论。
同余数论
exCRT
考虑合并两个方程:
设 ,则有:,
考虑用 exgcd 求出一组 和 ,根据 之间的倍数关系调整 ,更改模数为 ,并解出一个 的特解,取模后即完成合并。
exLucas
Lucas 定理:, 为质数,证明利用 进制分解。
exlucas:求解 , 为质数。
展开:,其中剩余的部分均无 因子。
关键则在求解 , 可以表示为二元组 ,表示模数与分离的 指数,可以预处理。
定义函数 ,且 中无 因子,有:
使用扩展欧拉定理对幂次降幂,并提前预处理,可以将上述算法做到 。
正确性证明
,则 在 意义下有循环节长度 。
则将 中各项划分为有 因子与没有 因子有:
后两项中显然没有 因子,则 只用将 中的 因子个数加上当前的 个即可。
则要乘上后两项,并考虑 中非 因子的部分,递归即可。
递归边界是 。
则 exlucas 算法可以做到 的预处理, 的单组查询。
code
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define lll __uint128_t
namespace fastIO{
#define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,IO,stdin))==rp1?EOF:*rp1++)
#define pc(a) ((wrp==obuf+IO&&(fwrite(obuf,1,IO,stdout),wrp=obuf)),(*wrp++)=a)
const int IO=1<<22;
char buf[IO+1],obuf[IO+1],*wrp=obuf,*rp1,*rp2;
template<typename qwq>inline void read(qwq &a){
a=0;int c=gc();
while(!isdigit(c)) c=gc();
while(isdigit(c)) a=10*a+c-'0',c=gc();
}
template<typename qwq>inline void write(qwq x){
int sk[20],top=0;
do{
sk[++top]=x%10,x/=10;
}while(x);
while(top) pc(sk[top--]+'0');
pc('\n');
}
#undef pc
#undef gc
};using namespace fastIO;
class exlucas_algorithm{
private:
struct fastmod{
ull b,m;//注意 phi=1 时 m=1<<64/b 会出错,所以用 1<<63
inline void init(int x){b=x,m=(1ull<<63)/b;}
inline ull operator ()(ull x){//快速模
ull q=((lll)x*m)>>63,r=x-q*b;
return r>=b?r-b:r;
}
inline ull operator [](ull x){//快速除
ull q=((lll)x*m)>>63,r=x-q*b;
return r>=b?q+1:q;
}
}M0,M,Mphi;//euler
inline ull ksm(ull a,int b){
ull ret=1;
for(;b;b>>=1,a=M(a*a)){
if(b&1) ret=M(ret*a);
}
return ret;
}
vector<ull>fac,pw;
int qwq[50];
public:
int p0,p,phi,cntp;
void init(int _p0,int _p,int _cntp){
p0=_p0,p=_p,phi=p/p0*(p0-1),cntp=_cntp;
M0.init(p0),M.init(p),Mphi.init(phi);
fac.resize(p+5),fac[0]=1,qwq[0]=1;
for(int i=1;i<=cntp;i++) qwq[i]=qwq[i-1]*p0;
for(int i=1,x;i<=p;i++){
if(M0(i)) fac[i]=M(fac[i-1]*i);
else fac[i]=fac[i-1];
}
pw.resize(2*phi+5),pw[0]=1,pw[1]=fac[p];
for(int i=2;i<=2*phi;i++) pw[i]=M(pw[i-1]*fac[p]);
}
inline void exlucas(ull n,ull &x){//O(log_p n)
ull tmp;//这里没有采用递归写法
for(x=1;n;n=M0[n]){//扩展欧拉定理降幂
tmp=M[n],(tmp>phi)&&(tmp=Mphi(tmp)+phi);
x=M(M(x*pw[tmp])*fac[M(n)]);
}
}
inline void check(ull n,ull m,ull t,ull &a){
for(a=0;a<cntp&&n!=0;) n=M0[n],m=M0[m],t=M0[t],a+=n-m-t;
//根据有关数论证明,每次的 n-m-t\in{0,1},则 a 不降
}
inline ull binom(ull n,ull m){//这里面采用 a^{phi-1} 求逆元
ull x,y,z,a;check(n,m,n-m,a);
if(a>=cntp) return 0;
exlucas(n,x),exlucas(m,y),exlucas(n-m,z);
x=M(x*ksm(M(y*z),phi-1));
return M(x*qwq[a]);
}
}Lucas[20];
int cnt,mod[20];
void exlucas_init(int P){
for(int i=2,c,a;i*i<=P;i++){
if(P%i) continue;
for(c=0,a=1;P%i==0;c++) a*=i,P/=i;
Lucas[++cnt].init(i,a,c),mod[cnt]=a;
}
if(P!=1) Lucas[++cnt].init(P,P,1),mod[cnt]=P;
}
void exgcd(int a,int b,ll &x,ll &y){
if(b==0) return x=1,y=0,void();
exgcd(b,a%b,y,x),y-=(a/b)*x;
}
inline void exCRT(ull n,ull m){
ll ans=0,M=1,r,p,c,k0,k1;//初始值
for(int i=1;i<=cnt;i++){//ax=b(mod p)
p=mod[i],r=Lucas[i].binom(n,m);
c=p-(ans+p-r)%p,exgcd(M,p,k0,k1);//gcd=1 显然
k0=k0*c%p+p,ans=ans+M*k0,M*=p,ans%=M;
}
printf("%lld\n",ans);
}
int main(){
int T,P;ull n,m;read(T),read(P),exlucas_init(P);
for(int i=1;i<=T;i++) read(n),read(m),exCRT(n,m);
return fwrite(obuf,1,wrp-obuf,stdout),0;
}exBSGS
BSGS:求 。
解法:分块,设 ,则用哈希表存储所有 ,然后枚举 检查 在哈希表中是否有值。
exBSGS:去掉了 的限制,则考虑如何转化出该限制。
求得 ,若 ,无解,否则有:,
不断消去公因子,则最后一定有 ,且可以证明,前面的系数均与最终的 互质。最后记得暴力检查消去的次数(最后 的指数形如 ),这一部分是 的,总复杂度为 。
code
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;//判了 EOF
#define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,IO,stdin))==rp1?EOF:*rp1++)
const int IO=1<<22;
char buf[IO+1],*rp1,*rp2;
__gnu_pbds::gp_hash_table<int,int>mp;
inline int read(){
int a=0,c=gc();
while(!isdigit(c)) c=gc();
while(isdigit(c)) a=10*a+c-'0',c=gc();
return a;
}
inline int bsgs(int c,int a,int p,int b){
int up=ceil(sqrt(p)),d=1;mp.clear();
for(int i=1;i<=up;i++) b=1ll*b*a%p,mp[b]=i;
for(int i=1;i<=up;i++) d=1ll*d*a%p;//a^up
for(int i=1,t=0;i<=up;i++){
c=1ll*c*d%p,t+=up;
if(mp.find(c)!=mp.end()) return t-mp[c];
}
return -998244353;//注意后面有一步加法
}
inline int exbsgs(int a,int p,int b){
int k=0,p0=p,b0=b,c=1;//c 是 a 前面的系数
if(b==1) return 0;
for(int d=__gcd(a,p),x=1,i=1;d!=1;i++,d=__gcd(a,p)){
x=1ull*a*x%p0;
if(x==b0) return i;//同步检查<=k 的答案是否存在
if(b%d) return -1;
b/=d,p/=d,c=1ull*c*(a/d)%p,k++;
}
return bsgs(c,a%p,p,b%p)+k;
}
int main(){
for(int a=read(),p=read(),b=read(),t;a;b=read()){
t=exbsgs(a%p,p,b%p),a=read(),p=read();
if(t<0) puts("No Solution");
else printf("%d\n",t);
}
return 0;
}
阶与原根
定义
对于数 ,定义 模 的阶为最小的数 ,满足 ,记为 。(类似循环节)
对于数 ,定义模 的原根为数 ,当且仅当 。
原根存在定理:数 有原根,当且仅当 能表示为 的其中一种形式,其中 为奇素数, 为整数。
原根分别为 。
由于阶的最小性和定义,再由欧拉定理:若 。
我们可以推出:。进一步,有如下原根判定定理: 时, 是 的原根,
当且仅当 ,且对于 的所有质因数 ,都有 。
注:实现优秀的判定定理复杂度为 而不是 ,具体地,对每个素因子分别求快速幂,用前缀积和后缀积,可以证明复杂度。
性质
-
两两模 不同余。
-
。
-
上式推广:。
-
对于 ,,利用第二条性质与整除有关理论可以证明。
-
如果 有原根,则其原根个数为 ,设 为一个最小原根,则 都是原根。
-
原根与离散对数:任意 ,存在一个 ,。
-
的最小原根的大小数量级为 ,则可以枚举求解,用判定定理即可。
值得注意的是,在利用原根求离散对数解决连乘和乘方的问题时,要注意模数会变化成 。
质数相关
Miller-Rabin
费马小定理:如果 是素数,则 。
然而这不足以让我们判定一个数是否是素数,错误率较高。
二次探测定理:若 为素数,则由 可以推得 或 。
则移项有:,则结合费马小定理,假设 是素数,再选取一个 。
令 ,我们不断考虑序列 ,
显然,最后一项为 (不为一显然不是素数),则有:,
应用二次探测定理:.
不断开根,如果某一次满足后者,则可知:序列中有 。如果每次都满足前者,则可知 。
故:序列中出现了 或 的情况时, 才可能是素数。
上述算法选一个 单次检验是 的。
选取 这七个就能判定 long long 范围内的所有情况。也可以选用前十二个质数。
然而,一般情况下,为了追求效率,往往只取较少的个数。
数论函数
积性函数
积性函数:对于一个数论函数 ,若 ,则称 为积性函数。
若去掉 限制仍满足,则为完全积性函数。
常见积性函数:
-
,完全积性函数。
-
,积性函数。
-
,完全积性函数,叫元函数。
-
,积性函数,叫欧拉函数。
-
,即对 质因数分解,则 ,积性函数,叫莫比乌斯函数。
-
,完全积性函数。
狄利克雷卷积
对于两个数论函数 ,定义它们的狄利克雷卷积为:
对于一个积性函数 ,有如下性质:
-
也是积性函数,利用 易证。
-
也是积性函数,按照定义证明即可,注意这里的乘法相当于对位相乘。
-
也是积性函数,根据上式即可。
-
也是积性函数,套定义证明即可,较麻烦。
常见卷积:
-
。
-
,考虑对于质数幂次证明即可,其余根据积性函数易证。
-
,其中 为任意积性函数,则表明: 是狄利克雷卷积中的单位元。
-
,莫比乌斯反演核心,同样先对质数的幂次证明即可。