随笔 - 68  文章 - 57  trackbacks - 0
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(8)

随笔分类(74)

随笔档案(68)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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

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

public class Main
{
  
static public BigInteger x = null, y = null;
    
  
static BigInteger extended_gcd(BigInteger a, BigInteger b)
  
{
      BigInteger zero 
= new BigInteger(new String("0"));
      BigInteger ret, tmp;
      
      
if (b.compareTo(zero) == 0)
      
{
          x 
= new BigInteger(new String("1"));
          y 
= zero;
          
return a;
      }

      ret 
= extended_gcd(b, a.mod(b));
      tmp 
= x;
      x 
= y;
      y 
= tmp.subtract(a.divide(b).multiply(y));
      
      
return ret;
  }

  
  
static BigInteger modular_linear_equation(BigInteger a, BigInteger b, BigInteger n)
  
{
      BigInteger e, e2;
      BigInteger d 
= extended_gcd(a, n);
      e 
= b.divide(d).multiply(x).mod(n.divide(d));
      e2 
= e.subtract(n.divide(d)).abs();
      
      
if (e.compareTo(e2) < 0)    return e;
      
return e2;
  }

  
  
public static void main(String[] args)
  
{
      BigInteger a, b, c;
      Scanner in 
= new Scanner(System.in);
    
      
while (in.hasNext())
      
{
          a 
= in.nextBigInteger();
          b 
= in.nextBigInteger();
          c 
= in.nextBigInteger();
          c 
= c.multiply(new BigInteger(new String("-1")));
          System.out.println(modular_linear_equation(a, c, b));
      }

  }

}
posted on 2009-03-17 20:16 sdfond 阅读(146) 评论(0)  编辑 收藏 引用 所属分类: Algorithm - Number Theory

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理