快读&快写
普通:
template<class T>
inline void read(T& x){
int f = 0;
char ch = getchar();
while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48) , ch = getchar();
x = f ? -x : x;
}
更快:
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template <typename T>
inline void read(T& r) {
r=0;bool w=0; char ch=getchar();
while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
r=w?-r:r;
}
inline void out(int a){
if(x<0){
putchar('-');
x=-x;
}
if(a>9)write(x/10);
putchar(a%10+'0');
}
格式控制
cout << setiosflags(ios::left);
cout << setw(maxn[c] + 1) << str[r][c];
cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n
cout << setw(3) << setfill('0')
随机数
//rand()
#include<stdlib>
srand(time(0));
rand();//[0,2^15)
rand()<<15|rand(); //大于2^15
rand()%n; //[0,n)
//C++11 myrand
#include<random>
mt19937 myrand(time(0));
myrand(); //unsigned int
mt19937_64 myrand(time(0));
myrand(); //unsigned long long
memset
原理是把每个字节设成预定值。
memset(a, 0, sizeof(a));
memset(a, -1, sizeof(a));
memset(a, 0x3f, sizeof(a)); //1061109567