CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
GCC 内置位运算
1
2
3
4
5
int __builtin_clz(unsigned int x); //求前缀0的个数
int __builtin_ctz(unsigned int x); //求后缀0的个数
int __builtin_ffs(unsigned int x); //x的二进制末尾最后一个1的位置,从1开始
int __builtin_popcount(unsigned int x); //x二进制中1的个数,相当于bitset::count()
int __builtin_parity(unsigned int x); //判断x的二进制中1的个数的奇(1)偶(0)性,这些函数都有相应的usigned long和usigned long long版本,只需要在函数名后面加上l或ll就可以了,比如__builtin_clzll
开栈
For C++
1
#pragma comment(linker, "/STACK:102400000,102400000") //For C++
For G++
1
2
3
4
5
6
int main() //For G++
{
int size = 256 << 20; //256MB
char *p = (char *)malloc(size) + size;
__asm__ __volatile__("movq %0, %%rsp\n" ::"r"(p)); //64bit,一定要最后写一句`exit(0);`退出程序,否则会得到非零退出的错误,可能RE。
}
读入优化
C 文件指针版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ll getll(FILE *in = stdin)
{
ll val = 0, sgn = 1, ch = getc(in);
for (; !isdigit(ch) && ch != EOF; ch = getc(in))
if (ch == '-')
sgn = -sgn;
for (; isdigit(ch); ch = getc(in))
val = val * 10 + ch - '0';
return ungetc(ch, in), sgn * val;
}
lf getlf(FILE *in = stdin)
{
lf val = getll(in), p = val < 0 ? -1 : 1;
ll ch = getc(in);
if (ch == '.')
for (ch = getc(in); isdigit(ch); ch = getc(in))
val += (p /= 10) * (ch - '0');
return ungetc(ch, in), val;
}
仿 C++IO 流沙雕版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define cin kin
struct Istream
{
char b[20 << 20], *i, *e; //20MB
Istream(FILE *in) : i(b), e(b + fread(b, sizeof(*b), sizeof(b), in)) {}
bool eof() const
{
return i == e;
}
Istream &operator>>(long long &val)
{
return val = strtoll(i, &i, 10 /*进制,取值2~36*/), *this;
}
Istream &operator>>(ll &val) //极限快
{
while (*i < '0')
++i; //无符号
for (val = 0; *i >= '0'; ++i)
val = (val << 3) + (val << 1) + *i - '0';
return *this;
}
Istream &operator>>(double &val)
{
return val = strtod(i, &i), *this;
}
Istream &operator>>(string &s)
{
while (!eof() && isspace(*i))
++i;
for (s.clear(); !eof() && !isspace(*i); ++i)
s += *i;
return *this;
}
} kin(stdin);