POJ 2115 C Looooops [linear modulus equation]

Source: Internet
Author: User
Tags modulus

Convert it to c * x = B-a mod (2 ^ k), and solve the minimum positive integer of the modulus linear equation.

Sample Input
3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0

Sample Output
0
2
32766
FOREVER

Equation solving: ax = B (mod n); [ax % n = B % n]
Set one solution of the linear modulus equation to x0.
Condition ①: d = gcd (a, n)
Condition ②: with d = ax1 + ny, the x1 value is obtained from the expanded Euclidean (Egcd ).
Condition ③: B % d = 0 (condition with solution)
Then x0 = x1 * (B/d );

Proof:
Because: it is easy to obtain d = gcd (a, n), then d = ax1 + ny ① (Extended Euclidean theorem)
Equation ① 2-side simultaneous modulus n: d % n = ax1 % n ②
B % d = 0, that is, B is a multiple of d;
Therefore, (B/d) must be an integer;
So from ②: B % n = d * (B/d) % n = ax1 * (B/d) % n = ax % n
So it is easy to see that x = x1 * (B/d) is an integer solution of the equation.

References:

C ++ code
# Include <iostream>
# Include <fstream>
# Include <algorithm>
# Include <string>
# Include <set>
// # Include <map>
# Include <queue>
# Include <utility>
# Include <iomanip>
# Include <stack>
# Include <list>
# Include <vector>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <cmath>
# Include <ctime>
# Include <ctype. h>
Using namespace std;
# Define L long

L Egcd (L a, L B, L & x, L & y) // extends Euclidean
{
L d, tp;
If (B = 0)
{
X = 1, y = 0;
Return;
}
D = Egcd (B, a % B, x, y );
Tp = x;
X = y;
Y = tp-(a/B) * y;
Return d;
}
Void MLE (L a, L B, L n) // solves the modulus linear equation
{
L x, y, d;
D = Egcd (a, n, x, y );
If (B % d = 0)
{
L x0 = (B/d * x) % n + n;
Cout <x0% (n/d) <endl;
// For a group of remainder formed by countless solutions: the number of cycles is d, the length of the cycle is n/d, that is, the minimum positive integer is in n/d. This is what the teacher said, but I forgot why it involves the concept of group ......
}
Else puts ("FOREVER"); // no solution
}
Int main ()
{
L a, B, c;
Int k;
While (cin> a> B> c> k)
{
If (! A &&! B &&! C &&! K)
Break;
MLE (c, B-a, 1LL <k );
}
Return 0;
}

Related Article

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.