常用模板库

C++模板

置顶

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
//#define int long long
//Fast IO
void read(int &x){
	x = 0;int h = 1;char tmp;
	do{tmp=getchar();if(tmp=='-')h*=-1;}while(!isdigit(tmp));
	while(isdigit(tmp)) x*=10,x+=tmp-'0',tmp=getchar();
	x*=h;
}
void read(char &x){do{x=getchar();}while(x==' '||x=='\n'||x=='\r');}
void write(char x){putchar(x);}
void write(int x){
	if(x<0) putchar('-'),x=-x;int st[200]={0},tot=0;
	do{st[++tot]=x%10,x/=10;} while(x);
	while(tot){putchar(st[tot--]+'0');};
}
void write(int x,char y){write(x);write(y);}
namespace gtx{
	signed main(){
		
		return 0;
	}
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int T = 1;
//	read(T);
	while(T--) gtx::main();
	return 0;
}

Before

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
//#define int long long
//Fast IO
void read(int &x){
	x = 0;int h = 1;char tmp;
	do{tmp=getchar();if(tmp=='-')h*=-1;}while(!isdigit(tmp));
	while(isdigit(tmp)) x*=10,x+=tmp-'0',tmp=getchar();
	x*=h;
}
void read(char &x){do{x=getchar();}while(x==' '||x=='\n'||x=='\r');}
void write(char x){putchar(x);}
void write(int x){
	if(x<0) putchar('-'),x=-x;int st[200]={0},tot=0;
	do{st[++tot]=x%10,x/=10;} while(x);
	while(tot){putchar(st[tot--]+'0');};
}
void write(int x,char y){write(x);write(y);}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	return 0;
}

精简(庸俗、老套

#include<bits/stdc++.h>
using namespace std;
signed main(){
	
	return 0;
}

快读 RSY

#include <bits/stdc++.h>
#define lb(x) (x&-x)
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)
#define swap(a,b) (a^=b^=a^=b)

using namespace std;
using i64 = long long;

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
void chmin(int &x, int c) {
	x = min(x, c);
}
void chmax(int &x, int c) {
	x = max(x, c);
}

namespace fast_IO{
#define IOSIZE 100000
int precision=3,POW[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};char ibuf[IOSIZE],obuf[IOSIZE],*p1=ibuf,*p2=ibuf,*p3=obuf;
#ifdef ONLINE_JUDGE
#define getchar()((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x)((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch)(ch>47&&ch<58)
#define isspace(ch)(ch<33)
#endif
template<typename T>inline T read(){T s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)&&(ch!=EOF))if(ch==45)w=-1;if(ch==EOF)return 0;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*w;}template<typename T>inline bool read(T&s){s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)&&(ch!=EOF))if(ch==45)w=-1;if(ch==EOF)return 0;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*=w,1;}inline bool read(char&s){while(s=getchar(),isspace(s));return 1;}inline bool read(char*s){char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return 0;while(!isspace(ch))*s++=ch,ch=getchar();*s='\000';return 1;}template<typename T>inline void print(T x){if(x<0)putchar(45),x=-x;if(x>9)print(x/10);putchar(x%10+48);}inline void print(char x){putchar(x);}inline void print(char*x){while(*x)putchar(*x++);}inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}inline bool read(std::string&s){s="";char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return 0;while(!isspace(ch))s+=ch,ch=getchar();return 1;}inline void print(std::string x){for(int i=0,n=x.size();i<n;i++)putchar(x[i]);}inline bool read(bool&b){char ch;while(ch=getchar(),isspace(ch));b=ch^48;return 1;}inline void print(bool b){putchar(b+48);}inline bool read(double&x){int a=0,b=0;char ch=getchar();bool f=0;while(ch<48||ch>57){if(ch==45)f=1;ch=getchar();}while(47<ch&&ch<58){a=(a<<1)+(a<<3)+(ch^48);ch=getchar();}if(ch!=46){x=f?-a:a;return 1;}ch=getchar();while(47<ch&&ch<58){b=(b<<1)+(b<<3)+(ch^48),ch=getchar();}x=b;while(x>1)x/=10;x=f?-a-x:a+x;return 1;}inline void print(double x){if(x<0){putchar(45),x=-x;}x=round((long double)x*POW[precision])/POW[precision],print((long long)x),putchar(46),x-=(long long)(x);for(int i=1;i<=precision;i++)x*=10,putchar(x+48),x-=(int)x;}template<typename T,typename...T1>inline int read(T&a,T1&...other){return read(a)+read(other...);}template<typename T,typename...T1>inline void print(T a,T1...other){print(a),print(other...);}struct Fast_IO{~Fast_IO(){fwrite(obuf,p3-obuf,1,stdout);}}io;template<typename T>Fast_IO&operator>>(Fast_IO&io,T&b){return read(b),io;}template<typename T>Fast_IO&operator<<(Fast_IO&io,T b){return print(b),io;}
#define cout io
#define cin io
#define endl '\n'
}using namespace fast_IO;
const int maxn = 3e5 + 10, mod = 998244353;


void solve() {

}

signed main() {
	int T = 1;
	while (T--)solve();
	return 0;
}

数学、数论

namespace math{
	int mu[MAXN],prime[MAXN];
	bitset<MAXN> is_prime;
	int frac[MAXN];
	int qpow(int a,int b){
		if(b==0) return 1;
		if(b==1) return a;
		int k = qpow(a,b>>1);
		k*=k;k%=MOD;
		if(b&1) k*=a;k%=MOD;
		return k;
	}
	int exgcd(int a,int b,int &x,int &y){
		if(b==0){
			x = 1;y = 0;
			return a;
		}
		int t = exgcd(b,a%b,x,y);
		int g = x;
		x = y;
		y = g-a/b*y;
		return t;
	}
	int inv(int a){
		return qpow(a,MOD-2);
	}
	int ex_inv(int a){
		int x,y;
		exgcd(a,MOD,x,y);
		return (x%MOD+MOD)%MOD;
	}
	int C(int n,int m){
		if(n<m) return 0;
		return frac[n]*ex_inv(frac[m]*frac[n-m]%MOD)%MOD;
	}
	int get(int n,int m){
		return C(m+n-1,m);
	}
	void frac_init(){
		frac[0] = 1;
		for(int i = 1;i<MAXN;i++){
			frac[i] = frac[i-1]*i;
			frac[i]%=MOD;
		}
	}
	int lowbit(int k){
		return k&(-k);
	}
	int bit(int k){
		int sum = 0;
		while(k){
			sum ++;
			k -= lowbit(k);
		}
		return sum;
	}
	int log2(int k){
		for(int i = 0;i<=64;i++){
			int p = 1;
			if(p<<i==k) return i;
		}
		return 0;
	}
	void Euler_sieve(){
		mu[1] = 1;
		for(int i = 2;i<MAXN;i++){
			if(!is_prime[i]){
				prime[++prime[0]] = i;
				mu[i] = -1;
			}
			for(int j = 1;j<=prime[0]&&i*prime[j]<MAXN;j++){
				is_prime[i*prime[j]] = 1;
				if(i%prime[j]==0){
					mu[i*prime[j]] = 0;
					break;
				}else{
					mu[i*prime[j]] = -mu[i];
				}
			}
		}
	}
}

实数模板

#include<bits/stdc++.h>
using namespace std;
struct Real{
	int p,q;
//	A real number that is p/q
};
int gcd(int a,int b){
	return __gcd(a,b);
}
int lcm(int a,int b){
	return a*b/__gcd(a,b);
}
Real operator * (Real &a,Real b){
	a.p *= b.p;
	a.q *= b.q;
	int tmp = __gcd(a.p,a.q);
	a.p /= tmp;
	a.q /= tmp;
	return a; 
}
Real operator * (Real a,int b){
	return a*(Real){b,1};
}
Real operator / (Real a,Real b){
	return a*(Real){b.q,b.p};
}
Real operator / (Real a,int b){
	return a/(Real){b,1};
}
Real operator + (Real a,Real b){
	int k = lcm(a.q,b.q);
	a.p *= k/a.q;
	a.q *= k/a.q;
	b.p *= k/b.q;
	b.q *= k/b.q;
	a.p += b.p;
	int tmp = __gcd(a.p,a.q);
	a.p /= tmp;
	a.q /= tmp;
	return a;
}
Real operator += (Real &a,Real b){
	a = a+b;
	return a;
}
long double R_v(Real a){
	return (long double)1.0*a.p/a.q;
}
void write(Real a){
	write(a.p,' ');write(a.q,'\n');
}

编译命令

D:\Dev-Cpp\MinGW64\bin\g++.exe "C:\Users\李\Desktop\CPP\对拍\P3609\data.cpp" -o "C:\Users\李\Desktop\CPP\对拍\P3609\data.exe" -g3 -std=c++14 -I"D:\Dev-Cpp\MinGW64\include" -I"D:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include" -I"D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include" -I"D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++" -L"D:\Dev-Cpp\MinGW64\lib" -L"D:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib" -static-libgcc -g3
posted @ 2024-06-03 16:29  辜铜星  阅读(17)  评论(0编辑  收藏  举报