SAM
struct SAMV
{
int a[27],fail,mx,lg;
}b[N];
int BSAM(int nw)
{
++b0;
fo(i,1,26)b[b0].a[i]=0;
b[b0].fail=0;
b[b0].mx=nw;
nw=a[nw];
for(;!b[la].a[nw];la=b[la].fail)b[la].a[nw]=b0;
int q=la;la=b0;
if(b[q].a[nw]!=b0)
{
int w=b[q].a[nw];
if(b[q].mx+1==b[w].mx)b[b0].fail=w;
else
{
b[++b0]=b[w];
b[b0].mx=b[q].mx+1;
b[w].fail=b0;
for(;b[q].a[nw]==w;q=b[q].fail)b[q].a[nw]=b0;
b[b0-1].fail=b0;
return b0-1;
}
}
return b0;
}
Pollard Rho
LL MO(LL q,LL w,LL mo)
{
LL ans=0;
for(;w;w>>=1,q=(q+q)%mo)if(w&1)ans=(ans+q)%mo;
return ans;
}
LL ksm(LL q,LL w,LL mo)
{
LL ans=1;
for(;w;w>>=1,q=MO(q,q,mo))if(w&1)ans=MO(ans,q,mo);
return ans;
}
LL RD(LL mo){return (LL)rand()*(LL)rand()%mo;}
bool MillerR(LL q)
{
if(q==2||q==3||q==5||q==7||q==11||q==13)return 1;
if(q==1||!(q&1)||!(q%3)||!(q%5)||!(q%7)||!(q%11)||!(q%13))return 0;
fo(i,1,5)if(ksm(RD(q-1)+1,q-1,q)!=1)return 0;//这里是有问题的请不要在意
return 1;
}
LL gcd(LL x,LL y){return y<1?x:(gcd(y,x%y));}
void PollarR(LL n)
{
if(!(n&1))for(;!(n&1);n>>=1)pr[++pr[0]]=2;
if(n<2)return;
if(MillerR(n)){pr[++pr[0]]=n;return;}
LL x1=0,x2=0,C,P=1;
printf("%lld\n",n);
for(;P<2||P==n;P=gcd(abs(x1-x2),n))
{
x2=(MO(x2,x2,n)+C)%n,x2=(MO(x2,x2,n)+C)%n;
x1=(MO(x1,x1,n)+C)%n;
while(x1==x2)x1=C=RD(n),x2=(MO(C,C,n)+C)%n;
}
PollarR(P);
PollarR(n/P);
}
Treap
struct qqww
{
int l,r,fa,si,rd,v;
LL ans;
}b[N];
void merge(int q)
{
b[q].si=b[b[q].l].si+b[b[q].r].si+1;
b[q].ans=b[b[q].l].ans+b[b[q].r].ans+b[q].v;
}
void build(int q)
{
if(b[q].l)build(b[q].l);
if(b[q].r)build(b[q].r);
merge(q);
}
void build_treap()
{
int la,w;
n++;b[n].rd=2e9;
fo(i,1,n)
{
int la=0;
for(;za[0]&&b[za[za[0]]].rd<b[i].rd;za[0]--)
{
la=w=za[za[0]];
if(za[0]-1)b[w].fa=za[za[0]-1],b[za[za[0]-1]].r=w;
}
if(i>n)break;
b[i].l=la;
b[la].fa=i;
za[++za[0]]=i;
}
root=n;
build(root);
}
int findS(int q,int si)
{
if(b[b[q].l].si<=si)return findS(b[q].l,si);
si-=b[b[q].l].si+1;
if(!si)return q;
return findS(b[q].r,si);
}
TRP split(int q,int si)
{
if(!si)return TRP(0,q);
TRP t;
if(si<=b[b[q].l].si)
{
t=split(b[q].l,si);
b[q].l=t.second;
t.second=q;
}else
{
t=split(b[q].r,si-1-b[b[q].l].si);
b[q].r=t.first;
t.first=q;
}
merge(q);
return t;
}
int amalgamate(int q,int w)
{
if(!q||!w)return q+w;
if(b[q].rd>b[w].rd)
{
b[q].r=amalgamate(b[q].r,w);
merge(q);
return q;
}
b[w].l=amalgamate(q,b[w].l);
merge(w);
return w;
}
FFT
struct FFT
{
db x,y;
FFT(db _x=0,db _y=0){x=_x;y=_y;}
friend FFT operator +(FFT q,FFT w){return FFT(q.x+w.x,q.y+w.y);}
friend FFT operator -(FFT q,FFT w){return FFT(q.x-w.x,q.y-w.y);}
friend FFT operator *(FFT q,FFT w){return FFT(q.x*w.x-q.y*w.y,q.x*w.y+q.y*w.x);}
}a[N],b[N],ans[N],c[N],ans1[N],a1[N];
void DFT(FFT *a,int K,int n,int ws)
{
fo(i,0,n-1)
{
int q=0;
for(int j=i,k=ws;k;k--,j>>=1)q=(q<<1)+(j&1);
c[q]=a[i];
}
for(int I=2;I<=n;I<<=1)
{
int mid=I/2;
fo(j,0,mid-1)
{
FFT w(cos(j*PI*K/mid),sin(j*PI*K/mid));
for(int i=j;i<n;i+=I)
{
FFT q=w*c[i+mid];
c[i+mid]=c[i]-q;c[i]=c[i]+q;
}
}
}
fo(i,0,n-1)a[i]=c[i];
if(K<0)fo(i,0,n-1)a[i].x=a[i].x/n;
}
void FFT(FFT *a,FFT *b,FFT *ans,int n)
{
int m=1,ws=1;
for(;m<=n;m<<=1,ws++);
m<<=1;
fo(i,n+1,m)a[i].x=a[i].y=0,b[i]=a[i];
DFT(a,1,m,ws);
DFT(b,1,m,ws);
fo(i,0,m-1)ans[i]=a[i]*b[i];
DFT(ans,-1,m,ws);
}
NNT
struct DXS
{
int n,ws;
LL f[N*4],fn[N*4],g[N*4],c[N*4],cc[N*4],c1[N*4],c2[N*4];
LL W[N*4],W0;
int Pre(int m)
{
ws=0;
for(W0=1;W0<=m;ws++,W0<<=1);
n=W0;W0<<=1;
W[0]=1;W[1]=ksm(3,(mo-1)/W0);
fo(i,2,W0)W[i]=W[i-1]*W[1]%mo;
return ws;
}
void DFT(LL *a,int n,int ws,int K)
{
fo(i,0,n-1)
{
int q=0;
for(int j=i,k=ws;k;--k,j>>=1)q=(q<<1)+(j&1);
c[q]=a[i];
}
for(int I=2;I<=n;I<<=1)
{
int mid=I>>1;
fo(j,0,mid-1)
{
LL w=(K>0)?(W[W0/I*j]):(W[W0-W0/I*j]);
for(int i=j;i<n;i+=I)
{
LL t=c[i+mid]*w%mo;
c[i+mid]=(c[i]-t)%mo;
c[i]=(c[i]+t)%mo;
}
}
}
if(K<0)
{
LL t=ksm(n,mo-2);
fo(i,0,n-1)c[i]=c[i]*t%mo;
}
}
void NNT(LL *a,LL *b,LL *Ans,int n,int ws)
{
DFT(a,n,ws,1);
fo(i,0,n-1)cc[i]=c[i];
DFT(b,n,ws,1);
fo(i,0,n-1)Ans[i]=cc[i]*c[i]%mo;
DFT(Ans,n,ws,-1);
fo(i,0,n-1)Ans[i]=c[i];
}
void NNT(LL *a,LL *Ans,int n,int ws)
{
DFT(a,n,ws,1);
fo(i,0,n-1)Ans[i]=c[i]*c[i]%mo;
DFT(Ans,n,ws,-1);
fo(i,0,n-1)Ans[i]=c[i];
}
void GNY(LL *f,LL *fn)
{
fn[0]=ksm(f[0],mo-2);c2[0]=f[0];
for(int I=2,j=1;I<=n;I<<=1,++j)
{
fo(i,(I>>1),I-1)c2[i]=f[i];
DFT(fn,(I<<1),j+1,1);
fo(i,0,(I<<1)-1)c1[i]=c[i];
DFT(c2,(I<<1),j+1,1);
fo(i,0,(I<<1)-1)fn[i]=(2LL*c1[i]-c1[i]*c1[i]%mo*c[i])%mo;
DFT(fn,(I<<1),j+1,-1);
fo(i,0,I-1)fn[i]=c[i];
fo(i,I,(I<<1))fn[i]=0;
}
}
void Doit(int m)
{
Pre(m);
GNY(f,fn);
NNT(fn,g,c1,n<<1,ws+1);
ans=c1[m];
}
}F;
Cipolla(二项式剩余)
#include <cstdio>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define min(q,w) ((q)>(w)?(w):(q))
#define max(q,w) ((q)<(w)?(w):(q))
using namespace std;
typedef int LL;
const int N=1500;
int mo;
int read(int &n)
{
char ch=' ';int q=0,w=1;
for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
if(ch=='-')w=-1,ch=getchar();
for(;ch>='0' && ch<='9';ch=getchar())q=q*10+ch-48;n=q*w;return n;
}
int m,n,ans;
LL W;
struct qqww
{
LL x,y;
qqww(LL _x=0,LL _y=0){x=_x,y=_y;}
friend qqww operator *(qqww q,qqww w){return qqww((q.x*w.x+q.y*w.y%mo*W)%mo,(q.x*w.y+q.y*w.x)%mo);}
};
LL ksm(LL q,int w,int Mo)
{
LL ans=1;q=q%Mo;(q<0?q=q+Mo:0);
for(;w;w>>=1,q=q*q%Mo)if(w&1)ans=ans*q%Mo;
return ans;
}
qqww ksm(qqww q,int w,int Mo)
{
::mo=Mo;
qqww ans(1,0);
for(;w;w>>=1,q=q*q)if(w&1)ans=ans*q;
return ans;
}
int RD(int mo){return rand()%mo;}
LL Cipolla(int n,int mo)
{
if(w==2)return 1;
LL q=ksm(n,(mo-1)>>1,mo);
if(q==0||q==mo-1)return -1;
for(q=RD(mo);ksm(q*q-n+mo,((mo-1)>>1),mo)!=mo-1;q=RD(mo));
qqww t(q,1);W=(q*q-n+mo)%mo;
t=ksm(t,((mo+1)>>1),mo);
return (t.x+mo)%mo;
}
int main()
{
int q,w,_;
srand(19260817);
for(read(_);_;_--)
{
read(q),read(w);
ans=Cipolla(q,w);
if(ans==-1)printf("No root\n");
else if(ans==w-ans)printf("%d\n",ans);
else if(ans<w-ans)printf("%d %d\n",ans,w-ans);
else printf("%d %d\n",w-ans,ans);
}
return 0;
}
LL*LL取模
有一种复杂度加多个log的方法,
或者把它拆成两半,
这里还有一种Yves___由提供的黑科技:(巧妙的利用了C++的爆炸特性)
LL mult( LL A, LL B, LL Mo )
{
LL temp = ( ( LL ) ( ( long double ) A*B/Mo+1e-6 ) * Mo );
return A*B - temp;
}
这个是CTY大爷的版子
LL MO(LL q,LL w,LL mo)
{
q%=mo;w%=mo;
LL tmp=(LL)((long double)q*w/mo+1e-8)*mo;
return (q*w-tmp+mo)%mo;
}
(这个可能会错啊QwQ)
SA
int a[N];
int tp[N*2],tpj[N];
int rk[N],sa[N],h[N];
void TP()
{
int mx=0;
fo(i,1,n)tpj[rk[tp[i]]]++,mx=max(mx,rk[tp[i]]);
fo(i,1,mx)tpj[i]+=tpj[i-1];
fod(i,n,1)sa[tpj[rk[tp[i]]]--]=tp[i];
fo(i,0,mx)tpj[i]=0;
}
void SA()
{
fo(i,1,n)tp[i]=i,rk[i]=a[i];TP();
for(int I=1;I<=n;I*=2)
{
int q=0;
fo(i,n-I+1,n)tp[++q]=i;
fo(i,1,n)if(sa[i]-I>0)tp[++q]=sa[i]-I;
TP();
fo(i,1,n)tp[i]=rk[i];
q=0;
fo(i,1,n)
rk[sa[i]]=(tp[sa[i-1]]!=tp[sa[i]]||tp[sa[i-1]+I]!=tp[sa[i]+I])?(++q):q;
}
fo(i,1,n)if(rk[i]>1)
{
int q=max(0,h[i-1]-1);
while(a[i+q]==a[sa[rk[i]-1]+q])q++;
h[i]=q;
}
}
Splay辣鸡模板
跑的非常慢,可能是因为struct吧QwQ
void merge(int e)
{
int l=b[e].l,r=b[e].r;
b[e].mx=max(b[l].mx,max(b[r].mx,b[e].v));
b[e].mi=min(b[l].mi,min(b[r].mi,b[e].v));
b[e].co=b[l].co+b[r].co+1;
}
void UP(int q)
{
int t=b[q].fa;
if(b[t].l==q)
{
b[t].l=b[q].r;
b[b[q].r].fa=t;
b[q].r=t;
}else
{
b[t].r=b[q].l;
b[b[q].l].fa=t;
b[q].l=t;
}
if(b[b[t].fa].l==t)b[b[t].fa].l=q;
else b[b[t].fa].r=q;
b[q].fa=b[t].fa;
b[t].fa=q;
merge(t);
merge(q);
}
bool SD(int q){return q==b[b[q].fa].l;}
void rotate(int q,int w)
{
while(b[q].fa!=w)
{
if(b[b[q].fa].fa!=w)
if(SD(q)==SD(b[q].fa))UP(b[q].fa);
else UP(q);
UP(q);
}
if(!w)root=q;
}
int search(int q,int w)
{
if(b[b[q].l].co>=w)return search(b[q].l,w);
w-=b[b[q].l].co+1;
return w?search(b[q].r,w):q;
}
SPFA(带俩优化)
void SPFA(int q)
{
int S,T;
d[S=T=1]=q;z[q]=1;
fo(i,1,n)dis[i]=INF;dis[q]=0;
LL Alls=0;
for(;S<=T;)
{
q=d[S];++S;
if((LL)dis[q]*(T-S+2LL)>Alls){d[++T]=q;continue;}
Alls-=(LL)dis[q];
efo(i,q)if(dis[B[i][1]]>dis[q]+B[i][2])
{
dis[B[i][1]]=dis[q]+B[i][2];
if(!z[B[i][1]])
{
z[B[i][1]]=1;Alls+=(LL)dis[B[i][1]];
if(dis[B[i][1]]<dis[d[S]])d[--S]=B[i][1];
else d[++T]=B[i][1];
}
}
z[q]=0;
}
}
Sublime Text
Sublime Text中的C++编译文件
(我也不知道为什么要放上来)
{
"encoding": "utf-8",
"working_dir": "$file_path",
"shell_cmd": "g++ -Wall -std=c++11 \"$file_name\" -o \"$file_base_name\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c++",
"variants":
[
{
"name": "build",
"shell_cmd": "g++ \"$file\" -o \"$file_base_name\" -g"
},
{
"name": "Run",
"shell_cmd": "g++ \"$file\" -o \"$file_base_name\" -g && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\""
},
{
"name": "Run Full Stack",
"shell_cmd": "g++ \"$file\" -o \"$file_base_name\" -Wl,--stack=268435456 && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\""
},
{
"name": "Run with o2",
"shell_cmd": "g++ \"$file\" -o \"$file_base_name\" -Wl,--stack=268435456 && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\""
},
{
"name": "Open GDB",
"shell_cmd": "start cmd /k gdb \"${file_base_name}\""
}
]
}