High-Precision related templates

Source: Internet
Author: User
Tags mul stdin strlen
/* high-precision addition. * * #include <cstring> #include <cstdio> #include <iostream> #
Define MAXN 10001 using namespace std;
int a[maxn],b[maxn],c[maxn],l1,l2,l3;
Char M[MAXN],N[MAXN];
    void Slove () {L3=max (L1,L2);
        for (int i=1;i<=l3;i++) {c[i]+=a[i]+b[i];
        C[I+1]+=C[I]/10;
    c[i]%=10;
    } if (c[l3+1]) l3++;
    while (!c[l3]&&l3>1) l3--;
for (int i=l3;i>=1;i--) printf ("%d", c[i]);
    } int main () {freopen ("add.in", "R", stdin);
    Freopen ("Add.out", "w", stdout);
    scanf ("%s", m+1); L1=strlen (m+1);
    scanf ("%s", n+1); L2=strlen (n+1);
    for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
    Slove ();
return 0; }
/* High precision subtraction.
*/#include <cstring> #include <cstdio> #include <iostream> #define MAXN 10001 using namespace std;
int a[maxn],b[maxn],c[maxn],l1,l2,l3;
Char M[MAXN],N[MAXN];
        void Pre () {if (L1&GT;L2) {for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
        } else if (L1&LT;L2) {printf ("-");
        for (int i=1;i<=l1;i++) b[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) a[i]=n[l2-i+1]-48;
        } else if (strcmp (m+1,n+1) >=0) {for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
        } else {printf ("-");
        for (int i=1;i<=l1;i++) b[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) a[i]=n[l2-i+1]-48;
    }} void Slove () {L3=max (L1,L2);
        for (int i=1;i<=l3;i++) {if (A[i]<b[i]) a[i+1]--, a[i]+=10;
    C[i]=a[i]-b[i];
    } while (!c[l3]&&l3>1) l3--; for (int i=l3;i>=1;i--) printf ("%d", c[i]);
    } int main () {freopen ("sub.in", "R", stdin);
    Freopen ("Sub.out", "w", stdout);
    scanf ("%s", m+1); L1=strlen (m+1);
    scanf ("%s", n+1); L2=strlen (n+1);
    Pre ();
    Slove ();
return 0; }
/* high-precision multiplication. * * #include <cstring> #include <cstdio> #include <iostream> #
Define MAXN 10001 using namespace std;
int a[maxn],b[maxn],c[maxn],l1,l2,l3;
Char M[MAXN],N[MAXN];
    void Slove () {int x;
    L3=L1+L2;
        for (int i=1;i<=l1;i++) {int x=0;
            for (int j=1;j<=l2;j++) {c[i+j-1]+=a[i]*b[j];
            C[I+J]+=C[I+J-1]/10;
            X=C[I+J-1]/10;
        c[i+j-1]%=10;
    } c[i+l2]=x;
    } if (c[l3+1]) l3++;
    while (!c[l3]&&l3>1) l3--;
for (int i=l3;i>=1;i--) printf ("%d", c[i]);
    } int main () {freopen ("mul.in", "R", stdin);
    Freopen ("Mul.out", "w", stdout);
    scanf ("%s", m+1); L1=strlen (m+1);
    scanf ("%s", n+1); L2=strlen (n+1);
    for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
    Slove ();
return 0; }
/* High Precision division.
*/#include <cstring> #include <cstdio> #include <iostream> #define MAXN 10001 using namespace std;
int a[maxn],b[maxn],c[maxn],tmp[maxn],l1,l2,l3;
Char M[MAXN],N[MAXN];
    BOOL CMP () {if (TMP[0]&GT;L1) return false;
    if (L1>tmp[0]) return true;
        for (int i=l1;i>=1;i--) {if (A[i]>tmp[i]) return true;
    if (A[i]<tmp[i]) return false;
} return true;
        } bool Cmp1 () {for (int i=1;i<=l1;i++) {if (M[i]>n[i]) return true;
    if (M[i]<n[i]) return false;
} return true;
        } void Slovejian () {for (int i=1;i<=l1;i++) {if (A[i]<tmp[i]) a[i+1]--, a[i]+=10;
    A[i]-=tmp[i];
} while (!a[l1]&&l1>1) l1--;
    } void Slove () {l3=l1-l2+1;
        for (int i=l3;i>=1;i--) {memset (tmp,0,sizeof tmp);
        for (int j=1;j<=l2;j++) TMP[I+J-1]=B[J];
        Tmp[0]=l2+i-1;
    while (CMP ())) C[i]++,slovejian (); } while (!c[l3]&&l3>1)l3--;
for (int i=l3;i>=1;i--) printf ("%d", c[i]);
    } int main () {scanf ("%s", m+1); L1=strlen (m+1);
    scanf ("%s", n+1); L2=strlen (n+1);
        if (L1&GT;L2) {for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
        } else if (L1&LT;L2) {for (int i=1;i<=l1;i++) b[i]=m[l1-i+1]-48;
        for (int i=1;i<=l2;i++) a[i]=n[l2-i+1]-48;
    int t;t=l1,l1=l2,l2=t;
        } else if (CMP1 ()) {for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48;
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
        } else {for (int i=1;i<=l1;i++) b[i]=m[l1-i+1]-48;
        for (int i=1;i<=l2;i++) a[i]=n[l2-i+1]-48;
    int t;t=l1,l1=l2,l2=t;
    } slove ();
return 0; }
/* High precision modulus to find redundancy.
*/#include <cstring> #include <cstdio> #include <iostream> #define MAXN 10001 using namespace std;
int a[maxn],b[maxn],ans[maxn],c[maxn],tmp[maxn],l1,l2,l3;
Char M[MAXN],N[MAXN];
    BOOL CMP () {if (TMP[0]&GT;L1) return false;
    if (L1>tmp[0]) return true;
        for (int i=l1;i>=1;i--) {if (A[i]>tmp[i]) return true;
    if (A[i]<tmp[i]) return false;
} return true;
        } void Slovejian () {for (int i=1;i<=l1;i++) {if (A[i]<tmp[i]) a[i+1]--, a[i]+=10;
    A[i]-=tmp[i];
} while (!a[l1]&&l1>1) l1--;
    } void Chu () {l3=l1-l2+1;
        for (int i=l3;i>=1;i--) {memset (tmp,0,sizeof tmp);
        for (int j=1;j<=l2;j++) TMP[I+J-1]=B[J];
        Tmp[0]=l2+i-1;
    while (CMP ())) C[i]++,slovejian ();
    }} void Mul () {memset (tmp,0,sizeof tmp);
    TMP[0]=L2+L3;
        for (int i=1;i<=l2;i++) {int x=0; for (int j=1;j<=l3;j++) {tmp[i+J-1]+=B[I]*C[J];
            X=TMP[I+J-1]/10;
            TMP[I+J]+=TMP[I+J-1]/10;
        tmp[i+j-1]%=10;
    } tmp[i+l3]=x;
    } if (tmp[tmp[0]+1]) tmp[0]++;
while (tmp[tmp[0]]&&tmp[0]>1) tmp[0]--;
    } void Slove () {chu (); Mul ();
    Ans[0]=max (L2,L3);
        for (int i=1;i<=ans[0];i++) {if (Ans[i]<tmp[i]) ans[i+1]--, ans[i]+=10;
    Ans[i]-=tmp[i];
    } while (!ans[ans[0]]&&ans[0]>1) ans[0]--;
for (int i=ans[0];i>=1;i--) printf ("%d", ans[i]);
    } int main () {scanf ("%s", m+1); L1=strlen (m+1);
    scanf ("%s", n+1); L2=strlen (n+1);
    for (int i=1;i<=l1;i++) a[i]=m[l1-i+1]-48,ans[i]=a[i];
    for (int i=1;i<=l2;i++) b[i]=n[l2-i+1]-48;
    Slove ();
return 0; }
/* High-precision wrong-row formula.
F[n]= (n-1) (F[n-1]+f[n-2]). */#include <iostream> #include <cstring> #include <cstdio> #define MAXN 201 #define MAXM 1001 using names
Pace std;
int F[MAXN][MAXM],N,TMP[MAXM],T[MAXM];
    int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar ();
return x*f;
    } void Slove (int x) {memset (t,0,sizeof t);
while (x) T[++t[0]]=x%10,x/=10;return;
    } void Add (int c[],int a[],int b[]) {c[0]=a[0]+b[0];
        for (int i=1;i<=c[0];i++) {c[i]+=a[i]+b[i];
        C[I+1]+=C[I]/10;
    c[i]%=10;
    } if (c[c[0]+1]) c[0]++;
    while (!c[c[0]]&&c[0]>1) c[0]--;
Return
    } void Mul (int c[],int a[],int b[]) {c[0]=a[0]+b[0];
        for (int i=1;i<=a[0];i++) {int x=0;
            for (int j=1;j<=b[0];j++) {c[i+j-1]+=a[i]*b[j];
            C[I+J]+=C[I+J-1]/10; X=c[i+j-1]/10;
        c[i+j-1]%=10;
    } c[i+b[0]]=x;
    } while (!c[c[0]]&&c[0]>1) c[0]--;
Return
    } int main () {n=read ();
    if (n==0) {printf ("1"); return 0;}
    f[1][0]=1,f[1][1]=0;
    F[2][0]=1,f[2][1]=1;
        for (int i=3;i<=n;i++) {memset (tmp,0,sizeof tmp);
        Add (Tmp,f[i-1],f[i-2]);
        Slove (i-1);
    Mul (F[I],T,TMP);
    } for (int i=f[n][0];i>=1;i--) printf ("%d", f[n][i]);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.