跳到正文

算法·理论

模板题

Void_Rift约 23 分钟
原文

这是 2026年3月22日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月9日
收录
2026年3月22日
更新
2026年11月19日
篇幅
约 23 分钟
获赞
1

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1

普及−\color{#CA8920}普及−

P1226 【模板】快速幂

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,p;
int quick_pow(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b & 1)
		{
			res*=a;
            res%=p;
		}
		a*=a;
        a%=p;
		b>>=1;
	}
	return res;
}
signed main()
{
	cin>>a>>b>>p;
	cout<<a<<"^"<<b<<" mod "<<p<<"="<<quick_pow(a,b);
	return 0;
}

P3370 【模板】字符串哈希

C++
#include<bits/stdc++.h>
using namespace std;
map<string,int> q;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n,cnt=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string x;
		cin>>x;
		q[x]++;
	}
	for(auto x : q)
	{
		if(x.second!=0)
		{
			cnt++;
		}
	}
	cout<<cnt;
	return 0;
}

P3383 【模板】线性筛素数

C++
#include<bits/stdc++.h>
#define N 100000010
using namespace std;
int prim[N],cnt;
bool vis[N];
void get_prim(int n)
{
	for(int i=2;i<=n;i++)
	{
		if(!vis[i])
		{
			prim[++cnt]=i;
		}
		for(int j=1;1ll*i*prim[j]<=n;j++)
		{
			vis[i*prim[j]]=true;
			if(i%prim[j]==0)
			{
				break;
			}
		}
	}
}
int main()
{
	int n,q,k;
	scanf("%d%d",&n,&q);
	get_prim(n);
	for(int i=1;i<=q;i++)
	{
		scanf("%d",&k);
		printf("%d\n",prim[k]);
	}
	return 0; 
}

B3614 【模板】栈

C++
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
int main()
{
    ull t,m;
    string n;
    cin>>t;
    while(t--)
    {
        stack<ull> s;
        cin>>m;
        while(m--)
        {
            cin>>n;
            if(n=="push")
            {
                ull x;
                cin>>x;
                s.push(x);
            }
            else if(n=="pop")
            {
                if(s.empty())
                {
                    cout<<"Empty\n";
                }
                else
                {
                    s.pop();
                }
            }
            else if(n=="query")
            {
                if(s.empty())
                {
                    cout<<"Anguei!\n";
                }
                else
                {
                    cout<<s.top()<<"\n";
                }
            }
            else
            {
                cout<<s.size()<<"\n";
            }
        }
    }
    return 0;
}

B3616 【模板】队列

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
queue<int> q;
int n,op;
signed main()
{
    cin>>n;
    while(n--)
    {
        cin>>op;
        if(op==1)
        {
            int x;
            cin>>x;
            q.push(x);
        }
        else if(op==2)
        {
            if(q.empty())
            {
                cout<<"ERR_CANNOT_POP\n";
                continue;
            }
            q.pop();
        }
        else if(op==3)
        {
            if(q.empty())
            {
                cout<<"ERR_CANNOT_QUERY\n";
                continue;
            }
            cout<<q.front()<<"\n";
        }
        else if(op==4)
        {
            cout<<q.size()<<"\n";
        }
    }
    return 0;
}

B3656 【模板】双端队列 1

方法可行但是deque占用内存过大所以炸了(MLE)
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
int T;
string op;
deque<int> q[N];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>T;
    while(T--)
    {
        cin>>op;
        if(op=="push_back")
        {
            int x,y;
            cin>>x>>y;
            q[x].push_back(y);
        }
        else if(op=="pop_back")
        {
            int x;
            cin>>x;
            if(!q[x].empty()) q[x].pop_back();
        }
        else if(op=="push_front")
        {
            int x,y;
            cin>>x>>y;
            q[x].push_front(y);
        }
        else if(op=="pop_front")
        {
            int x;
            cin>>x;
            if(!q[x].empty()) q[x].pop_front();
        }
        else if(op=="size")
        {
            int x;
            cin>>x;
            cout<<q[x].size()<<"\n";
        }
        else if(op=="front")
        {
            int x;
            cin>>x;
            cout<<q[x].front()<<"\n";
        }
        else if(op=="back")
        {
            int x;
            cin>>x;
            cout<<q[x].back()<<"\n";
        }
    }
}

P10815 【模板】快速读入

仅本题写法(只是我不经常用)

C++
#include<bits/stdc++.h>
#define int long long
#define getchar getchar_unlocked
/*
Windos:_getchar_nolock(仅本地可编译)
Linux:getchar_unlocked
平时尽量不用解线程锁
*/
using namespace std;
const int N=1e7+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
int n,ans=0;
int read()
{
	int k=0,f=1,c=getchar();
	for(;!isdigit(c);c=getchar())
	{
		if(c=='-') f=-1;
	}
	for(;isdigit(c);c=getchar()) k=k*10+(c^48);
	return k*f;
/*
bool isdigit(c)
{
	return ((c>='0' && c<='9')? true : false);
}
*/
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	//由于不会输出优化所以还是关一下同步流(以后可能改) 
	n=read();
	while(n--)
	{
		int x=read();
		ans+=x;
	}
//	while(n--) ans+=read();
	cout<<ans;
//	printf("%d",ans);
	return 0;
}

快读纯享版

C++
int read()
{
	int k=0,f=1,c=getchar();
	for(;!isdigit(c);c=getchar())
	{
		if(c=='-') f=-1;
	}
	for(;isdigit(c);c=getchar()) k=k*10+(c^48);
	return k*f;
}

P1601 高精度加法

C++
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a[210],b[210],c[210];
void z(string s,int d[210])
{
	int len=s.size();
	for(int i=0;i<len;i++)
	{
		d[i]=s[len-1-i]-'0';
	}
}
int main()
{
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>s1>>s2;
	int len1=s1.size(),len2=s2.size(),len;
	z(s1,a);
	z(s2,b);
	len=max(len1,len2)+1;
	for(int i=0;i<len;i++)
	{
		c[i]+=a[i]+b[i];
		if(c[i]>=10)
		{
			c[i]=c[i]%10;
			c[i+1]++;
		}
	}
	while(c[len]==0 && len>0)
	{
		len--;
	}
	for(int i=len;i>=0;i--)
	{
		cout<<c[i];
	}
	return 0;
}

普及/提高−\color{#E0AB19}普及/提高−

P3366 【模板】最小生成树

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10;
struct node
{
    int u,v,w;
}s[N];
int n,m,k,p[N];
int find(int x)
{
	if(p[x]==x)
	{
		return x;
	}
	return p[x]=find(p[x]);
}
void merge(int x,int y)
{
	int t1=find(x),t2=find(y);
	if(t1!=t2)
	{
		p[t2]=t1;
	}
}
bool cmp(node x,node y)
{
	return x.w<y.w;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
		p[i]=i;
	} 
    for(int i=1;i<=m;i++)
    {
    	cin>>s[i].u>>s[i].v>>s[i].w;
	}
	sort(s+1,s+m+1,cmp);
	int cnt=0,ans=0;
	for(int i=1;i<=m;i++)
	{
		int u=s[i].u,v=s[i].v,w=s[i].w;
		if(find(u)!=find(v))
		{
			merge(u,v);
			cnt++;
			ans+=w;
			if(cnt==n-1)
			{
				cout<<ans;
				return 0;
			}
		}
	}
	cout<<"orz";
    return 0;
}

P3367 【模板】并查集

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10;
int n,m,x,p[N],op;
int find(int x)
{
	if(p[x]==x)
	{
		return x;
	}
	return p[x]=find(p[x]);
}
void merge(int x,int y)
{
	int t1=find(x),t2=find(y);
	if(t1!=t2)
	{
		p[t2]=t1;
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		p[i]=i;
	}
	for(int i=1,x,y;i<=m;i++)
	{
		cin>>op>>x>>y;
		if(op==1)
		{
			merge(x,y);
		}
		else
		{
			if(find(x)==find(y))
			{
				cout<<"Y\n";
			}
			else
			{
				cout<<"N\n";
			}
		}
	}
	return 0;
}

B3644 【模板】拓扑排序 / 家谱树

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10;
int n,rd[N];
vector<int> v[N];
queue<int> q;
signed main()
{
	cin>>n;
	for(int i=1,x;i<=n;i++)
	{
		while(true)
		{
			cin>>x;
			if(x==0)
			{
				break;
			}
			v[i].push_back(x);
			rd[x]++;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(rd[i]==0)
		{
			q.push(i);
		}
	}
	while(!q.empty())
	{
		int x=q.front();
		cout<<x<<" ";
		q.pop();
		for(auto i:v[x])
		{
			rd[i]--;
			if(rd[i]==0)
			{
				q.push(i);
			}
		}
	}
	return 0;
}

普及+/提高\color{#50A127}普及+/提高

P3372 【模板】线段树 1

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,m,a[N],sum[4*N],add[4*N];
void build(int id,int left,int right)
{
	if(left==right)
	{
		sum[id]=a[left];
		return ;
	}
	int mid=(left+right)/2;
	build(id*2,left,mid);
	build(id*2+1,mid+1,right);
	sum[id]=sum[id*2]+sum[id*2+1];
}
void pushdown(int id,int left,int right)
{
	if(add[id]!=0)
	{
		int mid=(left+right)/2;
		sum[id*2]=sum[id*2]+(mid-left+1)*add[id];
		add[id*2]=add[id*2]+add[id];
		sum[id*2+1]=sum[id*2+1]+(right-mid)*add[id];
		add[id*2+1]=add[id*2+1]+add[id];
		add[id]=0;
	}
}
void change(int id,int left,int right,int l,int r,int x)
{
	if(left>=l && right<=r)
	{
		sum[id]=sum[id]+(right-left+1)*x;
		add[id]=add[id]+x;
		return ;
	}
	pushdown(id,left,right);
	int mid=(left+right)/2;
	if(l<=mid)
	{
		change(id*2,left,mid,l,r,x);
	}
	if(r>mid)
	{
		change(id*2+1,mid+1,right,l,r,x);
	}
	sum[id]=sum[id*2]+sum[id*2+1];
}
int query(int id,int left,int right,int l,int r)
{
	if(left>=l && right<=r)
	{
		return sum[id];
	}
	pushdown(id,left,right);
	int mid=(left+right)/2,ans=0;
	if(l<=mid)
	{
		ans=ans+query(id*2,left,mid,l,r);
	}
	if(r>mid)
	{
		ans=ans+query(id*2+1,mid+1,right,l,r);
	}
	return ans;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	build(1,1,n);
	for(int i=1,c,l,r,x;i<=m;i++)
	{
		cin>>c>>l>>r;
		if(c==1)
		{
			cin>>x;
			change(1,1,n,l,r,x);
		}
		else
		{
			cout<<query(1,1,n,l,r)<<"\n";
		}
	}
	return 0;
}

P13825 【模板】线段树 1.5

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N=1e5+10;
struct node
{
	int lc,rc,sum,add;
}s[80*N];
int n,m,idx=1,a[N];
void pushdown(int id,int left,int right)
{
	if(s[id].add!=0)
	{
		int mid=(left+right)/2;
		s[s[id].lc].sum+=(mid-left+1)*s[id].add;
		s[s[id].lc].add+=s[id].add;
		s[s[id].rc].sum+=(right-mid)*s[id].add;
		s[s[id].rc].add+=s[id].add;
		s[id].add=0;
	}
}
void change(int id,int left,int right,int l,int r,int x)
{
	if(left>=l && right<=r)
	{
		s[id].sum+=(right-left+1)*x;
		s[id].add+=x;
		return;
	}
	if(s[id].lc==0)
	{
		s[id].lc=++idx;
	}
	if(s[id].rc==0)
	{
		s[id].rc=++idx;
	}
	pushdown(id,left,right);
	int mid=(left+right)/2;
	if(l<=mid)
	{
		change(s[id].lc,left,mid,l,r,x);
	}
	if(r>mid)
	{
		change(s[id].rc,mid+1,right,l,r,x);
	}
	s[id].sum=s[s[id].lc].sum+s[s[id].rc].sum;
}
int query(int id,int left,int right,int l,int r)
{
	if(left>=l && right<=r)
	{
		return s[id].sum;
	}
	if(s[id].lc==0)
	{
		s[id].lc=++idx;
	}
	if(s[id].rc==0)
	{
		s[id].rc=++idx;
	}
	pushdown(id,left,right);
	int mid=(left+right)/2,ans=0;
	if(l<=mid)
	{
		ans=ans+query(s[id].lc,left,mid,l,r);
	}
	if(r>mid)
	{
		ans=ans+query(s[id].rc,mid+1,right,l,r);
	}
	return ans;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1,c,l,r,x;i<=m;i++)
	{
		cin>>c>>l>>r;
		if(c==1)
		{
			cin>>x;
			change(1,1,n,l,r,x);
		}
		else
		{
			cout<<query(1,1,n,l,r)+(l+r)*(r-l+1)/2<<"\n";
		}
	}
	return 0;
}

提高+/省选−\color{#3688BE}提高+/省选−

省选/NOI−\color{#8A3EB3}省选/NOI−

NOI/NOI+/CTSC\color{#152056}NOI/NOI+/CTSC