http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=48388

前段时间偶然碰到的一道题,今天突然想到没把它记录下来。

比较不错的扩展欧几里德求解的应用

题意求是否满足ax+by=c gcd(a,b)==1 a>=0&&b>=0

数比较大 在求解时LL会溢出,用JAVA改写了一遍程序。

扩展欧几里德可以求出一组满足条件的解p= p1+b/gcd(a,b)*t q = q1+a/gcd(a,b)*t (t为任意整数)

因为上面的解有条件 所以根据p>=0 q>=0可以确定出来t的范围 在范围内枚举t可以求出解来 判断是否满足条件即可。

注意一下特殊情况,为0的情况。

 import java.text.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.applet.*;
public class Main
{
static BigInteger x,y,o = BigInteger.valueOf(0),o1 = BigInteger.valueOf(1);
static BigInteger gcd(BigInteger a,BigInteger b)
{
if(b.compareTo(BigInteger.valueOf(0))==0)
return a;
else
return gcd(b,a.mod(b));
}
static BigInteger exgcd(BigInteger a,BigInteger b)
{
if(b.compareTo(o)==0)
{
x = o1; y = o; return a;
}
BigInteger d = exgcd(b,a.mod(b));
BigInteger temp = x;
x = y;
y = temp.subtract(a.divide(b).multiply(y)) ;
return d;
}
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
BigInteger a,b,c;
while(cin.hasNext())
{
a = cin.nextBigInteger();
b = cin.nextBigInteger();
c = cin.nextBigInteger();
if(c.compareTo(a)<0&&c.compareTo(b)<0)
{
System.out.println("NO");
continue;
}
BigInteger o = BigInteger.valueOf(0);
if(a.compareTo(o)==0||b.compareTo(o)==0)
{
if(a.compareTo(o)==0&&b.compareTo(o)==0)
{
if(c.compareTo(o)==0)
System.out.println("YES");
else
System.out.println("NO");
}
else if(a.compareTo(o)==0)
{
if(c.mod(b).compareTo(o)==0)
System.out.println("YES");
else
System.out.println("NO");
}
else
{
if(c.mod(a).compareTo(o)==0)
System.out.println("YES");
else
System.out.println("NO");
}
continue;
}
if(c.compareTo(b)==0||c.compareTo(a)==0)
{
System.out.println("YES");
continue;
}
BigInteger t = gcd(a,b);
if(c.mod(t).compareTo(o)!=0)
{
System.out.println("NO");
continue;
}
exgcd(a,b);
x = x.multiply(c.divide(t));
y = y.multiply(c.divide(t));
BigInteger k1 = b.divide(t);
BigInteger k2 = a.divide(t);
int flag = 0;
BigInteger tx=x,ty=y;
BigInteger f1 = (x.multiply(BigInteger.valueOf(-1))).divide(k1).subtract(BigInteger.valueOf(1));
BigInteger f2 = y.divide(k2).add(BigInteger.valueOf(1));
BigInteger e = f1;
while(e.compareTo(f2)<=0)
{
tx = k1.multiply(e).add(x);
ty = k2.multiply(e);
ty = y.subtract(ty);
if(tx.compareTo(o1)>=0&&ty.compareTo(o1)>=0)
{
if(gcd(tx,ty).compareTo(BigInteger.valueOf(1))==0)
{
//System.out.println(tx+" "+ty);
flag = 1;
break;
}
}
e = e.add(BigInteger.valueOf(1));
}
if(flag==1)
System.out.println("YES");
else
System.out.println("NO");
}
}
}

最新文章

  1. MediatorPattern(中介者模式)
  2. 自定义 URL Scheme 完全指南
  3. POJ 2991–Crane【线段树+几何】
  4. Mysql错误:Ignoring query to other database解决方法
  5. 查看eclipse web项目中jsp编译后的servlet源文件【转】【JSP】
  6. IIS7.5开启GZip压缩
  7. Spring 配置文件详解 http://www.blogjava.net/hellxoul/archive/2011/11/19/364324.html
  8. Linux磁盘空间爆满,MySQL无法启动
  9. 从今天起,正式步入cnblogs,向曾经的脚印说声对不起!
  10. supervisor tornado 多进程多端口配置
  11. mapping 详解5(dynamic mapping)
  12. oc语言学习之基础知识点介绍(三):类方法、封装以及继承的介绍
  13. 有感,懂市场比懂产品重要,懂产品比懂技术重要——想起凡客诚品和YY语音了
  14. Linux cat命令详解
  15. jsp标签简介
  16. MariaDB:SSL配置
  17. Python+OpenCV图像处理(十五)—— 圆检测
  18. VS2015ASP.NET MVC5项目中Spring.NET配置方法(超详细)
  19. RabbitMQ None of the specified endpoints were reachable
  20. 单页面应用SPA和多页面应用MPA

热门文章

  1. 如何去除Office Excel的密码保护?
  2. React在Render中使用bind可能导致的问题
  3. jQuery常用插件大全(9)ResponsiveSlides插件
  4. 全排列 STL
  5. [Selenium] Selenium私房菜(新手入门教程)
  6. BZOJ_2726_[SDOI2012]任务安排_斜率优化+二分
  7. nginx开发_配置项
  8. 【NOIP2017Day1T1】 小凯的疑惑
  9. Cocos2d-X对常用Object-C特性的替换
  10. bzoj1304