模线性方程求解poj 2115

http://www.cppblog.com/mythit/archive/2009/06/12/87514.html

推论1:方程ax=b(mod n)对于未知量x有解,当且仅当gcd(a,n) | b。
    推论2:方程ax=b(mod n)或者对模n有d个不同的解,其中d=gcd(a,n),或者无解。
    定理1:设d=gcd(a,n),假定对整数x和y满足d=ax+ny(比如用扩展Euclid算法求出的一组解)。如果d | b,则方程ax=b(mod n)有一个解x0满足x0=x*(b/d) mod n 。特别的设e=x0+n,方程ax=b(mod n)的最小整数解x1=e mod (n/d),最大整数解x2=x1+(d-1)*(n/d)。
    定理2:假设方程ax=b(mod n)有解,且x0是方程的任意一个解,则该方程对模n恰有d个不同的解(d=gcd(a,n)),分别为:xi=x0+i*(n/d) mod n 。
    以上定理的具体证明见《算法导论》。P533

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

static long x, y;

long exGCD(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long r = exGCD(b, a % b);
long t = x;
x = y;
y = t - a / b * y;
return r;
}

long moduler_linear(long a, long b, long n) {
long d = exGCD(a, n);
if (b % d != 0) return -1;
long e = x * (b / d) % n + n;
return e % (n / d);
}

void run() {
long a, b, c, k;
while (true) {
a = cin.nextInt();
b = cin.nextInt();
c = cin.nextInt();
k = cin.nextInt();
if (a == 0 && b == 0 && c == 0 && k == 0)
break;

long t = moduler_linear(c, b - a, 1L << k);

if (t == -1) System.out.println("FOREVER");
else System.out.println(t);
}
}

public static void main(String[] args) {
Main solved = new Main();
solved.run();
}

// static InputStream inputStream = System.in;
// static InputReader cin = new InputReader(inputStream);

Scanner cin = new Scanner(new BufferedInputStream(System.in));

}

posted on 2013-02-18 10:02  Sure_Yi  阅读(198)  评论(0编辑  收藏  举报

导航