Congruence and modulo operation

Source: Internet
Author: User
Tags greatest common divisor

1, basic operation:///Overflow condition, B is a positive integer

Addition: (a+b) mod n = ((a mod n) + (b mod n)) mod n

Subtraction: (-a) mod n = ((a mod n)-(b mod n)+n) mod n

Multiplication: AB mod n = (a mod n) (b mod n) mod n


2, large integer modulus:

    Char st[1000];    int m;    scanf ("%s%d", st,&m);    int Len =strlen (ST);    int ans=0;    for (int i=0;i<len;++i) {        ans= (int) ((Long Long) ans*10+st[i]-' 0 ')%m);    printf ("%d\n", ans);


3, Power modulo: a^n mod m

The use of Split-treatment method to solve


4, modular linear equations (with congruence): Ax = B (mod n) that is Ax-b = NY can be extended Euclidean algorithm solution

Special case: When B=1, the solution is a inverse of modulo n, when and only if the GCD of A and N (greatest common divisor is 1) there is a unique solution




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Congruence and modulo operation

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.