也是很赤裸裸的模型,这里求的是绝对值最小解,还有就是用到高精。我用java不会写传引用,因此只好开了全局变量。
 import java.math.*;
import java.math.*;
 import java.util.*;
import java.util.*;

 public class Main
public class Main


 {
{
 static public BigInteger x = null, y = null;
  static public BigInteger x = null, y = null;
 
    
 static BigInteger extended_gcd(BigInteger a, BigInteger b)
  static BigInteger extended_gcd(BigInteger a, BigInteger b)

 
   {
{
 BigInteger zero = new BigInteger(new String("0"));
      BigInteger zero = new BigInteger(new String("0"));
 BigInteger ret, tmp;
      BigInteger ret, tmp;
 
      
 if (b.compareTo(zero) == 0)
      if (b.compareTo(zero) == 0)

 
       {
{
 x = new BigInteger(new String("1"));
          x = new BigInteger(new String("1"));
 y = zero;
          y = zero;
 return a;
          return a;
 }
      }
 ret = extended_gcd(b, a.mod(b));
      ret = extended_gcd(b, a.mod(b));
 tmp = x;
      tmp = x;
 x = y;
      x = y;
 y = tmp.subtract(a.divide(b).multiply(y));
      y = tmp.subtract(a.divide(b).multiply(y));
 
      
 return ret;
      return ret;
 }
  }
 
  
 static BigInteger modular_linear_equation(BigInteger a, BigInteger b, BigInteger n)
  static BigInteger modular_linear_equation(BigInteger a, BigInteger b, BigInteger n)

 
   {
{
 BigInteger e, e2;
      BigInteger e, e2;
 BigInteger d = extended_gcd(a, n);
      BigInteger d = extended_gcd(a, n);
 e = b.divide(d).multiply(x).mod(n.divide(d));
      e = b.divide(d).multiply(x).mod(n.divide(d));
 e2 = e.subtract(n.divide(d)).abs();
      e2 = e.subtract(n.divide(d)).abs();
 
      
 if (e.compareTo(e2) < 0)    return e;
      if (e.compareTo(e2) < 0)    return e;
 return e2;
      return e2;
 }
  }
 
  
 public static void main(String[] args)
  public static void main(String[] args)

 
   {
{
 BigInteger a, b, c;
      BigInteger a, b, c;
 Scanner in = new Scanner(System.in);
      Scanner in = new Scanner(System.in);
 
    
 while (in.hasNext())
      while (in.hasNext())

 
       {
{
 a = in.nextBigInteger();
          a = in.nextBigInteger();
 b = in.nextBigInteger();
          b = in.nextBigInteger();
 c = in.nextBigInteger();
          c = in.nextBigInteger();
 c = c.multiply(new BigInteger(new String("-1")));
          c = c.multiply(new BigInteger(new String("-1")));
 System.out.println(modular_linear_equation(a, c, b));
          System.out.println(modular_linear_equation(a, c, b));
 }
      }
 }
  }
 }
}posted on 2009-03-17 20:16 
sdfond 阅读(187) 
评论(0)  编辑 收藏 引用  所属分类: 
Algorithm - Number Theory