LOJ#162. 快速幂 2(分块)
生活随笔
收集整理的这篇文章主要介绍了
LOJ#162. 快速幂 2(分块)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
题面
传送门
题解
orzljz
我们分块,设\(s=\sqrt{p}+1\),那么\(x^a\)可以拆成\((x^s)^{a/s}\)和\(x^{a\bmod s}\),\(O(s)\)预处理,\(O(1)\)计算就可以了
//minamoto #include<bits/stdc++.h> #define R register #define inline __attribute__((always_inline)) #define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i) #define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i) #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v) using namespace std; char buf[1<<21],*p1=buf,*p2=buf; inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;} int read(){R int res,f=1;R char ch;while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');return res*f; } char sr[1<<21],z[20];int C=-1,Z=0; inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;} void print(R int x){if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;while(z[++Z]=x%10+48,x/=10);while(sr[++C]=z[Z],--Z);sr[++C]=' '; } const int N=50005,P=998244352; inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;} inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;} inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;} int ksm(R int x,R int y){R int res=1;for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;return res; } int bin[N],bs[N],n,x,s,a; int main(){ // freopen("testdata.in","r",stdin);x=read(),n=read(),s=sqrt(P)+1;bin[0]=1;fp(i,1,s)bin[i]=mul(bin[i-1],x);bs[0]=1;fp(i,1,s)bs[i]=mul(bs[i-1],bin[s]);while(n--)a=read(),print(mul(bs[a/s],bin[a%s]));return Ot(),0; }转载于:https://www.cnblogs.com/bztMinamoto/p/10688045.html
《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的LOJ#162. 快速幂 2(分块)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: SQL-18 查找当前薪水(to_dat
- 下一篇: 程序猿修仙之路--算法之直接插入排序