模板总结(个人向)

1|0快速读入输出

namespace io{ #define gc() (iS == iT ? (iT = (iS = ibuff) + fread(ibuff, 1, SIZ, stdin), (iS == iT ? EOF : *iS++)) : *iS++) const int SIZ = 1 << 21 | 1; char *iS, *iT, ibuff[SIZ], obuff[SIZ], *oS = obuff, *oT = oS + SIZ - 1, fu[110], c; int fr; inline void out() { fwrite(obuff, 1, oS - obuff, stdout); oS = obuff; } template<class Type> inline void read(Type &x) { x = 0; Type y = 1; for (c = gc(); (c > '9' || c < '0') && c != '-'; c = gc()); c == '-' ? y = -1 : x = (c & 15); for (c = gc(); c >= '0' && c <= '9'; c = gc()) x = x * 10 + (c & 15); x *= y; } template<class Type> inline void print(Type x, char text = '\n') { if (x < 0) *oS++ = '-', x *= -1; if (x == 0) *oS++ = '0'; while (x) fu[++fr] = x % 10 + '0', x /= 10; while (fr) *oS++ = fu[fr--]; *oS++ = text; out(); } } using namespace io;

2|0大整数类

namespace BigInteger { #define maxn 10005 using std::sprintf; using std::string; using std::max; using std::istream; using std::ostream; struct Big_integer{ int d[maxn], len; void clean() { while(len > 1 && !d[len-1]) len--; } Big_integer() { memset(d, 0, sizeof(d)); len = 1; } Big_integer(int num) { *this = num; } Big_integer(char* num) { *this = num; } Big_integer operator = (const char* num){ memset(d, 0, sizeof(d)); len = strlen(num); for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0'; clean(); return *this; } Big_integer operator = (int num){ char s[10005]; sprintf(s, "%d", num); *this = s; return *this; } Big_integer operator + (const Big_integer& b){ Big_integer c = *this; int i; for (i = 0; i < b.len; i++){ c.d[i] += b.d[i]; if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++; } while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++; c.len = max(len, b.len); if (c.d[i] && c.len <= i) c.len = i+1; return c; } Big_integer operator - (const Big_integer& b){ Big_integer c = *this; int i; for (i = 0; i < b.len; i++){ c.d[i] -= b.d[i]; if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--; } while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--; c.clean(); return c; } Big_integer operator * (const Big_integer& b)const{ int i, j; Big_integer c; c.len = len + b.len; for(j = 0; j < b.len; j++) for(i = 0; i < len; i++) c.d[i+j] += d[i] * b.d[j]; for(i = 0; i < c.len-1; i++) c.d[i+1] += c.d[i]/10, c.d[i] %= 10; c.clean(); return c; } Big_integer operator / (const Big_integer& b){ int i, j; Big_integer c = *this, a = 0; for (i = len - 1; 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; } Big_integer operator % (const Big_integer& b){ int i, j; Big_integer a = 0; for (i = len - 1; 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; } Big_integer operator += (const Big_integer& b){ *this = *this + b; return *this; } bool operator <(const Big_integer& 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 Big_integer& b) const{return b < *this;} bool operator<=(const Big_integer& b) const{return !(b < *this);} bool operator>=(const Big_integer& b) const{return !(*this < b);} bool operator!=(const Big_integer& b) const{return b < *this || *this < b;} bool operator==(const Big_integer& b) const{return !(b < *this) && !(b > *this);} string str() const{ char s[maxn]={}; for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0'; return s; } }; istream& operator >> (istream& in, Big_integer& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream& out, const Big_integer& x) { out << x.str(); return out; } } using namespace BigInteger;

3|0图论算法

int cnt,head[100],nxt[100],to[100],edge[100]; int vis[1000010],dis[1000010]; int inq[1000010],dis[1000010];

3|1边表存图

inline void add(int x,int y,int x){ cnt++; nxt[cnt]=head[x]; head[x]=cnt; to[cnt]=y; edge[cnt]=z; } for(int i=head[x];i;i=nxt[i])

3|2SPFA

inline void SPFA(int s){ for(int i=1;i<=n;i++){ dis[i]=0x7fffffff; } queue<int> Q; Q.push(s); inq[s]=1; dis[s]=0; while(!Q.empty()){ int fr=Q.front(); Q.pop(); inq[fr]=0; for(int i=head[fr];i;i=nxt[i]){ int v=to[i],w=edge[i]; if(dis[fr]+w<dis[v]){ dis[v]=dis[fr]+w; if(!inq[v])Q.push(v),inq[v]=1; } } } }

3|3DIJKSTRA

struct node{ int u,d; bool operator<(const node & rhs){ return d>rhs.d; } }temp; inline void DIJKSTRA(int s){ for(int i=1;i<=n;i++){ dis[i]=0x7fffffff; } priority_queue<node> Q; Q.push((node){s,0}); while(!Q.empty()){ temp=Q.top(); Q.pop(); int u=temp.u; if(vis[u]){ continue; } vis[u]=1; for(int i=head[u];i;i=nxt[i]){ int w=edge[i],v=to[i]; if(dis[u]+w<dis[v]){ dis[v]=dis[u]+w; } Q.push((node){v,dis[v]}); } } }

4|0数学&&其他

4|1辗转相除求gcd

inline int gcd(int a,int b){ return b?gcd(b,a%b):a; }

4|2快速幂卡粟米

inline int pow(int a,int b){ int base=a,ans=1,temp=b; while(temp){ if(temp&1){ ans*=base; } temp>>1; base*=base; } return ans; }

4|3欧拉质数筛法

inline void prime(int n) { v[0] = v[1] = 1; for (int i = 2; i <= n; i++) { if (!v[i]) { isin[i] = 1; v[i] = 1; for (int j = 1; j * i <= n; j++) v[i * j] = 1; } } }

4|4归并排序

inline void marge(int a[],int l,int mid,int r,int temp[]){ int i=l,j=mid+1,n=mid,m=r,k=l; while(i<=n&&j<=m){ if(a[i]<=a[j]){ temp[k++]=a[i++]; }else{ temp[k++]=a[j++]; } } while(i<=n){ temp[k++]=a[i++]; } while(j<=n){ temp[k++]=a[j++]; } for(int i=l;i<=r;i++){ a[i]=temp[i]; } } inline void msort(int a[],int l,int r,int temp[]){ if(l<r){ int mid=(l+r)/2; msort(a,l,mid,temp); msort(a,mid+1,r,temp); marge(a,l,mid,r,temp); } }

__EOF__

本文作者Kdlyh
本文链接https://www.cnblogs.com/kdlyh/p/17777003.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   加固文明幻景  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示