模板大全

快读快写

namespace Fast_I{char*_Buf,*_Start_ptr,*_End_ptr;std::streambuf*inbuf;unsigned int Size;bool _Ok;struct Fast_Istream{operator bool(){return _Ok;}Fast_Istream(std::streambuf*,unsigned int);Fast_Istream(unsigned int);Fast_Istream(const char*,unsigned int);Fast_Istream&operator>>(char&);Fast_Istream&operator>>(char*);Fast_Istream&operator>>(bool&);Fast_Istream&operator>>(short&);Fast_Istream&operator>>(int&);Fast_Istream&operator>>(long&);Fast_Istream&operator>>(long long&);Fast_Istream&operator>>(unsigned short&);Fast_Istream&operator>>(unsigned int&);Fast_Istream&operator>>(unsigned long&);Fast_Istream&operator>>(unsigned long long&);Fast_Istream&operator>>(float&);Fast_Istream&operator>>(double&);Fast_Istream&operator>>(long double&);Fast_Istream&operator>>(std::string&);template<typename Typex>void operator()(Typex&_Val){*this>>_Val;}template<typename Typex,typename...More>void operator()(Typex&,More&...);std::streambuf*rdbuf(){return inbuf;}void rdbuf(std::streambuf*_inbuf){inbuf=_inbuf;}void rdbuf(const char*);void pop();char peek();};}namespace Fast_O{std::string buf;std::streambuf*outbuf;struct Fast_Ostream{Fast_Ostream(std::streambuf*,unsigned int);Fast_Ostream(std::streambuf*out){outbuf=out;}Fast_Ostream(const char*,unsigned int);Fast_Ostream(unsigned int);void flush();~Fast_Ostream();void endl(){buf.push_back('\n');}template<typename Typex>void endl(const Typex&_Val);template<typename Typex,typename...More>void endl(const Typex&,const More&...);template<typename Typex>void operator()(const Typex&_Val);template<typename Typex,typename...More>void operator()(const Typex&,const More&...);Fast_Ostream&operator<<(char);Fast_Ostream&operator<<(const char*);Fast_Ostream&operator<<(const std::string&);Fast_Ostream&operator<<(bool);Fast_Ostream&operator<<(short);Fast_Ostream&operator<<(int);Fast_Ostream&operator<<(long);Fast_Ostream&operator<<(long long);Fast_Ostream&operator<<(unsigned short);Fast_Ostream&operator<<(unsigned int);Fast_Ostream&operator<<(unsigned long);Fast_Ostream&operator<<(unsigned long long);std::streambuf*rdbuf(){return outbuf;}void rdbuf(std::streambuf*_outbuf){outbuf=_outbuf;}void rdbuf(const char*);};}namespace Fast_IO{Fast_I::Fast_Istream fin(std::cin.rdbuf(),1048576);Fast_O::Fast_Ostream fout(std::cout.rdbuf());}
#define cin Fast_IO::fin
#define cout Fast_IO::fout
signed main(){int a,b;cin(a,b);cout(a+b,'\n');return 0;}namespace Fast_I{Fast_Istream::Fast_Istream(std::streambuf*in,unsigned int Sz){_Ok=1;Fast_I::Size=Sz;inbuf=in;_Start_ptr=_End_ptr=_Buf=new char[Sz];}Fast_Istream::Fast_Istream(const char*in,unsigned int Sz){_Ok=1;Fast_I::Size=Sz;rdbuf(in);_Start_ptr=_End_ptr=_Buf=new char[Sz];}Fast_Istream::Fast_Istream(unsigned int Sz){_Ok=1;Fast_I::Size=Sz;_Start_ptr=_End_ptr=_Buf=new char[Sz];}void Fast_Istream::rdbuf(const char*File){static std::ifstream __In__(File);rdbuf(__In__.rdbuf());}void Get_Char(char&_Val){if(_Start_ptr==_End_ptr){_Start_ptr=_Buf;_End_ptr=_Buf+inbuf->sgetn(_Buf,Size);}if(_Start_ptr==_End_ptr){_Val=-1;_Ok=0;}else{_Val=*_Start_ptr++;}}Fast_Istream&Fast_Istream::operator>>(char&_Val){if(_Ok){Get_Char(_Val);while(_Val==32||_Val==10||_Val==13||_Val==8||_Val==9||_Val==7||_Val==12||_Val==11){Get_Char(_Val);}}return*this;}Fast_Istream&Fast_Istream::operator>>(char*_Val){if(_Ok){Get_Char(*_Val);while(*_Val==32||*_Val==10||*_Val==13||*_Val==8||*_Val==9||*_Val==7||*_Val==12||*_Val==11){Get_Char(*_Val);}while(*_Val!=32&&*_Val!=10&&*_Val&&*_Val!=-1&&*_Val!=9&&*_Val!=11&&*_Val!=12){Get_Char(*++_Val);}*_Val=0;--_Start_ptr;}return*this;}Fast_Istream&Fast_Istream::operator>>(std::string&_Val){if(_Ok){char c;Get_Char(c);while(c==32||c==10||c==13||c==8||c==9||c==7||c==12||c==11){Get_Char(c);}for(_Val.clear();c!=32&&c!=10&&c&&c!=-1&&c!=9&&c!=11&&c!=12;Get_Char(c)){_Val.push_back(c);}--_Start_ptr;}return*this;}template<typename Typex>void Get_Int(Typex&_Val){if(_Ok){char ch;bool _F=0;for(Get_Char(ch);(ch<48||ch>57)&&ch!=-1;Get_Char(ch)){_F=ch==45;}for(_Val=0;ch>47&&ch<58&&ch!=-1;Get_Char(ch)){_Val=_Val*10+(ch^48);}if(_F){_Val=~_Val+1;}--_Start_ptr;}}template<typename Typex>void Get_Unsigned(Typex&_Val){if(_Ok){char ch;Get_Char(ch);while((ch<48||ch>57)&&ch!=-1){Get_Char(ch);}for(_Val=0;ch>47&&ch<58&&ch!=-1;Get_Char(ch)){_Val=_Val*10+(ch^48);}--_Start_ptr;}}template<typename Typex>void Get_Double(Typex&_Val){if(_Ok){char ch;bool _F=0;for(Get_Char(ch);(ch<48||ch>57)&&ch!=-1;Get_Char(ch)){_F=ch==45;}for(_Val=0;ch>47&&ch<58&&ch!=-1;Get_Char(ch)){_Val=_Val*10+(ch^48);}if(ch==46){unsigned long long _Pow=1;for(Get_Char(ch);ch>47&&ch<58&&ch!=-1;Get_Char(ch)){_Val+=Typex((ch^48)*1.0/(_Pow*=10));}}if(_F){_Val=-_Val;}--_Start_ptr;}}Fast_Istream&Fast_Istream::operator>>(bool&_Val){if(_Ok){char ch;Get_Char(ch);while(ch==32||ch==10||ch==13||ch==8||ch==9||ch==7||ch==12||ch==11){Get_Char(ch);}while(ch!=32&&ch!=10&&ch&&ch!=-1&&ch!=9&&ch!=11&&ch!=12){_Val|=ch!=48;Get_Char(ch);}--_Start_ptr;}return*this;}Fast_Istream&Fast_Istream::operator>>(short&_Val){Get_Int(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(int&_Val){Get_Int(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(long&_Val){Get_Int(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(long long&_Val){Get_Int(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(unsigned short&_Val){Get_Unsigned(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(unsigned int&_Val){Get_Unsigned(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(unsigned long&_Val){Get_Unsigned(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(unsigned long long&_Val){Get_Unsigned(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(float&_Val){Get_Double(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(double&_Val){Get_Double(_Val);return*this;}Fast_Istream&Fast_Istream::operator>>(long double&_Val){Get_Double(_Val);return*this;}template<typename Typex,typename...More>void Fast_Istream::operator()(Typex&_Val,More&..._More){*this>>_Val;operator()(_More...);}void Fast_Istream::pop(){char ch;Get_Char(ch);}char Fast_Istream::peek(){if(_Start_ptr==_End_ptr){_Start_ptr=_Buf;_End_ptr=_Buf+inbuf->sgetn(_Buf,Size);}if(_Start_ptr==_End_ptr){_Ok=0;return-1;}else{return*_Start_ptr;}}}namespace Fast_O{Fast_Ostream::Fast_Ostream(std::streambuf*out,unsigned int Size){buf.reserve(Size);outbuf=out;}Fast_Ostream::Fast_Ostream(const char*File,unsigned int Size){buf.reserve(Size);rdbuf(File);}void Fast_Ostream::rdbuf(const char*File){static std::ofstream __Out__(File);rdbuf(__Out__.rdbuf());}Fast_Ostream::Fast_Ostream(unsigned int Size){buf.reserve(Size);}void Fast_Ostream::flush(){outbuf->sputn(buf.data(),buf.size());buf.clear();}Fast_Ostream::~Fast_Ostream(){flush();}Fast_Ostream&Fast_Ostream::operator<<(char _Val){buf.push_back(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(const char*_Val){while(*_Val){buf.push_back(*_Val++);}return*this;}Fast_Ostream&Fast_Ostream::operator<<(const std::string&_Val){for(auto&&i:_Val){buf.push_back(i);}return*this;}template<typename Typex>void Put_Unsigned(Typex _Val){char*_Stack=(char*)malloc(sizeof(Typex)*3);unsigned S_top=0;while(_Val){_Stack[++S_top]=(_Val%10)^48;_Val/=10;}if(!S_top){buf.push_back('0');}while(S_top){buf.push_back(_Stack[S_top--]);}free(_Stack);}void Put_Int(long long _Val){if(_Val<0){buf.push_back('-');Put_Unsigned(~_Val+1);}else{Put_Unsigned(_Val);}}Fast_Ostream&Fast_Ostream::operator<<(bool _Val){buf.push_back(_Val?'1':'0');return*this;}Fast_Ostream&Fast_Ostream::operator<<(short _Val){Put_Int(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(int _Val){Put_Int(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(long _Val){Put_Int(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(long long _Val){Put_Int(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(unsigned short _Val){Put_Unsigned(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(unsigned int _Val){Put_Unsigned(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(unsigned long _Val){Put_Unsigned(_Val);return*this;}Fast_Ostream&Fast_Ostream::operator<<(unsigned long long _Val){Put_Unsigned(_Val);return*this;}template<typename Typex>void Fast_Ostream::endl(const Typex&_Val){*this<<_Val<<'\n';}template<typename Typex,typename...More>void Fast_Ostream::endl(const Typex&_Val,const More&..._More){*this<<_Val;endl(_More...);}template<typename Typex>void Fast_Ostream::operator()(const Typex&_Val){*this<<_Val;}template<typename Typex,typename...More>void Fast_Ostream::operator()(const Typex&_Val,const More&..._More){*this<<_Val;operator()(_More...);}}

高精度

struct bign{int d[kMaxN],len;void clean(){while(len>1&&!d[len-1])len--;}bign(){fill(d,d+kMaxN,0);len=1;}bign(int num){*this=num;}bign(string num){*this=num;}bign operator=(const string num){fill(d,d+kMaxN,0);len=num.size();for(int i=0;i<len;i++)d[i]=num[len-1-i]-'0';clean();return*this;}bign operator=(int num){string s;for(;num;num/=10)s=char(num%10+'0')+s;*this=s;return*this;}bign operator+(const bign&b){bign c=*this;int cnt;for(cnt=0;cnt<b.len;cnt++){c.d[cnt]+=b.d[cnt];c.d[cnt+1]+=c.d[cnt]/10,c.d[cnt]%=10;}while(c.d[cnt]>9)c.d[cnt++]%=10,c.d[cnt]++;c.len=max(len,b.len);if(c.d[cnt]&&c.len<=cnt)c.len=cnt+1;return c;}bign operator-(const bign&b){bign c=*this;int cnt;for(cnt=0;cnt<b.len;cnt++){c.d[cnt]-=b.d[cnt];if(c.d[cnt]<0)c.d[cnt]+=10,c.d[cnt+1]--;}while(c.d[cnt]<0)c.d[cnt++]+=10,c.d[cnt]--;c.clean();return c;}bign operator*(const bign&b)const{bign c;c.len=len+b.len+2;for(int i=0;i<len;i++)for(int j=0;j<b.len;j++)c.d[i+j]+=d[i]*b.d[j];for(int i=0;i<c.len-1;i++)c.d[i+1]+=c.d[i]/10,c.d[i]%=10;c.clean();return c;}bign operator/(const bign&b){bign c=*this,a=0;for(int i=len-1,j;i>=0;i--){a=a*10+d[i];for(j=0;j<10;j++)if(a<b*(j+1))break;c.d[i]=j;a=a-b*j;}c.clean();return c;}bign operator%(const bign&b){bign a=0;for(int i=len-1,j;i>=0;i--){a=a*10+d[i];for(j=0;j<10;j++)if(a<b*(j+1))break;a=a-b*j;}return a;}bign operator+=(const bign&b){*this=*this+b;return*this;}bign operator-=(const bign&b){*this=*this-b;return*this;}bign operator*=(const bign&b){*this=*this*b;return*this;}bign operator/=(const bign&b){*this=*this/b;return*this;}bign operator%=(const bign&b){*this=*this%b;return*this;}bool operator<(const bign&b)const{if(len!=b.len)return len<b.len;for(int i=len-1;i>=0;i--)if(d[i]!=b.d[i])return d[i]<b.d[i];return false;}bool operator>(const bign&b)const{return b<*this;}bool operator<=(const bign&b)const{return!(b<*this);}bool operator>=(const bign&b)const{return!(*this<b);}bool operator!=(const bign&b)const{return b<*this||*this<b;}bool operator==(const bign&b)const{return!(b<*this)&&!(b>*this);}string str()const{string s(len,'0');for(int i=0;i<len;i++)s[len-1-i]=d[i]+'0';return s;}friend istream&operator>>(istream&in,bign&x){string s;in>>s;x=s.c_str();return in;}friend ostream&operator<<(ostream&out,const bign&x){out<<x.str();return out;}}

线段树

struct E {
  ll t[kMaxN << 2], tag[kMaxN << 2];
}tr;

void push_up(ll x) {
  tr.t[x] = tr.t[ls] + tr.t[rs];
}

void add_tag(ll x, ll l, ll r,ll sum) {
  tr.t[x] += sum * (r - l + 1), tr.tag[x] += sum;
}

void push_down(ll x, ll l, ll r) {
  if(tr.tag[x]) {
    ll mid = l + r >> 1;
    add_tag(ls, l, mid, tr.tag[x]), add_tag(rs, mid + 1, r, tr.tag[x]), tr.tag[x] = 0;
  }
}

void update(ll x, ll l , ll r, ll L, ll R, ll sum) {
  if(L <= l && r <= R) {
    return add_tag(x, l, r, sum);
  }
  push_down(x, l, r);
  ll mid = l + r >> 1;
  if(L <= mid) update(ls, l, mid, L, R, sum);
  if(mid < R) update(rs, mid + 1, r, L, R, sum);
  push_up(x);
}

ll query (ll x, ll l ,ll r, ll L, ll R) {
  if(L <= l && r <= R) {
    return tr.t[x];
  }
  push_down(x, l, r);
  ll mid = l + r >> 1, f1 = L <= mid, f2 = mid < R;
  if(f1 && f2) return query(ls, l, mid, L, R) + query(rs, mid + 1, r, L, R);
  if(f1) return query(ls, l, mid, L, R);
  return query(rs, mid + 1, r, L, R);
}

平衡树

struct E {
  struct node {
    int v, ls, rs, sz, rk;
  } t[kMaxN];
  int tot, rt;

  void update(int x) {
    t[x].sz = t[t[x].ls].sz + t[t[x].rs].sz + 1;
  }
  int add(int val) {
    t[++tot] = (node){val, 0, 0, 1, rand()};
    return tot;
  }
  void split(int x, int& a, int& b, int val) {
    if (x == 0) {
      return a = b = 0, void();
    }
    if (t[x].v <= val) {
      a = x, split(t[x].rs, t[x].rs, b, val);
    } else {
      b = x, split(t[x].ls, a, t[x].ls, val);
    }
    update(x);
  }
  void merge(int& x, int a, int b) {
    if (a == 0 || b == 0) {
      return x = a + b, void();
    }
    if (t[a].rk > t[b].rk) {
      x = a, merge(t[a].rs, t[a].rs, b);
    } else {
      x = b, merge(t[b].ls, a, t[b].ls);
    }
    update(x);
  }
  void insert(int& x, int val) {
    int v = add(val), a = 0, b = 0;
    split(x, a, b, val);
    merge(a, a, v);
    merge(x, a, b);
  }
  void del(int& x, int val) {
    int a = 0, b = 0, c = 0;
    split(x, a, b, val);
    split(a, a, c, val - 1);
    merge(c, t[c].ls, t[c].rs);
    merge(a, a, c);
    merge(x, a, b);
  }
  int find(int x, int val) {
    for (; t[t[x].ls].sz + 1 != val;) {
      if (t[t[x].ls].sz >= val)
        x = t[x].ls;
      else
        val -= t[t[x].ls].sz + 1, x = t[x].rs;
    }
    return t[x].v;
  }
  int rnk(int& x, int val) {
    int a = 0, b = 0;
    split(x, a, b, val - 1);
    int v = t[a].sz + 1;
    merge(x, a, b);
    return v;
  }
  int prev(int& x, int val) {
    int a = 0, b = 0;
    split(x, a, b, val);
    int v = find(a, t[a].sz);
    merge(x, a, b);
    return v;
  }
  int suc(int& x, int val) {
    int a = 0, b = 0;
    split(x, a, b, val - 1);
    int v = find(b, 1);
    merge(x, a, b);
    return v;
  }
} t;

最短路

bool spfa(ll op) {
  fill(dis, dis + kMaxN, 1e18), h = 0;
  dis[op] = 0, vis[op] = 1, q[++h] = op;
  ll x, v, w;
  for (; h;) {
    x = q[h], vis[x] = 0, h--;
    for (auto u : g[x]) {
      v = u.first, w = u.second;
      if ((dis[x] + w) < dis[v]) {
        dis[v] = dis[x] + w;
        cnt[v] = cnt[x];
        if (++cnt[v] >= n) {
          return 0;
        }
        if (!vis[v]) {
          q[++h] = v, vis[v] = 1;
        }
      }
    }
  }
  return 1;
}

线性基

struct E {
  ll cnt, p[kMaxM];
  E() {
    cnt = 0, fill(p, p + kMaxM, 0);
  }

  bool update(ll x, bool y) {
    for (ll i = 63; i >= 0; i--) {
      if ((x >> i) & 1) {
        if (p[i])
          x = x ^ p[i];
        else
          return p[i] = x * y, 1;
      }
    }
    return 0;
  }

  ll find_k(ll x, ll k = 0) {
    for (int i = 0; i < cnt; i++) {
      if (x >= p[i] && (x ^ p[i]) < x) {
        x ^= p[i], k |= (1 << i);
      }
    }
    return k + 1;
  }

  ll find_x(ll k, ll x = 0) {
    k--;
    for (int i = 0; i < cnt; i++) {
      if (k & 1) {
        x ^= p[i];
      }
      k >>= 1;
    }
    return x;
  }

  ll find_max(ll x) {
    ll cnt = x;
    for(int i = 63; i >= 0; i--) {
      if((cnt ^ p[i]) > cnt) {
        cnt ^= p[i];
      }
    }
    return cnt;
  }

  void bin() {
    for (ll i = 0; i <= 63; i++) {
      for (ll j = i + 1; j <= 63; j++) {
        if ((p[j] >> i) & 1) {
          p[j] ^= p[i];
        }
      }
    }
    for (ll i = 0; i <= 63; i++) {
      if (p[i]) p[cnt++] = p[i];
    }
    return;
  }

  E operator+(const E& A) const {
    E C = *this;
    for (int i = 0; i <= 63; i++) {
      if (A.p[i]) {
        C.update(A.p[i], 1);
      }
    }
    return C;
  }
} p;

EXBSGS

ll bsgs(ll a, ll b, ll mod) {
  b = b % mod;
  unordered_map<int,int> mp;
  ll t = sqrt(mod) + 1, v = b;
  for (ll i = 0; i < t; mp[v] = ++i, v = v * a % mod);
  v = a = qpow(a, t, mod);
  for (ll i = 1; i <= t; i++) {
    if (mp[v]) {
      return i * t - mp[v] + 1;
    }
    v = v * a % mod;
  }
  return -1;
}


ll exbsgs(ll a, ll b, ll p) {
	a %= p, b = (b % p + p) % p;
	if(b == 1|| p == 1 && b == 0) return 0;
	ll x, y, d = exgcd(a, p, x, y);
	if(d == 1) {
		return (bsgs(a, b, p));
	}
	if(b % d) return -1;
	exgcd(a / d, p / d, x, y);
	ll ans = exbsgs(a, b / d * x, p / d);
	if(ans == -1) return -1;
	return ans + 1;
}

矩阵快速幂

struct E{
	ll n, m;
	ll a[kMaxN][kMaxN];
	E(ll x, ll y) {
		fill(a[0], a[0] + kMaxN * kMaxN, 0), n = x, m = y;
	}
	E operator* (const E A) const {
		E c(n, A.m);
		for(int i = 1; i <= c.n; i++) {
			for(int j = 1; j <= c.m; j++) {
				for(int k = 1; k <= m; k++) {
					c.a[i][j] = (c.a[i][j] + a[i][k] * A.a[k][j] % mod) % mod;
				}
			}
		}
		return c;
	}
}; 

E qpow(E x, ll y) {
	E sum(x.n, x.n);
	for(int i = 1; i <= x.n; i++) {
		sum.a[i][i] = 1;
	}
	for(;y;(y & 1) && (sum = sum * x, 0), (x = x * x, 0), (y >>= 1));
	return sum;
}

高斯消元

ll gauss(ll n) {
	for(int i = 1, x; i <= n; i++) {
		x = i;
		for (int j = 1; j <= n; j++) {
			if(i > j && abs(a[j][j]) < EPS && abs(a[j][i]) > abs(a[x][i])) x = j; 
			if(i < j && abs(a[j][i]) > abs(a[x][i])) x = j;
		}
		for(int j = 1; j <= n + 1; j++) {
			swap(a[i][j], a[x][j]);
		}
		if(abs(a[i][i]) < EPS) continue;
		double y = a[i][i];
		for(int j = 1; j <= n + 1; j++) {
			a[i][j]	/= y;
		}
		for(int j = 1; j <= n; j++) {
			if(i == j) continue;
			y = a[j][i];
			for(int k = 1; k <= n + 1; k++) { 
				a[j][k] -= y * a[i][k];
			}
		}
	}
	for(int i = 1, f; i <= n; i++) {
		f = 0;
		for(int j = 1; j <= n; j++) {
			if(abs(a[i][j]) > EPS) f = 1;
		}
		if(!f && abs(a[i][n + 1]) > EPS) {
			return -1;
		} 
	}
	for(int i = 1; i <= n; i++) {
		if(abs(a[i][i]) < EPS) return 0;
	}
	return 1;
}

中国剩余定理

ll exgcd(ll a, ll b, ll &x, ll &y) {// 扩欧
	if(!b) return x = 1, y = 0, a;
	ll x1, y1, ans = exgcd(b, a % b, x1, y1);
	return x = y1, y = x1 - (a / b) * y1, ans;
}

ll h(ll a, ll b, ll c, ll &d) {// 解同余方程
	ll x, y;
	d = exgcd(a, b, x, y);
	if(c % d) return -1;
	a /= d, b /= d, c /= d;
	return (x * c % b + b) % b; 
}

ll excrt(ll ans = b[1] % p[1], ll k = p[1], ll x = 1) {
  //对于第一个方程特解x为 a[1] % p[1], lcm = p[1];
	for(ll i = 2, d; i <= n; i++) {// 求解每一个方程
		b[i] = ((b[i]- ans) % p[i] + p[i]) % p[i];
		if((x = h(k, p[i], b[i], d)) == -1) return -1; 
		ans = ans + x * k, k = k / d * p[i], ans = (ans % k + k) % k;// 更新特解x和lcm
	}
	return ans;
}

欧拉函数

ll fie(ll x, ll ans = 0) {
  ans = x;
  for(int i = 2; i * i <= x; i++) {
		if(x % i == 0) {
			ans = ans / i * (i - 1);
			for(;x % i == 0;x /= i);
		}
	}
  if(x != 1) {
		ans = ans / x * (x - 1);
	}
  return ans;
}
void into(ll n) {
	f[1] = 1;
	for(int i = 2; i <= n; i++) {
		if(!vis[i]) {
			p[++tot] = i, f[i] = i - 1;
		}
		for(int j = 1; p[j] * i <= n && j <= tot; j++) {
			vis[p[j] * i] = 1;
			if(i % p[j] == 0) {
				f[i * p[j]] = f[i] * p[j];
				break;
			} else {
				f[i * p[j]] = f[i] * f[p[j]];
			}
		}
	}
}

线性逆元

  void into(ll n) {
    inv[1] = 1;
    for(int i = 2; i <= n; i++) {
    	inv[i] = (mod - mod / i) * inv[mod % i] % mod;
    }
  }
posted @ 2026-04-05 19:48  mouse_boy  阅读(7)  评论(0)    收藏  举报