模板……有需要的人也可以拿走
1、高精度模板(v1.0)
注意:主要耗时在乘法、除法和乘方。其中高精度乘方有快速幂优化,常数大致是乘法的5~10倍。除法用二分写的效率较低,大概是乘法的20~30倍,能不用尽量不用
1 #define mx 300 2 struct gaojing{ 3 int len; 4 int a[mx+10]; 5 }zero,one; 6 inline void set0(gaojing &s)//高精清零 7 { 8 s.len=1; 9 for (int i=1;i<=mx+5;i++)s.a[i]=0; 10 } 11 inline void inputn(gaojing &a)//高精输入 12 { 13 set0(a); 14 char ch=getchar(); 15 while (ch<'0'||ch>'9')ch=getchar(); 16 while (ch>='0'&&ch<='9') 17 { 18 a.a[a.len++]=ch-'0'; 19 ch=getchar(); 20 } 21 a.len--; 22 int change[mx+15]; 23 for (int i=1;i<=a.len;i++) 24 change[i]=a.a[i]; 25 for (int i=1;i<=a.len;i++) 26 a.a[i]=change[a.len-i+1]; 27 while (a.a[a.len]==0)a.len--; 28 } 29 inline void put(gaojing a)//高精输出 30 { 31 for (int i=a.len;i>=1;i--)printf("%d",a.a[i]); 32 printf(" "); 33 } 34 inline bool operator < (const gaojing &a,const gaojing &b)//高精< 35 { 36 if (a.len<b.len)return 1; 37 if (a.len>b.len)return 0; 38 for (int i=a.len;i>=1;i--) 39 { 40 if (a.a[i]<b.a[i])return 1; 41 if (a.a[i]>b.a[i])return 0; 42 } 43 return 0; 44 } 45 inline bool operator == (const gaojing &a,const gaojing &b)//高精== 46 { 47 if (a.len!=b.len)return 0; 48 for (int i=a.len;i>=1;i--) 49 { 50 if (a.a[i]!=b.a[i])return 0; 51 } 52 return 1; 53 } 54 inline gaojing max(const gaojing &a,const gaojing &b)//高精max 55 { 56 if (a<b)return b; 57 else return a; 58 } 59 inline gaojing min(const gaojing &a,const gaojing &b)//高精min 60 { 61 if (a<b)return a; 62 else return b; 63 } 64 inline gaojing operator + (const gaojing &a,const gaojing &b)//高精+ 65 { 66 gaojing c;set0(c); 67 int maxlen=max(a.len,b.len); 68 for (int i=1;i<=maxlen;i++) 69 { 70 c.a[i]=c.a[i]+a.a[i]+b.a[i]; 71 if (c.a[i]>=10) 72 { 73 c.a[i+1]+=c.a[i]/10; 74 c.a[i]%=10; 75 } 76 } 77 c.len=maxlen+4; 78 while (!c.a[c.len]&&c.len>1) c.len--; 79 return c; 80 } 81 inline gaojing operator - (const gaojing &a,const gaojing &b)//高精- 82 { 83 gaojing c;set0(c); 84 gaojing d;d=a; 85 for (int i=1;i<=b.len;i++) 86 { 87 c.a[i]=d.a[i]-b.a[i]; 88 if (c.a[i]<0) 89 { 90 c.a[i]+=10; 91 int now=i+1; 92 while (!d.a[now]) 93 { 94 d.a[now]=9; 95 now++; 96 } 97 d.a[now]--; 98 } 99 } 100 for (int i=b.len+1;i<=d.len;i++)c.a[i]=d.a[i]; 101 c.len=d.len; 102 while (c.a[c.len]==0&&c.len>1)c.len--; 103 return c; 104 } 105 inline gaojing operator * (const gaojing &a,const gaojing &b)//高精* 106 { 107 gaojing c;set0(c); 108 for(int i=1;i<=a.len;i++) 109 for (int j=1;j<=b.len;j++) 110 c.a[i+j-1]+=a.a[i]*b.a[j]; 111 c.len=a.len+b.len+5; 112 for (int i=1;i<=c.len;i++) 113 { 114 c.a[i+1]+=c.a[i]/10; 115 c.a[i]%=10; 116 } 117 while (!c.a[c.len]&&c.len>1)c.len--; 118 return c; 119 } 120 inline void div_by_2(gaojing &a) 121 { 122 for (int i=a.len;i>=1;i--) 123 { 124 if (a.a[i]&1 && i!=1)a.a[i-1]+=10; 125 a.a[i]/=2; 126 } 127 while (!a.a[a.len]&&a.len>1)a.len--; 128 } 129 inline gaojing operator / (gaojing a,const gaojing &b)//高精/ 130 { 131 gaojing l,r,ans; 132 set0(l);l.len=1; 133 set0(r);r=a; 134 set0(ans);ans.len=1; 135 while (l<r||l==r) 136 { 137 gaojing mid=l+r; 138 div_by_2(mid); 139 if(mid*b==a)return mid; 140 if(mid*b<a){ans=mid;l=mid+one;} 141 if(a<mid*b)r=mid-one; 142 } 143 return ans; 144 } 145 inline gaojing operator ^(const gaojing &a,int p)//高精乘方 146 { 147 gaojing ans=one,mult=a; 148 while (p) 149 { 150 if (p&1)ans=ans*mult; 151 mult=mult*mult; 152 p>>=1; 153 } 154 return ans; 155 } 156 inline void chushihua()//初始化,对0、1高精度常数赋值 157 { 158 set0(zero); zero.len=1; 159 set0(one);one.len=1;one.a[1]=1; 160 } 161 int main() 162 { 163 chushihua(); 164 }
2、打cf必备的一大串头文件(v2.1) ——连快速输出都有了还有什么好怕的
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define pi 3.1415926535897932384626433832795028841971 16 using namespace std; 17 inline LL read() 18 { 19 LL x=0,f=1;char ch=getchar(); 20 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 21 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 22 return x*f; 23 } 24 inline void write(LL a) 25 { 26 if (a<0){printf("-");a=-a;} 27 if (a>=10)write(a/10); 28 putchar(a%10+'0'); 29 } 30 inline void writeln(LL a){write(a);printf(" ");} 31 int main() 32 { 33 }
3、最短路模板(v1.0) spfa(+slf优化)和dijkstra(+stl堆优化)
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define N 100010 6 #define M 200010 7 using namespace std; 8 int n,m,s,t,cnt; 9 struct edge{int to,next,v;}e[M]; 10 long long dist[N]; 11 bool mrk[N]; 12 int q[N],head[N]; 13 inline void insert(int u,int v,int w) 14 { 15 e[++cnt].to=v; 16 e[cnt].v=w; 17 e[cnt].next=head[u]; 18 head[u]=cnt; 19 } 20 inline void spfa() 21 { 22 for (int i=0;i<=n;i++)dist[i]=(long long)1<<40; 23 q[1]=s;dist[s]=0;mrk[s]=1; 24 int t=0,w=1; 25 while (t!=w) 26 { 27 if (t==N-1)t=0;else t++; 28 int now=q[t]; 29 for (int i=head[now];i;i=e[i].next) 30 if (dist[e[i].to]>dist[now]+e[i].v) 31 { 32 dist[e[i].to]=dist[now]+e[i].v; 33 if (!mrk[e[i].to]) 34 { 35 mrk[e[i].to]=1; 36 if (dist[e[i].to]<=dist[now]) 37 { 38 q[t]=e[i].to; 39 if(t==0)t=N-1;else t--; 40 continue; 41 } 42 if (w==N-1)w=0;else w++; 43 q[w]=e[i].to; 44 } 45 } 46 mrk[now]=0; 47 } 48 } 49 int main() 50 { 51 scanf("%d%d%d%d",&n,&m,&s,&t); 52 for(int i=1;i<=m;i++) 53 { 54 int x,y,z; 55 scanf("%d%d%d",&x,&y,&z); 56 insert(x,y,z); 57 } 58 spfa(); 59 printf("%lld ",dist[t]); 60 }
dijkstra
……恩先这样吧还有的再加