[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:

 #include<bits/stdc++.h>
 #define LL long long
 using namespace std;
 LL A,B,C,lx,rx,ly,ry,X,Y,gcd,delx,dely;
 LL sx,sy,tx,ty,del,x[],y[],ans,kx,ky,k;
 LL exgcd(LL A,LL B,LL &x,LL &y) {
     ; y=; return A;}
     LL g=exgcd(B,A%B,x,y);
     LL x0=y,y0=x-y*(A/B);
     x=x0; y=y0; return g;
 }
 bool range_xy(LL x,LL y) {return x>=lx&&x<=rx&&y>=ly&&y<=ry;}
 int main() {
     int T; cin>>T;
     ; ts<=T; ts++) {
         scanf("%lld%lld%lld",&A,&B,&C);
         scanf("%lld%lld%lld%lld",&lx,&rx,&ly,&ry);

         ) A=-A,lx=-lx,rx=-rx,swap(lx,rx);
         ) B=-B,ly=-ly,ry=-ry,swap(ly,ry);
         C=-C;
         &&B==) {
             printf()*(ry-ly+)*(C==)); continue;
         }

         gcd=exgcd(A,B,X,Y);
         ) {printf(); continue;}
         X=X*C/gcd,Y=Y*C/gcd;
         delx=B/gcd,dely=A/gcd,ans=;

         ) {
             ) ans=; )*range_xy(sx,ly);
             printf("Case %d: %lld\n",ts,ans); continue;
         }else
         ) {
             ) ans=; )*range_xy(lx,sy);
             printf("Case %d: %lld\n",ts,ans); continue;
         }

         sx=X,sy=Y;
         if (sx>=lx) {
             k=(sx-lx)/delx+;
             sx=sx-k*delx,sy=sy+k*dely;
         }
         if (sx<lx||sy>ry) {
             ) kx=(lx-sx)/delx; ;
             ) ky=(sy-ry)/dely; ;
             k=max(kx,ky);
             sx+=delx*k,sy-=dely*k;
             ;
             else {
                 kx=(rx-sx)/delx;
                 ky=(sy-ly)/dely;
                 k=min(kx,ky);
                 tx=sx+k*delx,ty=sy-k*dely;
                 ans=min((tx-sx)/delx+,(sy-ty)/dely+);
             }
         }else
         if (sx>rx||sy<ly) {
             ) kx=(sx-rx)/delx; ;
             ) ky=(ly-sy)/dely; ;
             k=max(kx,ky);
             sx-=delx*k,sy+=dely*k;
             ;
             else{
                 kx=(sx-lx)/delx;
                 ky=(ry-sy)/dely;
                 k=min(kx,ky);
                 tx=sx-k*delx,ty=sy+k*dely;
                 ans=min((sx-tx)/delx+,(ty-sy)/dely+);
             }
         };
         printf("Case %d: %lld\n",ts,ans);
     }
     ;
 }

[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. 基于usb4java的usb通讯

    下载java API及lib库地址:http://usb4java.org/index.html 1.导入所需要的库: 2.添加配置文件:文件名:javax.usb.properties:内容:jav ...

  2. char 类型的操作函数

    1.内存充填 void *memset(void *s,int ch,size_t n); 是由C Run-time Library提供的提供的函数,作用是在一段内存块中填充某个给定的值,它是对较大的 ...

  3. Python-python中数组和列表读取一列的方法

    转载自:https://blog.csdn.net/songyunli1111/article/details/78109976 在python中,普通的列表list和numpy中的数组array是不 ...

  4. 台式电脑、笔记本快捷选择启动项Boot 快捷键大全

    我们在安装系统时,会去设置电脑是从硬盘启动.U盘启动.光驱启动.网卡启动. 一般设置的方法有两种:一种是进BIOS主板菜单设置启动项顺序:另一种就是我在这里要介绍的快捷选择启动项. 以下是网友整理的各 ...

  5. Tomcat出现The origin server did not find a current representation for the target resourc...

    访问页面出现404 解决方法: https://blog.csdn.net/dbc_121/article/details/79204340 我的问题主要还是在tomcat调整上, 对了,关于loca ...

  6. angularjs 绑定多个属性到下拉框

    绑定下拉框 angularjs  代码: //活动下拉切换 $scope.activityChange = function () { var cards = new Array(); var url ...

  7. 利用tcp三次握手,使用awl伪装MAC地址进行多线程SYN洪水攻击

    SYN洪水攻击概述:SYN洪水攻击主要源于: tcp协议的三次握手机制tcp协议面向链接的协议SYN洪水攻击的过程:在服务端返回一个确认的SYN-ACK包的时候有个潜在的弊端,如果发起的客户是一个不存 ...

  8. day05 Python中的set集合

    集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 1.去重,把一个列表变成集合,就自动去重了. 2.关 ...

  9. NET的基本用法

    NET的基本用法 NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP | HELPMSG | LOCALGROUP ...

  10. 节约内存:Instagram的Redis实践(转)

    Instagram可以说是网拍App的始祖级应用,也是当前最火热的拍照App之一,Instagram的照片数量已经达到3亿,而在Instagram里,我们需要知道每一张照片的作者是谁,下面就是Inst ...