我的C++模板

我的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;
	}//printf("%d",result);

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;  //去掉所有的2 
	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()
{
    //freopen("divcnt3.in","r",stdin);
    //freopen("divcnt3.out","w",stdout);
    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;//用来记录prime的个数
 
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;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值