快速读写
C++#include<iostream>
namespace fastIO{
static char buf[1<<21],*p1=buf,*p2=buf;
#define g (p1==p2&&(p1=buf,p2=(p1+fread(buf,1,1<<21,stdin)))==p1?EOF:*p1++)
#ifdef __linux__
#define p putchar_unlocked
#else
#define p _putchar_nolock
#endif
bool read(std::string&x){x[0]=g;while(x[0]<=32){if(!(~x[0]))return 0;x[0]=g;}int i=1;do x[i]=g;while(x[i]>32);if(!(~x[i]))return 0;return 1;}
bool read(char*x){x[0]=g;while(x[0]<=32){if(!(~x[0]))return 0;x[0]=g;}int i=1;do x[i]=g;while(x[i]>32);x[i]='\0';if(!(~x[i]))return 0;return 1;}
bool read(int&x){char c=g;if(!(~c))return 0;x=0;int f=1;while(!isdigit(c)){if(c==45)f=-1;c=g;if(!(~c))return 0;}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48),c=g;if(!(~c))return 0;}x*=f;return 1;}
bool read(unsigned int&x){char c=g;if(!(~c))return 0;x=0;while(!isdigit(c)){c=g;if(!(~c))return 0;}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48),c=g;if(!(~c))return 0;}return 1;}
bool read(long long&x){char c=g;if(!(~c))return 0;x=0;long long f=1;while(!isdigit(c)){if(c==45)f=-1;c=g;if(!(~c))return 0;}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48),c=g;if(!(~c))return 0;}x*=f;return 1;}
bool read(unsigned long long&x){char c=g;if(!(~c))return 0;x=0;while(!isdigit(c)){c=g;if(!(~c))return 0;}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48),c=g;if(!(~c))return 0;}return 1;}
void write(int x){if(!x){p(48);return;}if(x<0)p(45),x=-x;if(x>9)write(x/10);p(x%10|48);}
void write(const unsigned int&x){if(!x){p(48);return;}if(x>9)write(x/10);p(x%10|48);}
void write(long long x){if(!x){p(48);return;}if(x<0)p(45),x=-x;if(x>9)write(x/10);p(x%10|48);}
void write(const unsigned long long&x){if(!x){p(48);return;}if(x>9)write(x/10);p(x%10|48);}
void write(const std::string&x,unsigned int s=0){for(unsigned int i=s;i<x.size();p(x[i++]));}
void write(const char*x,unsigned int s=0){for(int i=s;x[i];p(x[i++]));}
template<typename T>void write(const T&x,const char&e){write(x);p(e);}
char Getchar(){return g;}
void Putchar(const char&x){p(x);}
#undef g
#undef p
}
using namespace fastIO;
离散化
C++#include<vector>
#include<algorithm>
namespace Hash{
template<typename it>
void hash(it bg,it ed) {
struct hash_t{
it id;
decltype(*bg) w;
hash_t(it Id=it(),decltype(*bg) W=0):id(Id),w(W){}
bool operator<(const hash_t&rhs) const {return w<rhs.w;}
};
it i;
std::vector<hash_t>tmp;
typename std::vector<hash_t>::iterator j;
decltype(*bg) k;
for(i=bg;i!=ed;++i) tmp.emplace_back(hash_t(i,*i));
std::sort(tmp.begin(),tmp.end());
for(i=bg,j=tmp.begin(),k=1;i!=ed;++i,++j) {
if(j!=tmp.begin()&&(j->w)!=((j-1)->w)) ++k;
*(j->id)=k;
}
}
}// namespace
using namespace Hash;
矩阵
C++#ifndef _MATRIX_HPP_
#define _MATRIX_HPP_ 1L
#include<cstring>
#include<iostream>
namespace Matrix{
template<
size_t R,
size_t C,
unsigned long long Mod,
typename matrix_t=long long
>
class matrix{
public:
size_t row,col;
matrix_t dgt[R+2][C+2];
void reset(size_t r=0,size_t c=0) {row=r,col=c;std::memset(dgt,0,sizeof dgt);}
matrix(size_t r=0,size_t c=0) {reset(r,c);}
void make_I(const size_t&n) {
reset(n,n);
for(size_t i=1;i<=n;++i) dgt[i][i]=1;
}
void read(std::istream&is,const size_t&r,const size_t&c) {
row=r,col=c;
for(size_t i=1;i<=r;++i)
for(size_t j=1;j<=c;++j) is>>dgt[i][j],dgt[i][j]%=Mod;
}
friend std::ostream&operator<<(std::ostream&os,const matrix&x) {
for(size_t i=1;i<=x.row;++i,os<<'\n')
for(size_t j=1;j<=x.col;++j) os<<x.dgt[i][j]<<' ';
return os;
}
matrix operator*(const matrix&x) const {
matrix res(row,x.col);
for(size_t k=1;k<=col;++k)
for(size_t i=1;i<=row;++i)
for(size_t j=1;j<=x.col;++j)
(res.dgt[i][j]+=dgt[i][k]*x.dgt[k][j]%Mod)%=Mod;
return res;
}
matrix&operator*=(const matrix&x) {return *this=*this*x;}
friend matrix operator*(const matrix_t&a,const matrix&b) {
matrix res(b.row,b.col);
for(size_t i=1;i<=b.row;++i)
for(size_t j=1;j<=b.col;++j)
(res.dgt[i][j]=b.dgt[i][j]*a%Mod)%=Mod;
return res;
}
friend matrix pow(matrix x,unsigned long long k) {
matrix res;
res.make_I(x.row);
do{
if(k&1ULL) res*=x;
x*=x;k>>=1ULL;
}while(k);
return res;
}
};
}// namespace
#endif// _MATRIX_HPP_
using namespace Matrix;
高精度
C++#ifndef _BIGINT_HPP_
#define _BIGINT_HPP_ 1L
#include<cstdio>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<stdexcept>
namespace Bigint{
template<
size_t Size,
size_t Base=100000000,//1e8
size_t Width=8,
typename Type=unsigned long long
>
class bigint{
protected:
Type dgt[Size+1];
size_t len;
bool sgn;
void clear() {memset(dgt,0,sizeof dgt);sgn=0,len=0;}
public:
Type operator[](const size_t&x) const {return dgt[x];}
bigint&operator=(const bigint&x) {
len=x.len,sgn=x.sgn;
for(size_t i=0;i<len;++i) dgt[i]=x[i];
return *this;
}
bigint&operator=(const std::string&x) {
len=0,sgn=(x[0]=='-'?1:0);
std::stringstream ss;
for(int i=x.size(),start,length;i>(sgn?1:0);i-=Width,++len) {
start=std::max((int)(sgn?1:0),(int)(i-Width)),length=i-start;
ss<<x.substr(start,length);
ss>>dgt[len];
}
return *this;
}
bigint&operator=(const char*x) {return *this=std::string(x);}
bigint&operator=(long long x) {
if(x==LLONG_MIN) return *this=std::string("-9223372036854775808");
len=0,sgn=0;
if(x==0) return *this;
if(x<0) sgn=1,x=-x;
while(x>0) dgt[len++]=x%Base,x/=Base;
return *this;
}
bigint() {clear();}
bigint(const bigint&x) {*this=x;}
bigint(const long long&x) {*this=x;}
bigint(const std::string&x) {*this=x;}
bigint operator-() const {
bigint res=*this;
res.sgn=!sgn;
return res;
}
bigint abs() const {
if(!sgn) return *this;
bigint res=*this;
res.sgn=0;
return res;
}
friend std::istream&operator>>(std::istream&is,bigint&x) {
std::string iss;
is>>iss;x=iss;
return is;
}
friend std::ostream&operator<<(std::ostream&os,const bigint&x) {
if(x.len<1) {os<<'0';return os;}
if(x.sgn) os<<'-';
os<<x[x.len-1];
for(int i=x.len-2;i>=0;--i)
os<<std::setw(Width)<<std::setfill('0')<<x[i];
return os;
}
std::string str() {std::stringstream ss;ss<<*this;return ss.str();}
int cmp(const bigint&x) const {
if(!sgn&&x.sgn) return 1;
if(sgn&&!x.sgn) return -1;
int res=sgn?-1:1;
if(len>x.len) return res;
if(len<x.len) return -res;
for(int i=len-1;i>=0;--i)
if(dgt[i]>x[i]) return res;
else if(dgt[i]<x[i]) return -res;
return 0;
}
bool operator>(const bigint&x) const {return cmp(x)>0;}
bool operator<(const bigint&x) const {return cmp(x)<0;}
bool operator>=(const bigint&x) const {return cmp(x)>=0;}
bool operator<=(const bigint&x) const {return cmp(x)<=0;}
bool operator==(const bigint&x) const {return cmp(x)==0;}
bool operator!=(const bigint&x) const {return cmp(x)!=0;}
bigint operator+(const bigint&x) const {
if(x.sgn) return *this-(-x);
if(sgn) return x-abs();
bigint res;
res.len=std::max(len,x.len)+1;
for(size_t i=0;i<res.len;++i) {
res.dgt[i]+=(dgt[i]+x[i]);
res.dgt[i+1]=res[i]/Base;
res.dgt[i]%=Base;
}
while(res.len>1&&res[res.len-1]==(Type)0) --res.len;
return res;
}
bigint operator-(const bigint&x) const {
if(x.sgn) return *this+(-x);
if(sgn) return -(abs()+x);
bigint res;
if(cmp(x)<0) {
res=x-(*this);
res.sgn=1;
return res;
}
for(size_t i=0;i<len;++i) res.dgt[i]=Base+dgt[i]-x[i];
for(size_t i=0;i<len;++i)
if(res[i]>=Base) res.dgt[i]-=Base;
else --res.dgt[i+1];
res.len=len;
while(res.len>1&&res[res.len-1]==(Type)0) --res.len;
return res;
}
bigint operator*(const bigint&x) const {
if(*this==bigint(0)||x==bigint(0)) return bigint(0);
bigint res;
res.len=len+x.len;
res.sgn=sgn^x.sgn;
for(size_t i=0;i<len;++i)
for(size_t j=0;j<x.len;++j) {
res.dgt[i+j]+=dgt[i]*x[j];
res.dgt[i+j+1]+=res[i+j]/Base;
res.dgt[i+j]%=Base;
}
while(res.len>1&&res[res.len-1]==(Type)0) --res.len;
return res;
}
void div(const bigint&x,bigint&res_div,bigint&res_mod) const {
if(*this==bigint(0)) return (res_div=res_mod=0LL,void());
if(x==bigint(0)) throw std::domain_error("Bigint::bigint::div: Division by zero.");
std::vector<bigint>pw2,est;
bigint _this=abs();
res_div=0LL; pw2.clear(); pw2.emplace_back(bigint(1));
res_mod=0LL; est.clear(); est.emplace_back(x.abs());
while(est.back()<_this) {
est.emplace_back(est.back()+est.back());
pw2.emplace_back(pw2.back()+pw2.back());
}
for(int i=est.size()-1;i>=0;--i)
if(est[i]<=_this) res_div+=pw2[i],_this-=est[i];
res_div.sgn=sgn^x.sgn;
res_mod=*this-x*res_div;
}
bigint operator/(const bigint&x) const {
bigint res,tmp;
div(x,res,tmp);
return res;
}
bigint operator%(const bigint&x) const {
bigint res,tmp;
div(x,tmp,res);
return res;
}
bigint&operator+=(const bigint&x) {return *this=*this+x;}
bigint&operator-=(const bigint&x) {return *this=*this-x;}
bigint&operator*=(const bigint&x) {return *this=*this*x;}
bigint&operator/=(const bigint&x) {return *this=*this/x;}
bigint&operator%=(const bigint&x) {return *this=*this%x;}
bigint&operator++() {return *this+=bigint(1);}
bigint&operator--() {return *this-=bigint(1);}
bigint operator++(int) {bigint res=*this;*this+=bigint(1);return res;}
bigint operator--(int) {bigint res=*this;*this-=bigint(1);return res;}
bool sign() const {return sgn;}
size_t size() const {return len;}
};// class
template<size_t Size,size_t Base,size_t Width,typename Type>
bigint<Size,Base,Width,Type>pow(bigint<Size,Base,Width,Type>x,unsigned long long k) {
bigint<Size,Base,Width,Type>res=1;
do{
if(k&1ULL) res*=x;
x*=x;k>>=1ULL;
}while(k);
return res;
}
}// namespace
#endif// _BIGINT_HPP_
using namespace Bigint;
: )