Congruence problem, multiplication modulo inverse "template"

Source: Internet
Author: User

Expand Euclid, find a group of x, Y, making gcd (A, b) = d = A * x + b * y

void exgcd (int a,int b,int &d,int &x,int &y) {    if (b = = 0)    {        x = 1;        y = 0;        D = A;    }    else    {        exgcd (b,a%b,d,y,x);        Y-= x* (A/b);    }}

Expand Euclidean, ask for all solution x, y, make C = a * x + b * y

BOOL Modeequal (int a,int b,int c)   //Solution a*x + b*y = c;a*x≡c (mod b) {    int x,y,d,x0;    EXGCD (a,b,d,x,y);    if (c%d) return false;   No solution    x0 = x * (C/D)% B;    for (int i = 1; i < D; ++i)  //Output All        of the solutions printf ("%d\n", (x0+i* (b/d))%b);    return true;}

Expand Euclid, ask a about the inverse of n a^-1, make A * a^-1≡1 (mod n)

int modinverse (int a,int n,int &d,int &x,int &y) {    exgcd (a,n,d,x,y); if (1 D! = 0) return-1;  Non-existent modulo inverse int ans = x/d < 0? X/d + n:x/d;return ans;     return modulo inverse a^-1}

Extended Euclidean, Solution X, satisfies the congruence equation set X≡ri (mod Ai)

int modequals (int N)  //Solution equation set X≡ri (mod Ai) {    int a,b,d,x,y,c,a1,r1,a2,r2;    BOOL flag = 1;  Mark whether there is a solution    scanf ("%d%d", &a1,&r1);    for (int i = 1; i < N; ++i)    {        scanf ("%d%d", &A2,&R2);        A = A1, B = A2, c = r2-r1;        EXGCD (a,b,d,x,y);        if (c% d! = 0)            flag = 0;        int t = b/d;        x = (x* (C/D)%t + t)% T;        R1 = A1 * x + R1;        A1 = A1 * (a2/d);    }    if (!flag)  R1 =-1;    return R1;  Find solution, 1 means no solution}

Extended Euclidean, Solution X, satisfies high-time congruence equation a^x≡b (mod C)

#define LL __int64const int maxn = 65535;struct hash{int A;    int b; int next;}    Hash[maxn*2];int flag[maxn+66];int top,idx;void ins (int a,int b) {int k = b & maxn;        if (flag[k]! = IDX) {Flag[k] = idx;        Hash[k].next =-1;        HASH[K].A = A;        hash[k].b = b;    Return        } while (Hash[k].next! =-1) {if (hash[k].b = = b) return;    K = Hash[k].next;    } hash[k].next = ++top;    Hash[top].next =-1;    HASH[TOP].A = A; hash[top].b = b;}    int Find (int b) {int k = b & maxn;    if (flag[k]! = idx) return-1;        while (k! =-1) {if (hash[k].b = = b) return hash[k].a;    K = Hash[k].next; } return-1;}    int GCD (int a,int b) {if (b = = 0) return A; Return GCD (b,a%b);}        void exgcd (int a,int b,int &d,int &x,int &y) {if (b = = 0) {x = 1;        y = 0;    D = A;        } else {EXGCD (b,a%b,d,y,x);    Y-= x* (A/b); }}int inval (int A,int b,int n) {int x,y,d,e;    EXGCD (A,n,d,x,y);    E = (LL) x*b%n; Return e < 0? e + n:e;}    int Powmod (ll a,int b,int c) {ll ret = 1%c;    a%= C;        while (b) {if (b&1) ret = ret*a%c;        A = a*a%c;    b >>= 1; } return ret;}    int babystep (int a,int b,int c)//Solution a^x≡b (mod C) {top = MAXN;    ++idx;    LL buf = 1%c,d = Buf,k;    int d = 0,temp,i;    for (i = 0; I <=; buf = buf*a%c,++i) {if (buf = = B) return i;        } while ((temp = GCD (a,c)) = 1) {if (B temp) return-1;        ++d;        C/= temp;        B/= temp;    D = d*a/temp%c;    } int M = (int) ceil (sqrt ((double) C));    for (buf = 1%c,i = 0; I <= M; buf = buf*a%c,++i) ins (i,buf); for (i = 0,k = Powmod (LL) a,m,c); I <= M;        D = d*k%c,++i) {temp = inval ((int) d,b,c);        int W;    if (temp >= 0 && (w = Find (temp))! =-1) return I * M + W + D;  } return-1; No solution} 



Congruence problem, multiplication modulo inverse "template"

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.