我的C++模板
1.读入优化模板
inline void read(int &x){
x=0;int f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
x*=f;
}
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
inline void read(ll &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
#undef ll
#undef OUT_SIZE
#undef BUF_SIZE
};
2.快速幂模板
int fastpow(int a,int b)
{
int r=1,base=a;
while(b){
if(b&1)r*=base;
base*=base;
b>>=1;
}
return r;
}
typedef long long ll;
ll fastpow(ll x,ll y,ll mod)
{
ll ans=1;
for(;y;y>>=1,x=x*x%mod)
if(y&1)
ans=ans*x%mod;
return ans;
}
3.快速乘模板
ll fast_multi(ll m,ll n,ll mod)
{
ll ans=0;
while(n){
if(n&1)
ans+=m;
m=(m+m)%mod;
m%=mod;
ans%=mod;
n>>=1;
}
return ans;
}
4.二分模板
while(l<=r){
mid=(l+r)>>1;
if(got(mid)){
result=mid;
l=mid+1;
}
else
r=mid-1;
}
5.GCD模板
inline ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
}
inline ll gcd(ll x,ll y)
{
if(!x) return y;
if(!y) return x;
ll i,j;
for(i=0;!(x&1);++i) x>>=1;
for(j=0;!(y&1);++j) y>>=1;
if(j<i) i=j;
while(1){
if(x<y) swap(x,y);
if(!(x-=y)) return y<<i;
while(!(x&1)) x>>=1;
}
}
6.并查集模板
#include <bits/stdc++.h>
#define MAXN 10005
using namespace std;
int mset[MAXN];
int find(int n){
if(mset[n]==n) return n;
return mset[n]=find(mset[n]);
}
int main()
{
int n,m,a,b,c;
cin>>n>>m;
for(int i=1;i<=n;++i)
mset[i]=i;
for(int i=1;i<=m;++i){
scanf("%d%d%d",&a,&b,&c);
if(a==1)
mset[find(b)]=find(c);
else
if(find(b)==find(c))
printf("Y\n");
else
printf("N\n");
}
return 0;
}
7.洲阁筛模板
#include <bits/stdc++.h>
#define MMax(x,y) (((x)>(y))?(x):(y))
#define MMin(x,y) (((x)<(y))?(x):(y))
int Prime[MAXN/10],pn;
int D3[MAXN+10],Fir[MAXN+10];
bool vis[MAXN+10];
int CntP[SQRTMAXN],SumP1[SQRTMAXN];
int L0[SQRTMAXN],L1[SQRTMAXN];
LL g0[SQRTMAXN],g1[SQRTMAXN],f0[SQRTMAXN],f1[SQRTMAXN];
void GetPrime();
void GetG(int SqrtN,int PN,int n);
void GetF(int SqrtN,int PN,int n);
LL GetSum(LL n);
int main()
{
GetPrime();
int T;
scanf("%d",&T);
while (T--)
{
LL n;
scanf("%lld",&n);
printf("%lld\n",GetSum(n));
}
return 0;
}
void GetPrime()
{
D3[1]=1;
for (int i=2;i<=MAXN;++i)
{
if (!vis[i])
Prime[pn++]=i,D3[i]=4,Fir[i]=i;
for (int j=0;j<pn;++j){
if (i*Prime[j]>MAXN) break;
vis[i*Prime[j]]=1;
if (i%Prime[j]==0){
if (i/Fir[i]==1) D3[i*Prime[j]]=D3[i]+3;
else D3[i*Prime[j]]=D3[i/Fir[i]]*D3[Prime[j]*Fir[i]];
Fir[i*Prime[j]]=Fir[i]*Prime[j];break;
}
else D3[i*Prime[j]]=D3[i]*4,Fir[i*Prime[j]]=Prime[j];
}
}
}
void GetG(int SqrtN,int PN,LL n)
{
for (int i=1;i<SqrtN;++i) g0[i]=i,g1[i]=n/i;
for (int i=0;i<PN;++i)
{
for (int j=1;j<SqrtN && i<L1[j];++j)
{
LL y=n/j/Prime[i];
g1[j]-=((y<SqrtN)?
(g0[y]-MMax(0,MMin(i,CntP[y])-L0[y])):
(g1[n/y]-MMax(0,i-L1[n/y])));
}
for (int j=SqrtN-1;j>=1 && i<L0[j];--j)
{
LL y=j/Prime[i];
g0[j]-=(g0[y]-MMax(0,MMin(i,CntP[y])-L0[y]));
}
}
for (int i=1;i<SqrtN;++i)
g0[i]-=CntP[i]-L0[i]+1,
g1[i]-=MMax(0,PN-L1[i])+1;
for (int i=1;i<SqrtN;++i) g0[i]*=4,g1[i]*=4;
}
void GetF(int SqrtN,int PN,LL n)
{
for (int i=1;i<SqrtN;++i) f0[i]=f1[i]=1;
for (int i=PN-1;i>=0;--i)
{
for (int j=1;j<SqrtN && i<L1[j];++j)
{
LL y=n/j/Prime[i];
for (int c=1;y;y /=Prime[i],++c)
f1[j]+=(3*c+1)*((y<SqrtN)?
(f0[y]+4*MMax(0,CntP[y]-MMax(i+1,L0[y]))):
(f1[n/y]+4*MMax(0,PN-MMax(i+1,L1[n/y]))));
}
for (int j=SqrtN-1;j>=1 && i<L0[j];--j)
{
int y=j/Prime[i];
for (int c=1;y;y/=Prime[i],++c)
f0[j]+=
(3*c+1)*
(f0[y]+4*MMax(0,CntP[y]-MMax(i+1,L0[y])));
}
}
for (int i=1;i<SqrtN;++i)
f0[i]+=4*MMax(0,CntP[i]-L0[i]),
f1[i]+=4*(PN-L1[i]);
}
LL GetSum(LL n)
{
int SqrtN=1,PN=0;
for (;LL(SqrtN)*SqrtN<=n;++SqrtN);
for (;LL(Prime[PN])*Prime[PN]<=n;++PN);
L0[0]=0;
for (int i=1;i<SqrtN;++i) for (L0[i]=L0[i-1];LL(Prime[L0[i]])*Prime[L0[i]]<=i;++L0[i]);
L1[SqrtN]=0;
for (int i=SqrtN-1;i>=1;--i)
{
LL x=n/i;
for (L1[i]=L1[i+1];LL(Prime[L1[i]])*Prime[L1[i]]<=x;++L1[i]);
}
CntP[0]=0;
for (int i=1;i<SqrtN;++i)
for (CntP[i]=CntP[i-1];Prime[CntP[i]]<=i;++CntP[i]);
GetG(SqrtN,PN,n);GetF(SqrtN,PN,n);
LL Ans=f1[1];
for (LL i=1;i<SqrtN;++i) Ans+=D3[i]*g1[i];
return Ans;
}
8.莫比乌斯反演模板
const int N = 1e6 + 5;
int mob[N], vis[N], prime[N];
int tot;
void Mobius(int n){
memset(prime,0,sizeof(prime));
memset(mob,0,sizeof(mob));
memset(vis,0,sizeof(vis));
tot = 0, mob[1] = 1;
for(int i = 2; i <=n; i ++){
if(!vis[i]){
prime[tot++] = i;
mob[i] = -1;
}
for(int j = 0; j < tot && i * prime[j] <=n ; j ++){
vis[i * prime[j]] = 1;
if(i % prime[j]) mob[i * prime[j]] = -mob[i];
else{
mob[i * prime[j]] = 0;
break;
}
}
}
}