Solutions to an Equation LightOJ - 1306

一个基础的扩展欧几里得算法的应用。

解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为1,为负则fla为-1,flb相同),然后对a和b取绝对值。求出ax+by=gcd(a,b)的一组解x=ansx,y=ansy,那么只有当c是gcd(a,b)的倍数时原方程才可能有解。设g=gcd(a,b),通解是x=ansx*(c/g)*fla+k*b/g*fla,y=ansy*(c/g)*flb-k*a/g*flb。这里设xa=ansx*(c/g)*fla,xb=b/g*fla,ya=ansy*(c/g)*flb,yb=-(a/g*flb)。

那么这题就是根据通解的式子和x和y的范围去求k的范围,基本操作就是手算一下解不等式。

举例:不等式(x相关):xa+k*xb>=x1,xa+k*xb<=x2

(解一下就会发现第一个式子解出的是k的最小值还是最大值,与xb的符号有关)

理论上不难,但是符号之类的细节实现起来有难度(...)。另外,a为0或b为0或a、b都为0时都需要特判(...)。

错误记录:

未特判0


upd:

以上应该有错。通解应该是x=ansx*fla+k*(b/g)*fla,y=ansy*flb-k*(a/g)*flb

通解就是那个没错的,但是要注意ansx和ansy是ax+by=gcd(a,b)的解,不是原方程的解。

如果有了一组原方程的解,求通解,那么就不要乘c/g

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long LL;
  6. LL T,x,y,a,b,c,x1,x2,y11,y2,g;
  7. LL gcd(LL a,LL b)
  8. {
  9. LL t;
  10. while(b!=)
  11. {
  12. t=a;
  13. a=b;
  14. b=t%b;
  15. }
  16. return a;
  17. }
  18. LL exgcd(LL a,LL b,LL& x,LL& y)
  19. {
  20. if(b==)
  21. {
  22. x=;
  23. y=;
  24. return a;
  25. }
  26. else
  27. {
  28. LL t=exgcd(b,a%b,x,y);
  29. LL t1=x;
  30. x=y;
  31. y=t1-a/b*y;
  32. return t;
  33. }
  34. }
  35. int main()
  36. {
  37. LL TT,fla,flb,xa,xb,ya,yb,kl,kr,kl1,kr1;
  38. scanf("%lld",&T);
  39. for(TT=;TT<=T;TT++)
  40. {
  41. scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&x1,&x2,&y11,&y2);
  42. c=-c;
  43. if(a==&&b==)
  44. {
  45. if(c==)
  46. {
  47. printf("Case %lld: %lld\n",TT,max(0ll,(x2-x1+)*(y2-y11+)));
  48. }
  49. else
  50. {
  51. printf("Case %lld: %lld\n",TT,0ll);
  52. }
  53. continue;
  54. }
  55. if(a==)
  56. {
  57. if(c%b==&&(c/b>=y11)&&(c/b<=y2))
  58. printf("Case %lld: %lld\n",TT,x2-x1+);
  59. else
  60. printf("Case %lld: %lld\n",TT,0ll);
  61. continue;
  62. }
  63. if(b==)
  64. {
  65. if(c%a==&&(c/a>=x1)&&(c/a<=x2))
  66. printf("Case %lld: %lld\n",TT,y2-y11+);
  67. else
  68. printf("Case %lld: %lld\n",TT,0ll);
  69. continue;
  70. }
  71. fla=;flb=;
  72. if(a<)
  73. {
  74. a=-a;
  75. fla=-;
  76. }
  77. if(b<)
  78. {
  79. b=-b;
  80. flb=-;
  81. }
  82. g=gcd(a,b);
  83. if(c%g!=)
  84. {
  85. printf("Case %lld: %lld\n",TT,0ll);
  86. continue;
  87. }
  88. exgcd(a,b,x,y);
  89. xa=x*(c/g)*fla;
  90. xb=b/g*fla;
  91. ya=y*(c/g)*flb;
  92. yb=-(a/g*flb);
  93. //x=xa+k*xb,y=ya+k*yb
  94. if(xb>)
  95. {
  96. kl=ceil((double)(x1-xa)/xb);
  97. kr=floor((double)(x2-xa)/xb);
  98. }
  99. else
  100. {
  101. kr=floor((double)(x1-xa)/xb);
  102. kl=ceil((double)(x2-xa)/xb);
  103. }
  104. if(yb>)
  105. {
  106. kl1=ceil((double)(y11-ya)/yb);
  107. kr1=floor((double)(y2-ya)/yb);
  108. }
  109. else
  110. {
  111. kr1=floor((double)(y11-ya)/yb);
  112. kl1=ceil((double)(y2-ya)/yb);
  113. }
  114. //if(kl1>kr1) swap(kl1,kr1);
  115. printf("Case %lld: %lld\n",TT,max(min(kr1,kr)-max(kl1,kl)+,0ll));
  116. }
  117. return ;
  118. }

Solutions to an Equation LightOJ - 1306的更多相关文章

  1. [lightoj P1306] Solutions to an Equation

    [lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...

  2. 1306 - Solutions to an Equation

    1306 - Solutions to an Equation    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Lim ...

  3. Jordan Lecture Note-6: The Solutions of Nonlinear Equation.

    The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 1: ...

  4. lightoj 1306 - Solutions to an Equation 扩展的欧几里得

    思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...

  5. LightOJ 1306 - Solutions to an Equation 裸EXGCD

    本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...

  6. (light oj 1306) Solutions to an Equation 扩展欧几里得算法

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...

  7. [ACM_数学] Counting Solutions to an Integral Equation (x+2y+2z=n 组合种类)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27938#problem/E 题目大意:Given, n, count the numbe ...

  8. [LeetCode] Solve the Equation 解方程

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  9. [Swift]LeetCode640. 求解方程 | Solve the Equation

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

随机推荐

  1. mysql关闭skip-grant-tables快速重置mysql密码

    如果你忘记了mysql密码几乎是没有什么好办法可以直接修改密码了,但我们可以在my.ini把加上skip-grant-tables,然后重启mysql就不需要密码了,这时我们再修改root密码,最后再 ...

  2. SpringMVC -- 第一个简单的程序

    学习springMVC,我们来记录下第一个HelloWord的程序 首先.我们组织须要的jar包 commons-logging-1.1.3.jar spring-aop-4.1.7.RELEASE. ...

  3. liberOJ#6006. 「网络流 24 题」试题库 网络流, 输出方案

    #6006. 「网络流 24 题」试题库     题目描述 假设一个试题库中有 n nn 道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性.现要从题库中抽取 m mm 道题组成试卷.并要求 ...

  4. 把node加入master节点时,日志内容分析

    root@node1:~# kubeadm --token bggbum.mj3ogzhnm1wz07mj --discovery-token-ca-cert-hash sha256:8f02f833 ...

  5. JavaScript算法题(二) && 数组filter使用

    1.Let's implement the reject() function... 例: var odds = reject([1, 2, 3, 4, 5, 6], function(num){ r ...

  6. xunit inlinedata classdata memberdata

    https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdat ...

  7. CentOS7 安装jdk8

    1.下载jdk8 jdk-8u162-linux-x64.tar.gz 2.解压 tar -vxf jdk-8u162-linux-x64.tar.gz 3.进入 jdk1.8.0_162 文件夹 终 ...

  8. WAS:Thread "server.startup : 1" (00000020) and may be hung异常

    有现场server启动时,启动不了,后台报错如下: [// ::: CST] ThreadMonitor W WSVR0605W: Thread ) has been active milliseco ...

  9. Redhat 安装perl模块

    CPAN上下载要安装的模块 解压 gzip -d DBD-mysql-4.006.tar.gz tar xvf DBD-mysql-4.006.tar 然后进入DBD-mysql-4.006目录,执行 ...

  10. POJ1113:Wall (凸包:求最小的多边形,到所有点的距离大于大于L)

    Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the ...