亿些模板

  • 快速读写
const int bufsz=1<<20;
char buf[bufsz],*p1,*p2;
#define GC (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsz,stdin),p1==p2)?EOF:*p1++)
I int read()
{
	char ch=GC;int x=0;bool w=0;
	while(ch<'0'||ch>'9'){w|=ch=='-';ch=GC;}
	while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=GC;}
	return w?-x:x;
}
char pbuf[bufsz],*p3=pbuf;
const char *p4=pbuf+bufsz;
I void flush(){fwrite(pbuf,1,p3-pbuf,stdout);p3=pbuf;}
I void PC(const char &c){*p3++=c;if(p3==p4)flush();}
I void write(int x)
{
	static int stk[20],tp;
	if(x<0)x=-x,PC('-');
	while(stk[++tp]=x%10,x/=10);
	while(PC(stk[tp]^48),--tp);
}
  • 八进制龟速乘
#define ll long long
ll qmul(ll a,ll b,ll P)//P<=1e18 
{
	ll res=0;
	for(;b;b>>=3)
	{
		res=(res+a*(b&7))%P;
		a=(a<<3)%P;
	}
	return res;
}
  • Sparse Table(卡常)
#include<bits/stdc++.h>
#define R register int
#define I inline
using namespace std;
const int bufsz=1<<21;
char buf[bufsz],*p1,*p2;
#define GC (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsz,stdin),p1==p2)?0:*p1++)
I void read(int &n)
{
	char ch=GC;int x=0;
	while(ch<'0'||ch>'9')ch=GC;
	while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=GC;}
	n=x;
}
char pbuf[bufsz],*p3=pbuf;
const char *p4=pbuf+bufsz;
I void flush(){fwrite(pbuf,1,p3-pbuf,stdout);p3=pbuf;}
I void PC(const char c){*p3++=c;if(p3==p4)flush();}
I void write(int x)
{
	static char stk[20];
	static int tp;
	while(stk[++tp]='0'+x%10,x/=10);
	while(PC(stk[tp]),--tp);
}
I unsigned int Log(unsigned int x)
{
	unsigned int ret;
	__asm__ __volatile__ ("bsrl %1, %%eax":"=a"(ret):"m"(x));
	return ret;
}
const int N=1e5+3,K=17;
int f[K][N];
int main()
{
	int n,m,l,r,k;
	read(n);read(m);
	for(R i=1;i<=n;++i)read(f[0][i]);
	for(int i=1;1<<i<=n;++i)
	{
		R *f0=f[i]+1,*f1=f[i-1]+1,*f2=f[i-1]+(1<<i-1)+1;
		const R *ed=f[i]+n-(1<<i)+2;
		for(;f0!=ed;++f0,++f1,++f2)*f0=max(*f1,*f2);
	}
	while(m--)
	{
		read(l);read(r);
		k=Log(r-l+1);
		write(max(f[k][l],f[k][r-(1<<k)+1]));
		PC('\n');
	}
	flush();
	return 0;
}
  • miller_rabin+pollard_rho
#include<bits/stdc++.h>
#define R register int
#define ll long long
#define I inline
using namespace std;
const int N=9,u[N]={2,3,5,7,11,13,17,19,23};
ll gcd(ll a,ll b)
{
	for(ll t;b;t=a%b,a=b,b=t);
	return a;
}
I ll qmul(ll a,ll b,ll P)
{
	ll c=a*b-(ll)((long double)a/P*b+0.5)*P;
	return c<0?c+P:c;
}
ll qpow(ll a,ll b,ll P)
{
	ll res=1;
	for(;b;b>>=1,a=qmul(a,a,P))
		if(b&1)res=qmul(res,a,P);
	return res;
}
bool mr(ll n,int x,int c)
{
	ll tx=qpow(x,n-1>>c,n),ty;
	while(c--&&tx!=1)
	{
		ty=qmul(tx,tx,n);
		if(tx!=n-1&&ty==1)return 0;
		tx=ty;
	}
	return tx==1;
}
bool isp(ll n)
{
	int c=__builtin_ctz(n-1);
	for(R i=0;i<N;i++)
	{
		if(u[i]>=n)return n==u[i];
		if(!mr(n,u[i],c))return 0;
	}
	return 1;
}
I ll nxt(ll t,ll c,ll P)
{
	t=qmul(t,t,P)+c;
	return t>=P?t-P:t;
}
vector<ll>fc;
void div(ll n)
{
	if(n==1)return;
	if(isp(n))
	{
		fc.push_back(n);
		return;
	}
	ll s=sqrt(n);
	if(s*s==n)return div(s);
	for(ll c=rand()%(n-1)+1;;c++)
	{
		ll t1=(rand()+c)%(n-1)+1,t2=nxt(t1,c,n),p=1,g,t;
		int sp=0;
		while(t1!=t2)
		{
			t=abs(t1-t2);
			p=qmul(t,p,n);++sp;
			if(!p)
			{
				g=gcd(t,n);
				div(n/g);div(g);
				return;
			}
			if(sp==127)
			{
				sp=0;g=gcd(p,n);
				if(g>1)return(div(g),div(n/g));
			}
			t1=nxt(t1,c,n);t2=nxt(nxt(t2,c,n),c,n);
		}
		g=gcd(p,n);
		if(g>1)return(div(g),div(n/g));
	}
}
int main()
{
	srand(time(0));
	ll n;
	scanf("%lld",&n);
	div(n);
	sort(fc.begin(),fc.end());
	fc.resize(unique(fc.begin(),fc.end())-fc.begin());
	for(auto it:fc)printf("%lld ",it);
	return 0;
}
  • FFT
const D pi=acos(-1);
struct Cp
{
	D re,im;
	Cp(D r=0,D i=0):re(r),im(i){};
	I Cp operator+(const Cp &a){return Cp(re+a.re,im+a.im);}
	I Cp operator-(const Cp &a){return Cp(re-a.re,im-a.im);}
	I Cp operator*(const Cp &a){return Cp(re*a.re-im*a.im,re*a.im+im*a.re);}
};
Cp w[M];//w[0].re=0
int r[M];
void FFT(Cp *s,int n,bool f)
{
	for(R i=0;i<n;i++)if(i<r[i])swap(s[i],s[r[i]]);
	for(R i=1;i<n;i<<=1)
	{
		const Cp wn(cos(pi/i),f?-sin(pi/i):sin(pi/i));
		for(R j=i-2>>1;~j;j--)w[j<<1|1]=(w[j<<1]=w[j])*wn;
		for(R j=0;j<n;j+=i<<1)
			for(R k=j;k<j+i;k++)
			{
				Cp x=s[k],y=s[k+i]*w[k-j];
				s[k]=x+y;s[k+i]=x-y;
			}
	}
	if(f)
	{
		const RG D in=1.0/n;
		for(R i=0;i<n;i++)s[i].re*=in,s[i].im*=in;
	}
}
posted @ 2021-01-06 15:51  nkxjlym  阅读(77)  评论(0)    收藏  举报