首页 > 代码库 > 高精度模板

高精度模板

技术分享

 

  1 #include<iostream>  2 #include<cstdio>  3 #include<algorithm>  4 #include<cstdlib>  5 #include<algorithm>  6 #include<vector>  7 #include<cmath>  8 #include<ctime>  9 #include<cstring> 10 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout); 11 #define llg long long 12 #define maxn 60100 13 #define RG register llg 14 using namespace std; 15 llg m,T,L,mi,jz,jzw; 16 struct node 17 { 18     llg len,z[maxn/7]; 19 }a,b,ling; 20  21 char s[maxn]; 22  23 inline llg compare(node a,node b) 24 { 25     if (a.len>b.len) return 1; 26     if (a.len<b.len) return -1; 27     for (RG i=a.len;i>0;i--) 28     { 29         if (a.z[i]>b.z[i]) return 1; 30         if (a.z[i]<b.z[i]) return -1; 31     } 32     return 0; 33 } 34  35 node numcpy(node p,llg det) 36 { 37     node c; 38     memset(c.z,0,sizeof(c)); 39     for (RG i=1;i<=p.len;i++) c.z[i+det-1]=p.z[i]; 40     c.len=p.len+det-1; 41     return c; 42 } 43  44 inline node operator +(node a,node b) 45 { 46     node c; 47     memset(c.z,0,sizeof(c.z)); 48     c.len=max(a.len,b.len); 49     for (RG i=1;i<=c.len;i++) c.z[i]=a.z[i]+b.z[i]; 50     for (RG i=1;i<=c.len;i++) {c.z[i+1]+=c.z[i]/jz; c.z[i]=c.z[i] % jz;} 51     if (c.z[c.len+1]!=0) c.len++; 52     return c; 53 } 54  55 inline node operator -(node a,node b) 56 { 57     node c; 58     memset(c.z,0,sizeof(c.z)); 59     c.len=max(a.len,b.len); 60     for (RG i=1;i<=c.len;i++) c.z[i]=a.z[i]-b.z[i]; 61     for (RG i=1;i<=c.len;i++) if (c.z[i]<0) c.z[i]+=jz,c.z[i+1]--; 62     while (c.z[c.len]==0 && c.len>1) c.len--; 63     return c; 64 } 65  66 inline node operator *(node a,node b) 67 { 68     node c; 69     llg x=0; 70     memset(c.z,0,sizeof(c.z)); 71     for (RG i=1;i<=a.len;i++) 72     { 73         x=0; 74         for (RG j=1;j<=b.len;j++) 75         { 76             c.z[i+j-1]+=a.z[i]*b.z[j]+x; 77             x=c.z[i+j-1]/jz; 78             c.z[i+j-1]%=jz; 79         } 80         c.z[i+b.len]=x; 81     } 82     c.len=min(a.len+b.len,(llg)maxn-10); 83     while (c.z[c.len]==0 && c.len>1) c.len--; 84     return c; 85 } 86  87 inline node operator /(node a,node b) 88 { 89     node c; 90     memset(c.z,0,sizeof(c.z)); 91     c.len=a.len-b.len+1; 92     if (compare(a,b)==-1)  93     { 94         c.len=1; 95         return c; 96     } 97     else 98     { 99         for (RG i=c.len;i>=1;i--)100         {101             node tmp=numcpy(b,i);102             while (compare(a,tmp)>=0) 103             {104                 c.z[i]++; a=a-tmp;105             }106         }107         while (c.len>1 && c.z[c.len]==0) c.len--;108     }109     return c;110 }111 112 inline node operator %(node a,node b)113 {114     node c;115     memset(c.z,0,sizeof(c.z));116     c.len=a.len-b.len+1;117     if (compare(a,b)==-1) 118     {119         return a;120     }121     else122     {123         for (RG i=c.len;i>=1;i--)124         {125             node tmp=numcpy(b,i);126             while (compare(a,tmp)>=0) 127             {128                 c.z[i]++; a=a-tmp;129             }130         }131         while (c.len>1 && c.z[c.len]==0) c.len--;132     }133     return a;134 }135 136 inline node operator ^(node a,llg mi)137 {138     node c;139     memset(c.z,0,sizeof(c.z));140     c.len=1,c.z[1]=1;141     while (mi!=0)142     {143         if (mi%2) c=c*a;144         mi/=2;145         a=a*a;146     }147     return c;148 }149 150 inline void in_()151 {152     scanf("%lld",&m);153     ling.len=1;154     if (m!=4 && m!=5 && m!=8)155     {156         jz=(llg)1e8,jzw=8;157     }158     else159     {160         jz=10,jzw=1;161     }162     scanf("%s",s+1);163     L=strlen(s+1);164     memset(a.z,0,sizeof(a.z));165     a.len=1;166     llg p=0,x=1;167     for (RG i=1;i<=L;i++)168     {169         if (p==jzw)170         {171             p=x=1;172             a.len++;173             a.z[a.len]+=(s[L-i+1]-0)*x;174             x*=10;175         }176         else177         {178             p++;179             a.z[a.len]+=x*(s[L-i+1]-0);180             x*=10;181         }182     }183     if (m!=7)184     {185         scanf("%s",s+1);186         L=strlen(s+1);187         b.len=1; p=0; x=1;188         memset(b.z,0,sizeof(b.z));189         for (RG i=1;i<=L;i++)190         {191             if (p==jzw)192             {193                 p=x=1;194                 b.len++;195                 b.z[b.len]=(s[L-i+1]-0)*x;196                 x*=10;197             }198             else199             {200                 p++;201                 b.z[b.len]+=x*(s[L-i+1]-0);202                 x*=10;203             }204         }205     }206     else207     {208         scanf("%lld",&mi);209     }210 }211 212 inline void swap_(node &a,node &b)213 {214     node c;215     c.len=a.len;216     for (RG i=1;i<=a.len;i++) c.z[i]=a.z[i];217     a.len=b.len;218     for (RG i=1;i<=b.len;i++) a.z[i]=b.z[i];219     b.len=c.len;220     for (RG i=1;i<=c.len;i++) b.z[i]=c.z[i];221 }222 223 inline void dg_(llg x)224 {225     if (x!=0) dg_(x/10);226     if (x!=0) printf("%lld",x%10);227 }228 229 inline void dg(llg x,llg sd)230 {231     if (sd<jzw) dg(x/10,sd+1);232     printf("%lld",x%10);233 }234 235 inline void oupt(node a)236 {237     if (a.len<=1 && a.z[1]==0)238     {239         printf("0\n");240         return ;241     }242     dg_(a.z[a.len]);243     for (RG i=a.len-1;i>=1;i--)244     {245         dg(a.z[i],1);246     }247     printf("\n");248 }249 250 inline node gcd(node a,node b) 251 {252     if (compare((a%b),ling)!=1) 253         return b; 254     else return gcd(b,a%b);255 } 256 257 int main()258 {259     yyj("lazy");260     cin>>T;261     while (T--)262     {263         in_();264         if (m==1)265         {266             oupt(a+b);267         }268         if (m==2)269         {270             //node c=a-b;271             oupt(a-b);272         }273         if (m==3)274         {275             oupt(a*b);276         }277         if (m==4)278         {279             //    node c=a/b;280             oupt(a/b);281         }282         if (m==5)283         {284             oupt(a%b);285         }286         if (m==6)287         {288             cout<<"FUCK";289         }290         if (m==7)291         {292             node c;293             memset(c.z,0,sizeof(c.z));294             c.len=1,c.z[1]=1;295             while (mi!=0)296             {297                 if (mi%2) c=c*a;298                 mi/=2;299                 a=a*a;300             }301             oupt(c);302         }303         if (m==8)304         {305             oupt(gcd(a,b));306         }307         if (m==9)308         {309             cout<<"FUCK!";310         }311     }312     return 0;313 }

 

高精度模板