向 command_block 巨佬学习!开始省选前的游击小记QAQ。
阶:
设 且 ,称满足 的最小正整数 为 在 意义下的阶,记作 。
阶的相关引理:
-
若 ,且 ,则
-
-
如果 ,则 。可推出
-
-
设 为非负整数,则
-
若 ,则
原根:
时,称 是 意义下的原根。
原根的相关引理
-
**原根个数:**若一个数 有原根,它的原根个数为
-
**原根判定:**若 是 的原根,则对于 的任意素因子 都有
-
**原根存在:**一个数 存在原根,当且仅当 ,其中 为奇素数,
#include<bits/stdc++.h>
using namespace std;
const int N=2000010;
int vis[N],p[N],phi[N],rt[N],cnt,d,m,a[N],tot,ans[N],num;
long long qpow(long long a,int b,int mo)
{
long long s=1;
while(b)
{
if(b&1)s=s*a%mo;
a=a*a%mo;
b>>=1;
}
return s;
}
void init()
{
phi[1]=1;
for(int i=2;i<=N-10;i++)
{
if(!vis[i])
{
p[++cnt]=i;
phi[i]=i-1;
}
for(int j=1;j<=cnt&&p[j]*i<=N-10;j++)
{
vis[i*p[j]]=1;
if(i%p[j]==0)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
rt[2]=rt[4]=1;
for(int i=2;i<=cnt;i++)
{
for(int j=1;1ll*j*p[i]<=N-10;j*=p[i])
rt[j*p[i]]=1;
for(int j=2;1ll*j*p[i]<=N-10;j*=p[i])
rt[j*p[i]]=1;
}
}
void divid(int n)
{
tot=0;
for(int i=1;i<=cnt&&n>1&&p[i]*p[i]<=n;i++)
{
if(n%p[i]==0)
{
a[++tot]=p[i];
while(n%p[i]==0)n/=p[i];
}
}
if(n>1)a[++tot]=n;
}
bool check(int x,int mo)
{
if(qpow(x,phi[mo],mo)!=1)return 0;
for(int i=1;i<=tot;i++)
if(qpow(x,phi[mo]/a[i],mo)==1)return 0;
return 1;
}
int findrt(int n)
{
for(int i=1;i<n;i++)
if(check(i,n)) return i;
}
void getrt(int mo,int x)
{
int s=1;
num=0;
for(int i=1;i<=phi[mo];i++)
{
s=s*x%mo;
if(__gcd(i,phi[mo])==1)
ans[++num]=s;
}
}
int main()
{
int t;
scanf("%d",&t);
init();
while(t--)
{
scanf("%d%d",&m,&d);
if(rt[m]==0)printf("0\n\n");
else{
divid(phi[m]);
int mi=findrt(m);
getrt(m,mi);
sort(ans+1,ans+num+1);
printf("%d\n",num);
for(int i=d;i<=num;i+=d)
printf("%d ",ans[i]);
printf("\n");
}
}
return 0;
}指标:
设 为 意义下的原根,,称最小非负的满足 的 为 在 意义下以 为底的指标,记作
指标的相关引理:
-
若 ,则
-
设 为 意义下的两个不同的原根,则
-
常用的求指标方法:BSGS
问题:
时,对 ,求
solution:
设 ,其中 ,则有
预处理出 的值,用 map 存起来。然后枚举 查表即可。用 map 的话复杂度为
EXBSGS: 不一定互质
思路:让 变得互质
具体地,设 。 时无解,否则有
若 与 不互质就继续这样做下去,直至 。记 ,则有
算出 再加上 即可。
注意: 答案有可能 ,因此在除 的过程中,若出现 ,则答案为此时的
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a,b,p,ans;
map<ll,ll> mp;
ll qpow(ll x,ll y,ll p)
{
ll s=1;
while(y)
{
if(y&1)s=s*x%p;
x=x*x%p;
y>>=1;
}
return s;
}
ll gcd(ll x,ll y)
{
return y==0? x:gcd(y,x%y);
}
ll exbsgs()
{
if(b==1) return 0;
mp.clear();
ll cnt=0,t=1;
for(int d=gcd(a,p);d>1;d=gcd(a,p))
{
cnt++;
if(b%d)return -1;
b/=d,p/=d;
t=t*a/d%p;
if(b==t)return cnt;
}
ll s=b,m=sqrt(p)+1;
for(int i=0;i<m;i++)
{
mp[s]=i;
s=s*a%p;
}
s=t;
ll x=qpow(a,m,p);
for(int i=1;i<=m;i++)
{
s=s*x%p;
if(mp.count(s))
return i*m-mp[s]+cnt;
}
return -1;
}
int main()
{
while(1)
{
scanf("%lld%lld%lld",&a,&p,&b);
if(a+b+p==0)break;
if(a%p)
{
a%=p,b%=p;
ans=exbsgs();
if(ans<0)printf("No Solution\n");
else printf("%lld\n",ans);
}
}
return 0;
}例题:
P4884 多少个1?
将原题等式左右同时 ,则问题转化为求 。注意:要龟速乘
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace FGF
{
ll k,p;
unordered_map<ll,ll> mp;
ll qpow(ll x,ll y,ll mo)
{
ll s=0;
while(y)
{
if(y&1)s=(s+x)%mo;
x=(x+x)%mo;
y>>=1;
}
return s;
}
ll BSGS(ll x,ll y,ll p)
{
if(y==1)return 0;
ll m=sqrt(p)+1,s=y,xx=1;
for(int i=0;i<=m;i++)
mp[s]=i,s=qpow(s,x,p);
for(int i=1;i<=m;i++)
xx=qpow(xx,x,p);
s=xx;
for(int i=1;i<=m;i++,s=qpow(s,xx,p))
if(mp.count(s))return i*m-mp[s];
return -1;
}
void work()
{
scanf("%lld%lld",&k,&p);
printf("%lld",BSGS(10,(9*k+1)%p,p));
}
}
int main()
{
FGF::work();
return 0;
}P5605 小A与两个Star_Cried
为奇素数的幂,因此它必然有原根。设 的原根为 ,。那么有
根据裴蜀定理 时才有解,进而推出
设 的阶为 ,。因为 的阶为 ,所以 。
设 ,因为 为最小的能使 为整数的数,。代入原式可得出 时有解。所以我们只需要求出 的阶即可。
根据阶都是 的因数,求阶时枚举 的每个质因数,确定这一质因数能取到的最高次数即可。
分解质因数需要用 Pollard-rho,时间复杂度为 ,如果像我一样实现不够优秀可能会写成 然后被卡常/kk。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 ull;
namespace FGF
{
ll n,m,a,b,phi;
const int p[15]={2,3,5,7,11,17,19,61,233,997,24251};
ll fac[100],cnt;
mt19937_64 C(998244853);
ull qpow(ull x,ull y,ull mo)
{
ull s=1;
while(y)
{
if(y&1)s=s*x%mo;
x=x*x%mo;
y>>=1;
}
return s;
}
ll gcd(ll x,ll y){return !y?x:gcd(y,x%y);}
bool check(ll x,ll p)
{
if(x%p==0)return 1;
if(qpow(p%x,x-1,x)!=1)return 1;
ll k=x-1,d;
while(k)
{
d=qpow(p,k,x);
if(d!=1&&d!=x-1)return 1;
if(((k&1)==1)||d==x-1)return 0;
k>>=1;
}
return 0;
}
bool MR(ll x)
{
if(x<2)return 0;
for(int i=0;i<=10;i++)
{
if(x==p[i])return 1;
if(check(x,p[i]))return 0;
}
return 1;
}
ll PR(ll x)
{
ull s=0,t=0,val=1,d,c=C()%(x-1)+1;
for(int g=1;;g<<=1,val=1,s=t)
{
for(int i=1;i<=g;i++)
{
t=(t*t%x+c)%x,val=val*llabs(t-s)%x;
if(i&127==0)
{
d=gcd(val,x);
if(d>1)return d;
}
}
d=gcd(val,x);
if(d>1)return d;
}
}
void getfac(ll x)
{
if(x<2)return;
if(MR(x)){fac[++cnt]=x;return;}
ll tmp=x;
while(tmp==x)tmp=PR(x);
getfac(tmp),getfac(x/tmp);
}
ll ord(ll x)
{
ll ans=phi;
for(int i=1,j;i<=cnt;i=j)
{
for(j=i;j<=cnt&&fac[i]==fac[j];j++)
if(qpow(x,ans/fac[i],m)==1)ans/=fac[i];
else break;
for(;j<=cnt&&fac[i]==fac[j];j++);
}
return ans;
}
void work()
{
scanf("%lld%lld",&m,&n);
getfac(m);
phi=m/fac[1]*(fac[1]-1);
cnt--;
getfac(fac[1]-1);
while(n--)
{
scanf("%lld%lld",&a,&b);
ord(a)%ord(b)==0?puts("Yes"):puts("No");
}
}
}
int main()
{
FGF::work();
return 0;
}