此贴用来存一些卡常用代码。
侵删。
普通快读:
为什么不用自己写的快读快写呢?好看且好写且好用。
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!(ch >= '0' && ch <= '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
取模:
用法:Mint x;
,其余按正常语法写即可。
template <class type,const type mod> struct Modint{
type val;
constexpr Modint():val(){}
const type& operator()() const { return val; }
template <typename Type> Modint(const Type &num){ type t=(-mod)<=num&&num<(+mod)?num:num%mod; val=t<0?t+mod:t; }
friend istream& operator >> (istream& ist,Modint& a){ return ist>>a.val; }
friend ostream& operator << (ostream& ost,Modint a){ return ost<<a.val; }
friend bool operator == (const Modint &lhs,const Modint &rhs){ return (lhs.val==rhs.val); }
friend bool operator != (const Modint &lhs,const Modint &rhs){ return (lhs.val!=rhs.val); }
Modint& operator += (const Modint &rhs){ if((val+=rhs.val)>=mod) val-=mod; return (*this); }
Modint& operator -= (const Modint &rhs){ if((val-=rhs.val)<=(-1)) val+=mod; return (*this); }
Modint& operator *= (const Modint &rhs){ if((val*=rhs.val)>=mod) val%=mod; return (*this); }
Modint& operator /= (const Modint &rhs){
type a=0,b=1,x=rhs.val,y=mod,t; for(;x;t=y/x,swap(x,y-=t*x),swap(a-=t*b,b)); (*this)*=a; return (*this);
}
template <typename Type> Modint& operator ^= (const Type &rhs){
Type a=rhs; Modint t=1,b=(*this); for(;a;a/=2,b*=b) if(a&1) t*=b; return ((*this)=t);
}
friend Modint operator + (const Modint &lhs,const Modint &rhs){ return Modint(lhs)+=rhs; }
friend Modint operator - (const Modint &lhs,const Modint &rhs){ return Modint(lhs)-=rhs; }
friend Modint operator * (const Modint &lhs,const Modint &rhs){ return Modint(lhs)*=rhs; }
friend Modint operator / (const Modint &lhs,const Modint &rhs){ return Modint(lhs)/=rhs; }
template <typename Type> friend Modint operator ^ (const Modint &bas,const Type &exp){ return Modint(bas)^=exp; }
Modint operator + () const { return Modint(+val); } Modint operator - () const { return Modint(-val); }
Modint& operator ++ (){ return (*this)+=1; } Modint operator ++ (int){ Modint res(*this); (*this)+=1; return res; }
Modint& operator -- (){ return (*this)-=1; } Modint operator -- (int){ Modint res(*this); (*this)-=1; return res; }
};
typedef Modint<long long,1000000007> Mint;
“超级快读快写”:
不建议用,毕竟我没搞清楚这是个什么玩意儿。
用法:
for (int i = 1; i <= m; i++)
write(ans[i]), *(p3++) = '\n';
fwrite(ouf, 1, p3 - ouf, stdout);
char buf[1 << 20], *p1 = buf, *p2 = buf, ouf[1 << 23], *p3 = ouf;
char input() {
if (p1 == p2) p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin);
return *(p1++);
}
void read(int &x) {
static char ch = input();
x = 0;
while (!isdigit(ch)) ch = input();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = input();
}
void write(int x) {
if (x > 9) write(x / 10);
*(p3++) = x % 10 ^ 48;
}
快读快写 2
我不知道为什么代码长这个样子啊。
用法:fio>>n; fio<<n<<endl;
class fastIO{private:char ibuf[50007],*p1=ibuf,*p2=ibuf,obuf[50007],*p3=obuf,sta[50];bool file_end=false;char get(){return p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,50007,stdin),p1==p2)?(file_end=true),char(EOF):*p1++;}void put(const char x){p3-obuf<50007?*p3++=x:(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x);}public:explicit operator bool(){return!file_end;}size_t flush(){size_t f=fwrite(obuf,p3-obuf,1,stdout);p3=obuf;*p3=0;return f;}fastIO&operator>>(char&t){for(t=get();!isgraph(t);t=get());return*this;}template<typename any>typename std::enable_if<std::is_same<any,char>::value,any>::type tpval(){char t;for(t=get();!isgraph(t);t=get());return t;}fastIO&operator>>(char*t){char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())*t=c,t++;*t=0;return*this;}fastIO&operator>>(std::string&t){t.clear();char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())t+=c;return*this;}template<typename any>typename std::enable_if<std::is_same<any,std::string>::value,any>::type tpval(){std::string t;char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())t+=c;return t;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,fastIO>::type&operator>>(any&t){t=0;bool y=0;char c=get();for(;!isdigit(c);c=get())if(c==45)y=true;for(;isdigit(c);c=get())t=t*10+c-48;if(y==1)t=-t;return*this;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,any>::type tpval(){any t=0;bool y=0;char c=get();for(;!isdigit(c);c=get())if(c==45)y=true;for(;isdigit(c);c=get())t=t*10+c-48;if(y==1)t=-t;return t;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,fastIO>::type&operator>>(any&t){t=0;char c=get();for(;!isdigit(c);c=get());for(;isdigit(c);c=get())t=t*10+c-48;return*this;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,any>::type tpval(){any t=0;char c=get();for(;!isdigit(c);c=get());for(;isdigit(c);c=get())t=t*10+c-48;return t;}template<typename any1,typename any2>fastIO&operator>>(std::pair<any1,any2>&t){return*this>>t.first>>t.second;}template<typename any1,typename any2>std::pair<any1,any2>tpval(){return std::pair<any1,any2>(tpval<any1>(),tpval<any2>());}template<typename any>fastIO&read(any&t){return*this>>t;}fastIO&read(char*t){char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())*t=c,t++;*t=0;return*this;}template<typename any,typename...args>fastIO&read(any&t1,args&...t2){return(*this>>t1).read(t2...);}fastIO&operator<<(const char t){put(t);return*this;}fastIO&operator<<(const char*t){for(;*t;t++)put(*t);return*this;}fastIO&operator<<(const std::string&t){for(const char it:t)put(it);return*this;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,fastIO>::type&operator<<(any t){if(!t){put(48);return*this;}int len=0;if(t<0)t=-t,put(45);while(t)sta[len++]=char(t%10+48),t/=10;while(len--)put(sta[len]);return*this;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,fastIO>::type&operator<<(any t){if(!t){put(48);return*this;}int len=0;while(t)sta[len++]=char(t%10+48),t/=10;while(len--)put(sta[len]);return*this;}template<typename any1,typename any2>fastIO&operator<<(const std::pair<any1,any2>&t){return*this<<t.first<<' '<<t.second;}template<typename any>fastIO&write(const any&t){return*this<<t;}template<typename any,typename...args>fastIO&write(const any&t1,const args&...t2){return(*this<<t1).write(t2...);}~fastIO(){fwrite(obuf,p3-obuf,1,stdout);}}fio;