Contents
快速读入
int read() {
int f=1,x=0;char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return f*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;
cin 关闭流同步
关闭同步后不能用 scanf 和 printf !
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
关于 cout 输出小数点:
#include <iomanip>
int main() {
const double value = 12.3456789;
cout << value << endl; // 默认以6精度,所以输出为 12.3457
cout << setprecision(4) << value << endl;
// 改成4精度,所以输出为12.35
cout << setprecision(8) << value << endl;
// 改成8精度,所以输出为12.345679
cout << fixed << setprecision(4);
cout << value << endl;
// fixed:固定点方式显示,精度指的是小数位,输出为12.3457
cout << value << endl;
// fixed和setprecision的作用还在,依然显示12.3457
cout.unsetf( ios::fixed ); cout << value << endl;
// 去掉fixed,精度恢复成整个数值的有效位数,显示为12.35
cout.precision( 6 ); cout << value << endl;
// 恢复成原来的样子,输出为12.3457
}
__int128 的输入输出重载
istream& operator >> (istream& stream, __int128 &v) {
string s; cin >> s; int l = (int)s.length(), i = 0, f = 1;
for (; i < l; i++) {
if (s[i] == '-') f = -1;
if (s[i] >= '0' && s[i] <= '9') break;
}
for (v = 0; i < l; i++) v = (v << 3) + (v << 1) + (s[i] ^ 48);
return stream;
}
ostream& operator << (ostream& stream, __int128 &v) {
__int128 x = v;
if (v < 0) {stream << '-'; x = -x;}
vector<int> t;
if (v == 0) stream << "0";
while (x) {t.push_back(x % 10); x /= 10;}
for (auto it = t.rbegin(); it != t.rend(); it++) stream << *it;
return stream;
}
大整数取模
读入时取模
int getmod(int p) {
int b = 0; char c;
while (!isdigit(c = getchar()));
for (; isdigit(c); c = getchar()) {
b = b * 10 + c - '0';
if (b >= p) b %= p;
}
return b;
}
读入后取模
ll Int(ll p, char *ch) {
ll res = 0;
int pos = 0;
while (!isdigit(ch[pos])) pos++;
while (isdigit(ch[pos])) {
res = (res * 10 + ch[pos] - '0') % p;
pos++;
}
return res;
}