[lightoj P1306] Solutions to an Equation

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

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

Sample Output

Case 1: 3

Case 2: 37

Case 3: 1

Case 4: 2

Case 5: 1

毒瘤题。。。

首先肯定要用到exgcd。。都快忘了。

再推一下——

Ax0+By0=gcd(A,B)

=  Bx+(A%B)y=gcd(B,A%B)

=  Bx+(A-(A/B)*B)y

=  Ay+B(x-(A/B)y)

则 x0=y,y0=(x-(A/B)y)。

好,推好式子再回到题目。

为了省去一些复杂的分类讨论,我们把A,B都搞成非负整数,同事区间范围也要改动。

然后判断几个特殊情况:

A==0&&B==0——>ans=(rx-lx+1)*(ry-ly+1)*(C==0)

A==0——>ans=(rx-lx+1)*jug(C/B in [ly..ry])*(C%B==0)

B==0——>ans=(ry-ly+1)*jug(C/A in [lx..rx])*(C%A==0)

然后,就是一般情况。

我们知道,AB同号时,x增加时,y减少,x减少时y增加。

我们设在做exgcd的时候得到的一组解为X,Y。

那么,如果X<lx||Y>ry,那么,我们要把他们都移进合法区间内。然后得到最极端的解。然后算出另一边的极端解,然后处理一下细节。

如果X>rx||Y<ly,也差不多。

如果原来X,Y就都在合法范围内,我们可以先把某一个处理得不合法,再做上面的工作。

具体怎么算极端解,我真的没法讲清楚,细节非常多。。

还有这种题要尽量避免分类讨论。。

code:

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. using namespace std;
  4. LL A,B,C,lx,rx,ly,ry,X,Y,gcd,delx,dely;
  5. LL sx,sy,tx,ty,del,x[],y[],ans,kx,ky,k;
  6. LL exgcd(LL A,LL B,LL &x,LL &y) {
  7. ; y=; return A;}
  8. LL g=exgcd(B,A%B,x,y);
  9. LL x0=y,y0=x-y*(A/B);
  10. x=x0; y=y0; return g;
  11. }
  12. bool range_xy(LL x,LL y) {return x>=lx&&x<=rx&&y>=ly&&y<=ry;}
  13. int main() {
  14. int T; cin>>T;
  15. ; ts<=T; ts++) {
  16. scanf("%lld%lld%lld",&A,&B,&C);
  17. scanf("%lld%lld%lld%lld",&lx,&rx,&ly,&ry);
  18.  
  19. ) A=-A,lx=-lx,rx=-rx,swap(lx,rx);
  20. ) B=-B,ly=-ly,ry=-ry,swap(ly,ry);
  21. C=-C;
  22. &&B==) {
  23. printf()*(ry-ly+)*(C==)); continue;
  24. }
  25.  
  26. gcd=exgcd(A,B,X,Y);
  27. ) {printf(); continue;}
  28. X=X*C/gcd,Y=Y*C/gcd;
  29. delx=B/gcd,dely=A/gcd,ans=;
  30.  
  31. ) {
  32. ) ans=; )*range_xy(sx,ly);
  33. printf("Case %d: %lld\n",ts,ans); continue;
  34. }else
  35. ) {
  36. ) ans=; )*range_xy(lx,sy);
  37. printf("Case %d: %lld\n",ts,ans); continue;
  38. }
  39.  
  40. sx=X,sy=Y;
  41. if (sx>=lx) {
  42. k=(sx-lx)/delx+;
  43. sx=sx-k*delx,sy=sy+k*dely;
  44. }
  45. if (sx<lx||sy>ry) {
  46. ) kx=(lx-sx)/delx; ;
  47. ) ky=(sy-ry)/dely; ;
  48. k=max(kx,ky);
  49. sx+=delx*k,sy-=dely*k;
  50. ;
  51. else {
  52. kx=(rx-sx)/delx;
  53. ky=(sy-ly)/dely;
  54. k=min(kx,ky);
  55. tx=sx+k*delx,ty=sy-k*dely;
  56. ans=min((tx-sx)/delx+,(sy-ty)/dely+);
  57. }
  58. }else
  59. if (sx>rx||sy<ly) {
  60. ) kx=(sx-rx)/delx; ;
  61. ) ky=(ly-sy)/dely; ;
  62. k=max(kx,ky);
  63. sx-=delx*k,sy+=dely*k;
  64. ;
  65. else{
  66. kx=(sx-lx)/delx;
  67. ky=(ry-sy)/dely;
  68. k=min(kx,ky);
  69. tx=sx-k*delx,ty=sy+k*dely;
  70. ans=min((sx-tx)/delx+,(ty-sy)/dely+);
  71. }
  72. };
  73. printf("Case %d: %lld\n",ts,ans);
  74. }
  75. ;
  76. }

[lightoj P1306] 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. 1306 - Solutions to an Equation

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

  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. docker基本知识

  2. OpenMVG学习笔记

    OpenMVG 是一个不错的SFM开源算法库,可以实现基于多视图像的稀疏重建. OpenMVG SfM pipelines run as a 4 step process:包含四个基本过程 1. Im ...

  3. postman 做接口测试之学习笔记

    Postman 之前是作为Chrome 的一个插件,现在要下载应用才能使用. 以下是postman 的界面: 各个功能区的使用如下: 快捷区: 快捷区提供常用的操作入口,包括运行收藏夹的一组测试数据, ...

  4. 如何通过代码审计挖掘REDos漏洞

    写这篇文章的目的一是由于目前网上关于java代码审计的资料实在是太少了,本人作为一个java代码审计的新手,深知学习java代码审计的难受之处,所以将自己学习过程中挖掘的一些漏洞写成博客发出来希望可以 ...

  5. SQL SERVER 备份脚本

    DECLARE @FileName VARCHAR(200), @CurrentTime VARCHAR(50), @DBName VARCHAR(100), @SQL VARCHAR(1000)SE ...

  6. 利用js和JQuery定义一个导航条菜单

    利用js和JQuery定义一个导航条 效果: 一.html代码: <div class="Maintenance"> <div class="Title ...

  7. Shell脚本:while read line无法读取最后一行的问题

    [1]Shell脚本:while read line无法读取最后一行的问题 刚刚利用shell脚本处理日志文件时,发现了一个问题:while read line无法读取到最后一行 通过编辑器可以看到待 ...

  8. 使用java进行 AES 加密 解密?

    百度百科是这样定义的: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标 ...

  9. [转载]Oracle Golden Gate - 概念和机制 (ogg)

    出处:https://www.cnblogs.com/qiumingcheng/p/5435907.html Golden Gate(简称OGG)提供异构环境下交易数据的实时捕捉.变换.投递. OGG ...

  10. js 简单的进度条

    html部分 <div id='div1'> <div id="div2"></div> </div> css部分 div{ hei ...