1306 - Solutions to an Equation
Time Limit: 2 second(s) Memory Limit: 32 MB

You have to find the number of solutions of the following equation:

Ax + By + C = 0

Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-108, 108].

Output

For each case, print the case number and the total number of solutions.

Sample Input

Output for Sample Input

5

1 1 -5 -5 10 2 4

-10 -8 80 -100 100 -90 90

2 3 -4 1 7 0 8

-2 -3 6 -2 5 -10 5

1 8 -32 0 0 1 10

Case 1: 3

Case 2: 37

Case 3: 1

Case 4: 2

Case 5: 1


PROBLEM SETTER: JANE ALAM JAN
思路:扩展欧几里得;
这题再次让我重新认识了扩欧;其实这题只要分情况讨论下,用扩欧求出特解,然后二分找通解中
t 的范围然后看t重合的部分就行,有几个特殊情况讨论下,还有A,B系数的符号讨论下。
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<string.h>
  5. #include<queue>
  6. #include<stack>
  7. #include<set>
  8. #include<math.h>
  9. using namespace std;
  10. typedef long long LL;
  11. LL gcd(LL n,LL m)
  12. {
  13. if(m==0)
  14. {
  15. return n;
  16. }
  17. else if(n%m==0)
  18. {
  19. return m;
  20. }
  21. else return gcd(m,n%m);
  22. }
  23. pair<LL,LL>P(LL n,LL m)
  24. {
  25. if(m==0)
  26. {
  27. pair<LL,LL>ask=make_pair(1,0);
  28. return ask;
  29. }
  30. else
  31. {
  32. pair<LL,LL>an=P(m,n%m);
  33. LL x=an.second;
  34. LL y=an.first;
  35. y-=(n/m)*x;
  36. an.first=x;
  37. an.second=y;
  38. return an;
  39. }
  40. }
  41. int main(void)
  42. {
  43. LL i,j,k;
  44. scanf("%lld",&k);
  45. LL s;
  46. LL A,B,C,x1,x2,y1,y2;
  47. for(s=1; s<=k; s++)
  48. {
  49. LL sum=0;
  50. scanf("%lld %lld %lld %lld %lld %lld %lld",&A,&B,&C,&x1,&x2,&y1,&y2);
  51. C=-C;
  52. if(A==0&&B==0&&C!=0)
  53. sum=0;
  54. else if(A==0&&B==0&&C==0)
  55. {
  56. sum=(LL)(x2-x1+1)*(LL)(y2-y1+1);
  57. }
  58. else if(A==0)
  59. {
  60. if(C%B)
  61. {
  62. sum=0;
  63. }
  64. else
  65. {
  66. LL t=(C/B);
  67. if(t>=y1&&t<=y2)
  68. sum=(x2-x1+1);
  69. else sum=0;
  70. }
  71. }
  72. else if(B==0)
  73. {
  74. if(C%A)
  75. {
  76. sum=0;
  77. }
  78. else
  79. {
  80. LL t=(C/A);
  81. if(t>=x1&&t<=x2)
  82. sum=(y2-y1+1);
  83. else sum=0;
  84. }
  85. }
  86. else
  87. {
  88. if(A<0)
  89. {
  90. C=-C;
  91. A=-A;
  92. B=-B;
  93. }
  94. LL gc=gcd(abs(A),abs(B));
  95. if(C%gc)
  96. {
  97. sum=0;
  98. }
  99. else if((LL)A*(LL)B>0)
  100. {
  101. A/=gc;
  102. B/=gc;
  103. C/=gc;
  104. pair<LL,LL>ask=P((A),(B));
  105. LL x=(LL)ask.first;
  106. LL y=(LL)ask.second;
  107. x*=C;
  108. y*=C;
  109. LL l=-1e9;
  110. LL r=1e9;
  111. LL id=1e9;
  112. while(l<=r)
  113. {
  114. LL mid=(l+r)/2;
  115. if(x+mid*B>=x1)
  116. {
  117. id=mid;
  118. r=mid-1;
  119. }
  120. else l=mid+1;
  121. }
  122. l=-1e9;
  123. r=1e9;
  124. LL ic=1e9;
  125. while(l<=r)
  126. {
  127. LL mid=(l+r)/2;
  128. if(x+mid*B<=x2)
  129. {
  130. ic=mid;
  131. l=mid+1;
  132. }
  133. else r=mid-1;
  134. }
  135. if(id>ic)
  136. {
  137. sum=0;
  138. }
  139. else if(id==ic)
  140. {
  141. LL xx=x+id*B;
  142. if(xx>=x1&&xx<=x2)
  143. {
  144. LL yy=y-id*A;
  145. if(yy>=y1&&yy<=y2)
  146. {
  147. sum=1;
  148. }
  149. }
  150. else sum=0;
  151. }
  152. else
  153. {
  154. l=-1e9;
  155. r=1e9;
  156. LL ip=1e9,iq=1e9;
  157. while(l<=r)
  158. {
  159. LL mid=(l+r)/2;
  160. if(y-mid*A>=y1)
  161. {
  162. ip=mid;
  163. l=mid+1;
  164. }
  165. else r=mid-1;
  166. }
  167. l=-1e9;
  168. r=1e9;
  169. while(l<=r)
  170. {
  171. LL mid=(l+r)/2;
  172. if(y-mid*A<=y2)
  173. {
  174. iq=mid;
  175. r=mid-1;
  176. }
  177. else l=mid+1;
  178. }
  179. if(ip<iq)
  180. {
  181. sum=0;
  182. }
  183. else
  184. {
  185.  
  186. if(ic<iq||id>ip)
  187. {
  188. sum=0;
  189. }
  190. else
  191. {
  192. if(id<=iq&&ic>=ip)
  193. {
  194. sum=ip-iq+1;
  195. }
  196. else if(iq<=id&&ip>=ic)
  197. {
  198. sum=ic-id+1;
  199. }
  200. else if(iq>=id&&iq<=ic)
  201. {
  202. sum=ic-iq+1;
  203. }
  204. else if(id>=iq&&id<=ip)
  205. {
  206. sum=ip-id+1;
  207.  
  208. }
  209. }
  210. }
  211. }
  212. }
  213. else
  214. {
  215. A/=gc;
  216. B/=gc;
  217. C/=gc;
  218. pair<LL,LL>ask=P(abs(A),abs(B));
  219. LL x=(LL)ask.first;
  220. LL y=(LL)ask.second;
  221. y=-y;
  222. x*=C;
  223. y*=C;
  224. LL l=-1e9;
  225. LL r=1e9;
  226. LL id=1e9;
  227. while(l<=r)
  228. {
  229. LL mid=(l+r)/2;
  230. if(x+mid*abs(B)>=x1)
  231. {
  232. id=mid;
  233. r=mid-1;
  234. }
  235. else l=mid+1;
  236. }
  237. l=-1e9;
  238. r=1e9;
  239. LL ic=1e9;
  240. while(l<=r)
  241. {
  242. LL mid=(l+r)/2;
  243. if(x+mid*abs(B)<=x2)
  244. {
  245. ic=mid;
  246. l=mid+1;
  247. }
  248. else r=mid-1;
  249. }
  250. if(id>ic)
  251. {
  252. sum=0;
  253. }
  254. else if(id==ic)
  255. {
  256. LL xx=x+id*abs(B);
  257. if(xx>=x1&&xx<=x2)
  258. {
  259. LL yy=y+id*A;
  260. if(yy>=y1&&yy<=y2)
  261. {
  262. sum=1;
  263. }
  264. }
  265. else sum=0;
  266. }
  267. else
  268. {
  269. l=-1e9;
  270. r=1e9;
  271. LL ip=1e9,iq=1e9;
  272. while(l<=r)
  273. {
  274. LL mid=(l+r)/2;
  275. if(y+mid*A>=y1)
  276. {
  277. iq=mid;
  278. r=mid-1;
  279. }
  280. else l=mid+1;
  281. }
  282. l=-1e9;
  283. r=1e9;
  284. while(l<=r)
  285. {
  286. LL mid=(l+r)/2;
  287. if(y+mid*A<=y2)
  288. {
  289. ip=mid;
  290. l=mid+1;
  291. }
  292. else r=mid-1;
  293. }
  294. if(ip<iq)
  295. {
  296. sum=0;
  297. }
  298. else
  299. {
  300.  
  301. if(ic<iq||id>ip)
  302. {
  303. sum=0;
  304. }
  305. else
  306. {
  307. if(id<=iq&&ic>=ip)
  308. {
  309. sum=ip-iq+1;
  310. }
  311. else if(iq<=id&&ip>=ic)
  312. {
  313. sum=ic-id+1;
  314. }
  315. else if(iq>=id&&iq<=ic)
  316. {
  317. sum=ic-iq+1;
  318. }
  319. else if(id>=iq&&id<=ip)
  320. {
  321. sum=ip-id+1;
  322.  
  323. }
  324. }
  325. }//printf("%lld %lld %lld %lld\n",ip,iq,id,ic);
  326. }
  327. }
  328. }
  329. printf("Case %lld: %lld\n",s,sum);
  330.  
  331. }
  332. return 0;
  333. }

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

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

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

  2. LightOJ 1306 - Solutions to an Equation 裸EXGCD

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

  3. Solutions to an Equation LightOJ - 1306

    Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...

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

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

  5. [lightoj P1306] Solutions to an Equation

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

  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. 日常Java 2021/10/5

    java 异常处理 Throwable中包括Error 和Exception,Exception包括IOException和RuntimeException 抛出异常 1.异常运算条件 Arithme ...

  2. Scala和Java的List集合互相转换

    import java.util import scala.collection.mutable /** * 集合互相转换 */ object ScalaToJava { def main(args: ...

  3. 【leetcode】 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  4. ORACLE 获取执行计划的方法

    一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了): 1. explain plan for获取: 2. set autotrace on : 3. statistics_lev ...

  5. Give You My Best Wishes

    亲耐滴IT童鞋们: 感谢大家一直以来的支持,因为有你们的支持,才有我这么"拼"的动力!!爱你们哟 OC的学习已经告一段落,希望大家通过阅读这几篇浅薄的随笔,能够寻找到解决问题的方法 ...

  6. jmeter设置参数化

    设置参数化方法有3种 第一种: 1.打开 jmeter,导入badboy录制的脚本 导入后记得选择"step"右键选择change controller ->逻辑控制器-&g ...

  7. SpringBoot 整合 spring security oauth2 jwt完整示例 附源码

    废话不说直接进入主题(假设您已对spring security.oauth2.jwt技术的了解,不懂的自行搜索了解) 依赖版本 springboot 2.1.5.RELEASE spring-secu ...

  8. Dubbo服务限流

    为了防止某个消费者的QPS或是所有消费者的QPS总和突然飙升而导致的重要服务的失效,系统可以对访问流量进行控制,这种对集群的保护措施称为服务限流. Dubbo中能够实现服务限流的方式较多,可以划分为两 ...

  9. jQuery - focusin/focusout/focus/blur事件的区别与不同

    focus与blur事件:不支持冒泡 focusin与focusout:支持冒泡 事件触发顺序: 对于同时支持这4个事件的浏览器,事件执行顺序为focusin(聚焦) > focus > ...

  10. jstl中的if标签

    <%@ page import="java.util.ArrayList" %><%@ page import="java.util.List" ...