各种实用模板或指令
作者:互联网
1. 指令
- 网络编译
#ifndef ONLINE_JUDGE
freopen...
#endif
2. 优化模板
- cin优化
std::ios::sync_with_stdio(false);
- 编译优化(火车头)
点击查看代码
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wattributes"
# pragma GCC diagnostic ignored "-Wpragmas"
# pragma GCC diagnostic error "-std=c++11"
# pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-falign-functions,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)
# pragma GCC target("avx","sse2")
# pragma GCC optimize(3)
# pragma GCC optimize("Ofast")
# pragma GCC optimize("inline")
# pragma GCC optimize("-fgcse")
# pragma GCC optimize("-fgcse-lm")
# pragma GCC optimize("-fipa-sra")
# pragma GCC optimize("-ftree-pre")
# pragma GCC optimize("-ftree-vrp")
# pragma GCC optimize("-fpeephole2")
# pragma GCC optimize("-ffast-math")
# pragma GCC optimize("-fsched-spec")
# pragma GCC optimize("unroll-loops")
# pragma GCC optimize("-falign-jumps")
# pragma GCC optimize("-falign-loops")
# pragma GCC optimize("-falign-labels")
# pragma GCC optimize("-fdevirtualize")
# pragma GCC optimize("-fcaller-saves")
# pragma GCC optimize("-fcrossjumping")
# pragma GCC optimize("-fthread-jumps")
# pragma GCC optimize("-funroll-loops")
# pragma GCC optimize("-freorder-blocks")
# pragma GCC optimize("-fschedule-insns")
# pragma GCC optimize("inline-functions")
# pragma GCC optimize("-ftree-tail-merge")
# pragma GCC optimize("-fschedule-insns2")
# pragma GCC optimize("-fstrict-aliasing")
# pragma GCC optimize("-falign-functions")
# pragma GCC optimize("-fcse-follow-jumps")
# pragma GCC optimize("-fsched-interblock")
# pragma GCC optimize("-fpartial-inlining")
# pragma GCC optimize("no-stack-protector")
# pragma GCC optimize("-freorder-functions")
# pragma GCC optimize("-findirect-inlining")
# pragma GCC optimize("-fhoist-adjacent-loads")
# pragma GCC optimize("-frerun-cse-after-loop")
# pragma GCC optimize("inline-small-functions")
# pragma GCC optimize("-finline-small-functions")
# pragma GCC optimize("-ftree-switch-conversion")
# pragma GCC optimize("-foptimize-sibling-calls")
# pragma GCC optimize("-fexpensive-optimizations")
# pragma GCC optimize("inline-functions-called-once")
# pragma GCC optimize("-fdelete-null-pointer-checks")
# pragma GCC diagnostic pop
#define Finline __inline__ __attribute__ ((always_inline))
Finline char get_char(){
static char READBUF[200000001], *READP1 = READBUF, *READP2 = READBUF + fread(READBUF, 1, 200000000, stdin);
return READP1 == READP2 ? EOF : *READP1 ++;
}
- O2优化
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
- min函数优化
inline int min (int a, int b) {
int c = (a - b) >> 31;
return a ^ c | b ^ ~c;
}
- 常数优化
#define re register
#define il inline
在自定义函数前加上inline,在循环内加上register,可提升少许运行速度,例如:
il int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
for (re int i = a;i <= b;++ i)
- 提升效率的头部注释
freopen:
#define fin(a) freopen (#a".in","r",stdin)
#define fout(a) freopen (#a".out","w",stdout)
循环:
#define rep(i,a,b) for (re int i = a;i <= b;++ i)
#define Rep(i,a,b) for (re int i = a;i < b;++ i)
#define drep(i,a,b) for (re int i = a;i >= b;-- i)
3.函数模板
- 快读快写函数
il ll read() {
ll x = 0;
char ch = 0;
while(!isdigit(ch)) {
ch = getchar();
}
while(isdigit(ch)) {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x;
}
il void write(ll x) {
if(x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
- 幂函数
il ll Pow(ll a,ll b) {
ll ans = 1;
while(b) {
if(b & 1)
ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans % MOD;
}
- 组合数
il ll C(ll n,ll m) {
if (m > n)
return 0;
if (m > n - m)
m = n - m;
ll s1 = 1,s2 = 1;
Rep(i,0,m) {
s1 = s1 * (n - i) % MOD;
s2 = s2 * (i + 1) % MOD;
}
return s1 * Pow(s2,MOD - 2) % MOD;
}
- 卢卡斯定理
il ll Lucas (int n,int m) {
if (!m)
return 1;
return C(n % MOD,m % MOD) * Lucas(n / MOD,m / MOD) % MOD;
}
标签:GCC,functions,实用,指令,pragma,inline,optimize,模板,MOD 来源: https://www.cnblogs.com/StudyingVeyron-MUST/p/16675898.html