题解
(拔山盖世)
- 其实叫 (大步小步) ,事实上还有 ,但是这里只写 。
- 当 时, 可以用 的时间复杂度求解 的问题。(原根是 )
- 基本的思路就是,设 ,其中 , ,得出 继续得出 。
- 用哈希或 存下 的所有值,再枚举 是否可以与 的值相等。显然,如果有数可以满足,就是有解的,否则无解。
- map复杂度 ,哈希复杂度 。
题解
- 和 的情况很好判断, 时写一个快速幂就好了, 时也不难,用 。首先 , 于是很容易得出 。所以 。很明显你也看出来了, 。因为这里 是个质数,所以可以把 看成 。 之后就将 扩大 倍,就得到了 。再找出最小非负整数解即可。
- 直接套 。
代码
| #include<bits/stdc++.h> |
| #define N (10000010) |
| #define int long long |
| using namespace std; |
| namespace IO |
| { |
| #define ll long long |
| const int MAX=1<<25; |
| char buf[MAX],*p1=buf,*p2=buf; |
| char obuf[MAX],*o=obuf; |
| #define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) |
| |
| |
| inline int read() |
| { |
| int x=0;bool f=1; |
| char c=gc(); |
| for(;c<48||c>57;c=gc())if(c=='-')f=0; |
| for(;c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c^48); |
| return f?x:~x+1; |
| } |
| void print(ll x){if(x>9)print(x/10);*o++=(x%10)+'0';} |
| void pit(ll x){if(x<0)*o++='-',x=~x+1;print(x);} |
| void write(ll x,char end){pit(x);*o++=end;} |
| void flush(){fwrite(obuf,o-obuf,1,stdout);} |
| #undef ll |
| } |
| using IO::read;using IO::write;using IO::flush; |
| int n,m,t,P,q,d; |
| int x,y,k,ans,possible,lon; |
| int mod[N],a[N],gt[N],nth_mod[N]; |
| int len,prime[700010],phi[N]; |
| short mu[N]; |
| int mtot; |
| bitset<N>vis; |
| int jc[N]; |
| long long inv[N]; |
| inline int max(int x,int y){return x>y?x:y;} |
| inline int min(int x,int y){return x<y?x:y;} |
| inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;} |
| int e[N],siz[N]; |
| long long qpow(long long x,int b,int P=P) |
| { |
| long long ans=1; |
| for(;b;b>>=1){if(b&1)ans=(ans*x)%P;x=(x*x)%P;} |
| return ans; |
| } |
| int gcd(int a,int b){return b?gcd(b,a%b):a;} |
| int exgcd(int a,int b,int &x,int &y) |
| { |
| if(!b){x=1,y=0;return a;} |
| int d=exgcd(b,a%b,y,x); |
| y-=(a/b*x); |
| return d; |
| } |
| int ola(int n) |
| { |
| int ans=n; |
| for(int i=2;i*i<=n;++i) |
| { |
| if(n%i==0)ans=ans/i*(i-1); |
| for(;n%i==0;n/=i); |
| } |
| if(n>1)ans=ans/n*(n-1); |
| return ans; |
| } |
| void eular(int n) |
| { |
| |
| phi[1]=1; |
| for(int i(2);i<=n;++i) |
| { |
| if(!vis[i]) |
| prime[++len]=i,phi[i]=(i-1); |
| for(int j(1);j<=len&&i*prime[j]<=n;++j) |
| { |
| vis[i*prime[j]]=1; |
| if(!(i%prime[j])) |
| {phi[i*prime[j]]=(phi[i]*prime[j]);break;} |
| else phi[i*prime[j]]=(phi[i]*(prime[j]-1)); |
| } |
| } |
| } |
| void mobius(int n) |
| { |
| |
| mu[1]=1; |
| for(int i(2);i<=n;++i) |
| { |
| if(!vis[i])prime[++mtot]=i,mu[i]=-1; |
| for(int j(1);j<=mtot&&i*prime[j]<=n;++j) |
| { |
| vis[i*prime[j]]=1; |
| if(!(i%prime[j])){mu[i*prime[j]]=0;break;} |
| mu[i*prime[j]]=~mu[i]+1; |
| } |
| } |
| } |
| void eular_mobius(int n) |
| { |
| |
| mu[1]=phi[1]=1; |
| for(int i(2);i<=n;++i) |
| { |
| if(!vis[i])prime[++len]=i,phi[i]=(i-1),mu[i]=-1; |
| for(int j(1);j<=len&&i*prime[j]<=n;++j) |
| { |
| vis[i*prime[j]]=1; |
| if(!(i%prime[j])) |
| { |
| phi[i*prime[j]]=(phi[i]*prime[j]); |
| mu[i*prime[j]]=0; |
| break; |
| } |
| phi[i*prime[j]]=(phi[i]*(prime[j]-1)); |
| mu[i*prime[j]]=~mu[i]+1; |
| } |
| } |
| } |
| void niyuan1(int n,int P=P) |
| { |
| inv[1]=1; |
| for(int i(2);i<=n;++i)inv[i]=((P-P/i)*inv[P%i])%P; |
| } |
| int inv_it(int a,int P=P) |
| { |
| int d(exgcd(a,P,x,y)); |
| return(x%P+P)%P; |
| } |
| int C(int n,int m,int P=P) |
| { |
| if(m>n)return 0; |
| int a(1),b(1); |
| for(int i(n-m+1);i<=n;++i)a=(a*i)%P; |
| for(int i(2);i<=m;++i)b=(b*i)%P; |
| return(a*qpow(b,P-2,P))%P; |
| } |
| int lucas(int n,int m,int P=P) |
| {return(!m)?1:(C(n%P,m%P,P)*lucas(n/P,m/P,P))%P;} |
| int excrt(int n) |
| { |
| int mul(mod[1]);ans=a[1]; |
| int x,y,c,d; |
| for(int i(2);i<=n;++i) |
| { |
| x=y=0; |
| c=(a[i]-ans%mod[i]+mod[i])%mod[i]; |
| d=exgcd(mul,mod[i],x,y); |
| if(!(c%d)) |
| ans+=(((x+mod[i])%mod[i])*(c/d)%mod[i])*mul, |
| mul=mul*mod[i]/gcd(mul,mod[i]), |
| ans%=mul; |
| else return -1; |
| } |
| return ans%mul; |
| } |
| int exlucas_jc(int n,int mod,int P=P) |
| { |
| if(!n)return 1; |
| int res(1); |
| for(int i(1);i<=P;++i) |
| if(i%mod)res=(res*i)%P; |
| res=qpow(res,n/P,P); |
| for(int i(1);i<=n%P;++i)if(i%mod)res=(res*i)%P; |
| return(res*(exlucas_jc(n/mod,mod,P)))%P; |
| } |
| int cal(int n,int m,int mod,int P=P) |
| { |
| int c1(exlucas_jc(n,mod,P)); |
| int c2(exlucas_jc(m,mod,P)); |
| int c3(exlucas_jc(n-m,mod,P)); |
| int cnt(0); |
| for(int i(n);i;i/=mod)cnt+=(i/mod); |
| for(int i(m);i;i/=mod)cnt-=(i/mod); |
| for(int i(n-m);i;i/=mod)cnt-=(i/mod); |
| return(((qpow(mod,cnt,P)*c1)%P*inv_it(c2,P))%P*inv_it(c3,P))%P; |
| } |
| inline int crt(int x,int mod,int P) |
| { |
| return inv_it(P/mod,mod)*(P/mod)*x; |
| } |
| int exlucas(int n,int m,int now,int P) |
| {return(crt(cal(n,m,nth_mod[now],mod[now]),mod[now],P));} |
| namespace hs |
| { |
| int nhs,mhs,hs[100010],to[100010],nxt[100010]; |
| int mod=98317,head[100010],cnmhs; |
| void clean(){memset(head,0,sizeof(head));} |
| void add(int x,int P=mod) |
| { |
| hs[++cnmhs]=x,to[cnmhs]=P,x%=mod, |
| nxt[cnmhs]=head[x],head[x]=cnmhs; |
| } |
| int find_x(int x) |
| { |
| for(int i(head[x%mod]);i;i=nxt[i]) |
| if(hs[i]==x)return to[i]; |
| return -1; |
| } |
| } |
| using namespace hs; |
| int bsgs(int x,int y,int P=P) |
| { |
| if(!(x%P))return -1; |
| x%=P,y%=P; |
| if(y==1)return 0; |
| int Z(sqrt(P)+1); |
| int xx(y),yy; |
| hs::clean(); |
| for(int i(0);i<Z;++i,xx=(xx*x)%P)hs::add(xx,i); |
| yy=qpow(x,Z),xx=1; |
| for(int i(1),k;i<=Z;++i) |
| { |
| xx=(xx*yy)%P,k=hs::find_x(xx); |
| if(k!=-1)return i*Z-k; |
| } |
| return -1; |
| } |
| signed main() |
| { |
| #ifndef ONLINE_JUDGE |
| freopen("in.txt","r",stdin); |
| freopen("out.txt","w",stdout); |
| #endif |
| ios::sync_with_stdio(0); |
| cin.tie(0),cout.tie(0); |
| m=read(),t=read(); |
| int sum(0),num(0);ans=1; |
| if(t==1) |
| for(int i(1);i<=m;++i) |
| { |
| n=read(),q=read(),P=read(); |
| cout<<qpow(n,q,P)<<'\n'; |
| } |
| else if(t==2) |
| for(int i(1);i<=m;++i) |
| { |
| n=read(),q=read(),P=read(); |
| d=exgcd(n,P,x,y); |
| if(q%d)cout<<"Orz, I cannot find x!\n"; |
| else x*=q/d,y=P/d,cout<<(x%y+y)%y<<'\n'; |
| } |
| else |
| for(int i(1);i<=m;++i) |
| { |
| n=read(),q=read(),P=read(); |
| d=bsgs(n%P,q%P,P); |
| if(d<0)cout<<"Orz, I cannot find x!\n"; |
| else cout<<d<<'\n'; |
| } |
| flush(); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)