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 |
- #include<stdio.h>
- #include<algorithm>
- #include<iostream>
- #include<string.h>
- #include<queue>
- #include<stack>
- #include<set>
- #include<math.h>
- using namespace std;
- typedef long long LL;
- LL gcd(LL n,LL m)
- {
- if(m==0)
- {
- return n;
- }
- else if(n%m==0)
- {
- return m;
- }
- else return gcd(m,n%m);
- }
- pair<LL,LL>P(LL n,LL m)
- {
- if(m==0)
- {
- pair<LL,LL>ask=make_pair(1,0);
- return ask;
- }
- else
- {
- pair<LL,LL>an=P(m,n%m);
- LL x=an.second;
- LL y=an.first;
- y-=(n/m)*x;
- an.first=x;
- an.second=y;
- return an;
- }
- }
- int main(void)
- {
- LL i,j,k;
- scanf("%lld",&k);
- LL s;
- LL A,B,C,x1,x2,y1,y2;
- for(s=1; s<=k; s++)
- {
- LL sum=0;
- scanf("%lld %lld %lld %lld %lld %lld %lld",&A,&B,&C,&x1,&x2,&y1,&y2);
- C=-C;
- if(A==0&&B==0&&C!=0)
- sum=0;
- else if(A==0&&B==0&&C==0)
- {
- sum=(LL)(x2-x1+1)*(LL)(y2-y1+1);
- }
- else if(A==0)
- {
- if(C%B)
- {
- sum=0;
- }
- else
- {
- LL t=(C/B);
- if(t>=y1&&t<=y2)
- sum=(x2-x1+1);
- else sum=0;
- }
- }
- else if(B==0)
- {
- if(C%A)
- {
- sum=0;
- }
- else
- {
- LL t=(C/A);
- if(t>=x1&&t<=x2)
- sum=(y2-y1+1);
- else sum=0;
- }
- }
- else
- {
- if(A<0)
- {
- C=-C;
- A=-A;
- B=-B;
- }
- LL gc=gcd(abs(A),abs(B));
- if(C%gc)
- {
- sum=0;
- }
- else if((LL)A*(LL)B>0)
- {
- A/=gc;
- B/=gc;
- C/=gc;
- pair<LL,LL>ask=P((A),(B));
- LL x=(LL)ask.first;
- LL y=(LL)ask.second;
- x*=C;
- y*=C;
- LL l=-1e9;
- LL r=1e9;
- LL id=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(x+mid*B>=x1)
- {
- id=mid;
- r=mid-1;
- }
- else l=mid+1;
- }
- l=-1e9;
- r=1e9;
- LL ic=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(x+mid*B<=x2)
- {
- ic=mid;
- l=mid+1;
- }
- else r=mid-1;
- }
- if(id>ic)
- {
- sum=0;
- }
- else if(id==ic)
- {
- LL xx=x+id*B;
- if(xx>=x1&&xx<=x2)
- {
- LL yy=y-id*A;
- if(yy>=y1&&yy<=y2)
- {
- sum=1;
- }
- }
- else sum=0;
- }
- else
- {
- l=-1e9;
- r=1e9;
- LL ip=1e9,iq=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(y-mid*A>=y1)
- {
- ip=mid;
- l=mid+1;
- }
- else r=mid-1;
- }
- l=-1e9;
- r=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(y-mid*A<=y2)
- {
- iq=mid;
- r=mid-1;
- }
- else l=mid+1;
- }
- if(ip<iq)
- {
- sum=0;
- }
- else
- {
- if(ic<iq||id>ip)
- {
- sum=0;
- }
- else
- {
- if(id<=iq&&ic>=ip)
- {
- sum=ip-iq+1;
- }
- else if(iq<=id&&ip>=ic)
- {
- sum=ic-id+1;
- }
- else if(iq>=id&&iq<=ic)
- {
- sum=ic-iq+1;
- }
- else if(id>=iq&&id<=ip)
- {
- sum=ip-id+1;
- }
- }
- }
- }
- }
- else
- {
- A/=gc;
- B/=gc;
- C/=gc;
- pair<LL,LL>ask=P(abs(A),abs(B));
- LL x=(LL)ask.first;
- LL y=(LL)ask.second;
- y=-y;
- x*=C;
- y*=C;
- LL l=-1e9;
- LL r=1e9;
- LL id=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(x+mid*abs(B)>=x1)
- {
- id=mid;
- r=mid-1;
- }
- else l=mid+1;
- }
- l=-1e9;
- r=1e9;
- LL ic=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(x+mid*abs(B)<=x2)
- {
- ic=mid;
- l=mid+1;
- }
- else r=mid-1;
- }
- if(id>ic)
- {
- sum=0;
- }
- else if(id==ic)
- {
- LL xx=x+id*abs(B);
- if(xx>=x1&&xx<=x2)
- {
- LL yy=y+id*A;
- if(yy>=y1&&yy<=y2)
- {
- sum=1;
- }
- }
- else sum=0;
- }
- else
- {
- l=-1e9;
- r=1e9;
- LL ip=1e9,iq=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(y+mid*A>=y1)
- {
- iq=mid;
- r=mid-1;
- }
- else l=mid+1;
- }
- l=-1e9;
- r=1e9;
- while(l<=r)
- {
- LL mid=(l+r)/2;
- if(y+mid*A<=y2)
- {
- ip=mid;
- l=mid+1;
- }
- else r=mid-1;
- }
- if(ip<iq)
- {
- sum=0;
- }
- else
- {
- if(ic<iq||id>ip)
- {
- sum=0;
- }
- else
- {
- if(id<=iq&&ic>=ip)
- {
- sum=ip-iq+1;
- }
- else if(iq<=id&&ip>=ic)
- {
- sum=ic-id+1;
- }
- else if(iq>=id&&iq<=ic)
- {
- sum=ic-iq+1;
- }
- else if(id>=iq&&id<=ip)
- {
- sum=ip-id+1;
- }
- }
- }//printf("%lld %lld %lld %lld\n",ip,iq,id,ic);
- }
- }
- }
- printf("Case %lld: %lld\n",s,sum);
- }
- return 0;
- }
1306 - Solutions to an Equation的更多相关文章
- lightoj 1306 - Solutions to an Equation 扩展的欧几里得
思路:看题就知道用扩展的欧几里得算法做!!! 首先我们可以求出ax+by=gcd(a,b)=g的一个组解(x0,y0).而要使ax+by=c有解,必须有c%g==0. 继而可以得到ax+by=c的一个 ...
- LightOJ 1306 - Solutions to an Equation 裸EXGCD
本题是极其裸的EXGCD AX+BY+C=0 给你a b c 和x与y的区间范围,问你整数解有几组 作为EXGCD入门,题目比较简单 主要需要考虑区间范围的向上.向下取整,及正负符号的问题 问题是这正 ...
- Solutions to an Equation LightOJ - 1306
Solutions to an Equation LightOJ - 1306 一个基础的扩展欧几里得算法的应用. 解方程ax+by=c时,基本就是先记录下a和b的符号fla和flb(a为正则fla为 ...
- Jordan Lecture Note-6: The Solutions of Nonlinear Equation.
The Solutions of Nonlinear Equation 本文主要介绍几种用于解非线性方程$f(x)=0$的一些方法. (1) Bisection Method. 算法: step 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 ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- [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 ...
- [LeetCode] Solve the Equation 解方程
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- [Swift]LeetCode640. 求解方程 | Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
随机推荐
- 日常Java 2021/10/5
java 异常处理 Throwable中包括Error 和Exception,Exception包括IOException和RuntimeException 抛出异常 1.异常运算条件 Arithme ...
- Scala和Java的List集合互相转换
import java.util import scala.collection.mutable /** * 集合互相转换 */ object ScalaToJava { def main(args: ...
- 【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 ...
- ORACLE 获取执行计划的方法
一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了): 1. explain plan for获取: 2. set autotrace on : 3. statistics_lev ...
- Give You My Best Wishes
亲耐滴IT童鞋们: 感谢大家一直以来的支持,因为有你们的支持,才有我这么"拼"的动力!!爱你们哟 OC的学习已经告一段落,希望大家通过阅读这几篇浅薄的随笔,能够寻找到解决问题的方法 ...
- jmeter设置参数化
设置参数化方法有3种 第一种: 1.打开 jmeter,导入badboy录制的脚本 导入后记得选择"step"右键选择change controller ->逻辑控制器-&g ...
- SpringBoot 整合 spring security oauth2 jwt完整示例 附源码
废话不说直接进入主题(假设您已对spring security.oauth2.jwt技术的了解,不懂的自行搜索了解) 依赖版本 springboot 2.1.5.RELEASE spring-secu ...
- Dubbo服务限流
为了防止某个消费者的QPS或是所有消费者的QPS总和突然飙升而导致的重要服务的失效,系统可以对访问流量进行控制,这种对集群的保护措施称为服务限流. Dubbo中能够实现服务限流的方式较多,可以划分为两 ...
- jQuery - focusin/focusout/focus/blur事件的区别与不同
focus与blur事件:不支持冒泡 focusin与focusout:支持冒泡 事件触发顺序: 对于同时支持这4个事件的浏览器,事件执行顺序为focusin(聚焦) > focus > ...
- jstl中的if标签
<%@ page import="java.util.ArrayList" %><%@ page import="java.util.List" ...