快读快输模板快捷方式
I/O
//fread
namespace IO {
const int kS = 1 << 22;
char ibuf[kS], *p1 = ibuf, *p2 = ibuf;
inline char gc() {
return p1 == p2 && (p2 = ibuf + fread(p1 = ibuf, 1, kS, stdin), p1 == p2) ? EOF : *p1++;
}
int x,f;
inline int read() {
char c = gc();
x = 0,f=1;
while (!isdigit(c)) {if(c=='-')f=0;c = gc();}
while (isdigit(c)) x = x * 10 + c - '0', c = gc();
return f?x:-x;
}
inline int read() {
char c = gc();
x = 0;
while (!isdigit(c)) c = gc();
while (isdigit(c)) x = x * 10 + c - '0', c = gc();
return x;
}
}
using IO::read;
//unsigned
inline int read(){
register char ch=getchar();register int x=0;
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x;
}
void print(int x){
if(x/10)print(x/10);
putchar(x%10+48);
}
//signed
inline int read(){
register char ch=getchar();register int x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void print(int x){
if(x<0){print(-x);return;}
if(x/10)print(x/10);
putchar(x%10+48);
}
火车头
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
手写哈希表
struct hash{
#define mod 1000003
ull to[100010],cnt=0;
int head[1000003],nxt[100010],val[100010];
inline void clr(){
for(int i=1;i<=cnt;i++){
head[to[i]%mod]=0;
to[i]=val[i]=nxt[i]=0;
}
cnt=0;
}
inline void ins(ull x,int w){
nxt[++cnt]=head[x%mod];
to[cnt]=x;
val[cnt]=w;
head[x%mod]=cnt;
}
inline int find(ull x,int w){
for(int i=head[x%mod];i;i=nxt[i]){
if(to[i]==x){
val[i]+=w;
return val[i];
}
}
return 0;
}
}mp;
unordered_map<pair<int,int>,int,pairhash>
/unordered_set<pair<int,int>,pairhash>
struct pairhash {
public:
template <typename T, typename U>
size_t operator()(const pair<T, U> &x) const{
return hash<T>()((x.first-x.second)<<5) ^ hash<U>()(x.second<<1)+(hash<T>()(x.first>>1) & hash<U>()(x.second<<1));
}
};
unordered_map 加速加速加加速
mp.reserve(2048);
mp.max_load_factor(0.38);
逆元
#include <bits/stdc++.h>
#define int long long
using namespace std;
int qp(int a,int b,int mod){
int c=1;
while(b){
if(b&1)c=c*a%mod;
a=a*a%mod,b>>=1;
}
return c;
}
signed main(){
int a,b;cin>>a>>b;
cout<<qp(a,b-2,b);
}
取模常数优化
inline void adde(int &x,int y){(x+=y)>=mod&&(x-=mod);}
inline void sube(int &x,int y){(x-=y)<0&&(x+=mod);}
inline int mul(int x,int y) {
int t=1ll*x*y-(ll)(x*1./mod*y+0.5)*mod;
return (t<0?t+mod:t);
}
快速乘(long double)
long long qmod(long long a,long long b,long long mod) {
long long d=(long double)a/mod*b;
long long ans=(unsigned long long)a*b-(unsigned long long)d*mod;
return (ans+mod)%mod;
}
平面最近点对
signed main(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].x>>a[i].y;
sort(a+1,a+n+1,cmpx);
printf("%.4f",solve(1,n));
}
double solve(int l,int r){
if(r-l+1<=2){
if(r-l+1==2)return dist(a[l],a[r]);
else return INF;
}
int mid=l+r>>1;
double d=min(solve(l,mid),solve(mid+1,r)),ans=d;
vector<point>b;b.clear();
for(int i=l;i<=r;i++)
if(fabs(a[i].x-a[mid].x)<=d)
b.push_back(a[i]);
sort(b.begin(),b.end(),cmpy);
for(int i=0;i<b.size();i++){
for(int j=i+1;j<b.size();j++){
double dis=dist(b[i],b[j]);
if(dis<=d)ans=min(ans,dis);
else break;
}
}
return ans;
}
多项式转下降幂
S[0][0]=1;
for(int i=1;i<=m;i++){
S[i][0]=(mod+1ll-i)*S[i-1][0]%mod;
for(int j=1;j<=i;j++)S[i][j]=((mod+1ll-i)*S[i-1][j]%mod+S[i-1][j-1])%mod;
}
for(int i=m;i;i--)for(int j=i-1;~j;j--)(a[j]-=1ll*a[i]*S[i][j]%mod)%=mod;
for(int i=0;i<=m;i++)a[i]=(a[i]+mod)%mod;
Dijkstra 次短路
void dij(){
priority_queue<PII, vector<PII>, greater<PII> > q;
memset(dist, 0x3f, sizeof(dist));
memset(dist2, 0x3f, sizeof(dist2));
dist[1] = 0;
q.push(PII(0, 1));
while(!q.empty()){
PII p = q.top(); q.pop();
int v = p.se, d = p.fi;
if(dist2[v] < d) continue;
int len = G[v].size();
rep(i, 0, len - 1){
edge &e = G[v][i];
int d2 = d + e.cost;
if(dist[e.to] > d2){
swap(dist[e.to], d2);
q.push(PII(dist[e.to], e.to));
}
if(dist2[e.to] > d2 && dist[e.to] < d2){
dist2[e.to] = d2;
q.push(PII(dist2[e.to], e.to));
}
}
}
}