高精度
struct bigint{
char str[2005];
static const int bigint_M=1005,bigintmod=10000;//注意数组大小,数组大小将严重影响效率
int num[bigint_M],len;//num从0开始
bigint(){memset(num,0,sizeof(num));len=1;}
//改变压位时^-^的地方可能要修改
void read() {
scanf("%s",str);
len=0;
int sz=strlen(str);
for(int i=sz-1;i>=0;i-=4) {//^-^
num[len]=0;
for(int j=max(0,i-3);j<=i;j++)//^-^
num[len]=(num[len]<<3)+(num[len]<<1)+(str[j]^48);
len++;
}
while(len>1&&!num[len-1]) --len;
}
void print() {
printf("%d",num[len-1]);//^-^
for(int i=len-2;i>=0;i--) printf("%04d",num[i]);//^-^
putchar(' ');
}
void println() {
printf("%d",num[len-1]);//^-^
for(int i=len-2;i>=0;i--) printf("%04d",num[i]);//^-^
putchar('\n');
}
bool operator < (const bigint &cmp)const{
if(len!=cmp.len)return len<cmp.len;
for(int i=len-1;i>=0;i--)
if(num[i]!=cmp.num[i]) return num[i]<cmp.num[i];
return false;
}
bool operator > (const bigint &cmp)const{return cmp<*this;}
bool operator <= (const bigint &cmp)const{return !(cmp<*this);}
bool operator != (const bigint &cmp)const{return cmp<*this||*this<cmp;}
bool operator == (const bigint &cmp)const{return !(cmp<*this||*this<cmp);}
bigint operator + (const int &p)const {
bigint B;B=*this;
B.num[0]+=p;
int tmp=0;
while(B.num[tmp]>=bigintmod) {
B.num[tmp+1]++;
B.num[tmp]-=bigintmod;
tmp++;
}
while(B.num[B.len]) B.len++;
return B;
}
bigint operator + (const bigint &A)const {
bigint B;
B.len=max(A.len,len);
for(int i=0;i<B.len;i++){
B.num[i]+=num[i]+A.num[i];
if(B.num[i]>=bigintmod){
B.num[i]-=bigintmod;
B.num[i+1]++;
}
}
while(B.num[B.len]) B.len++;
return B;
}
bigint operator - (const int &p)const {
bigint B;B=*this;
B.num[0]-=p;
int tmp=0;
while(B.num[tmp]<0){
B.num[tmp]+=bigintmod;
B.num[tmp+1]--;
tmp++;
}
while(B.len>1&&!B.num[B.len-1]) B.len--;
return B;
}
bigint operator - (const bigint &A)const {
bigint B;
B.len=max(A.len,len);
for(int i=0;i<B.len;i++){
B.num[i]+=num[i]-A.num[i];
if(B.num[i]<0){
B.num[i]+=bigintmod;
B.num[i+1]--;
}
}
while(B.len>1&&!B.num[B.len-1]) B.len--;
return B;
}
bigint operator * (const int &p)const {
bigint B;
B.len=len;
for(int i=0;i<len;i++){
B.num[i]+=num[i]*p;
if(B.num[i]>=bigintmod){
B.num[i+1]+=B.num[i]/bigintmod;
B.num[i]%=bigintmod;
}
}
while(B.num[B.len]) B.len++;
return B;
}
bigint operator * (const bigint &A)const {
bigint B;
B.len=A.len+len-1;
for(int i=0;i<len;i++)for(int j=0;j<A.len;j++){
B.num[i+j]+=num[i]*A.num[j];
if(B.num[i+j]>=bigintmod){
B.num[i+j+1]+=B.num[i+j]/bigintmod;
B.num[i+j]%=bigintmod;
}
}
while(B.num[B.len]) B.len++;
return B;
}
bigint operator / (const int &p) {
bigint B=*this;
for(int i=B.len-1;i>=0;i--){
if(i) B.num[i-1]+=B.num[i]%p*bigintmod;
B.num[i]/=p;
}
while(B.len>1&&!B.num[B.len-1]) B.len--;
return B;
}
bigint operator / (const bigint &A)const {
bigint L,R,res;
if(*this<A)return res;
R=*this;
while(L<=R){
bigint mid=(L+R)/2;
if((mid*A)<=*this){
res=mid;
L=mid+1;
}else R=mid-1;
}
return res;
}
};
并查集
void Init(int n){for(int i=1;i<=n;i++) Fa[i]=i;}//初始化
int Find(int x){return Fa[x]==x?x:Fa[x]=Find(Fa[x]);}
//寻根,同 时路径压缩
void unite(int x,int y){Fa[Find(x)]=Find(y);}//合并集合
bool same(int x,int y){return Find(x)==Find(y);}
//判 断两个点是否同一个集合
读入输出挂
namespace IO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
//fread->read
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
//{printf("IO error!\n");system("pause");for (;;);exit(0);}
}
return *p1++;
}
inline bool blank(char ch){return ch!='-'&&!(ch>='0'&&ch<='9');}
inline void read(int &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
inline void read(ll &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
inline void read(double &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (ch=='.'){
double tmp=1; ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
}
if (sign)x=-x;
}
inline void read(char *s){
char ch=nc();
for (;blank(ch);ch=nc());
if (IOerror)return;
for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=0;
}
inline void read(char &c){
for (c=nc();blank(c);c=nc());
if (IOerror){c=-1;return;}
}
//fwrite->write
struct Ostream_fwrite{
char *buf,*p1,*pend;
Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
void out(char ch){
if (p1==pend){
fwrite(buf,1,BUF_SIZE,stdout);p1=buf;
}
*p1++=ch;
}
void print(int x){
static char s[15],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1);
}
void println(int x){
static char s[15],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1); out('\n');
}
void print(ll x){
static char s[25],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1);
}
void println(ll x){
static char s[25],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1); out('\n');
}
void print(double x,int y){
static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,
1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL};
if (x<-1e-12)out('-'),x=-x;x*=mul[y];
ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1;
ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2);
if (y>0){out('.'); for (size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i); print(x3);}
}
void println(double x,int y){print(x,y);out('\n');}
void print(char *s){while (*s)out(*s++);}
void println(char *s){while (*s)out(*s++);out('\n');}
void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
~Ostream_fwrite(){flush();}
}Ostream;
inline void print(int x){Ostream.print(x);}
inline void println(int x){Ostream.println(x);}
inline void print(char x){Ostream.out(x);}
inline void println(char x){Ostream.out(x);Ostream.out('\n');}
inline void print(ll x){Ostream.print(x);}
inline void println(ll x){Ostream.println(x);}
inline void print(double x,int y){Ostream.print(x,y);}
inline void println(double x,int y){Ostream.println(x,y);}
inline void print(char *s){Ostream.print(s);}
inline void println(char *s){Ostream.println(s);}
inline void println(){Ostream.out('\n');}
inline void flush(){Ostream.flush();}
#undef ll
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace IO;
常用模板:
#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;i++)
#define per(i,t,s) for(int i=(t),_s=(s);i>=_s;i--)
#define bug(x) cerr<<#x<<'='<<x<<' '
#define debug(x) cerr<<#x<<'='<<x<<endl
#define all(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const db dinf=1e18;
const ll linf=1e18;
ll powmod(ll x,ll k){ll res=1;x%=mod;for(;k;k>>=1,x=x*x%mod){if(k&1)res=res*x%mod;}return res;}
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
int lowbit(int x){return x&(-x);}
template<class _>void add(_ &a,_ b){(a+=b)%=mod;}
template<class _>void sub(_ &a,_ b){(a-=b)%=mod;}
template<class _>void Max(_ &a,_ b){if(a<b)a=b;}
template<class _>void Min(_ &a,_ b){if(a>b)a=b;}
inline void read(int &x) {
static char c;x=0;
while(c=getchar(),c<48);
do x=(x<<3)+(x<<1)+(c^48);
while(c=getchar(),c>47);
}
//head
sublime ,dev 手动开栈 (加在freopen下面):
int __size__=128<<20;
char*__p__=(char*)malloc(__size__)+ __size__;
__asm__("movl %0, %%esp\n"::"r"(__p__));