SGU 106 The equation
Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
Description
There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer roots of this equation are satisfy to the following conditions : x1<=x<=x2, y1<=y<=y2. Integer root of this equation is a pair of integer numbers (x,y). |
Input
Input contains integer numbers a,b,c,x1,x2,y1,y2 delimited by spaces and line breaks. All numbers are not greater than 108 by absolute value。
Output
Write answer to the output.
Sample Input
1 1 -3
0 4
0 4
Sample Output
4
我的思路就是首先把一个基本解求出来,然后看在x1、x2的范围内x的范围是多少,然后找到对应的y的范围,再看y的范围有多少个解是在y1、y2范围之内的,这个就是最后的答案。
当然,对于含有a=0或b=0的情况要特判一下。
附上一个很不错的网址:传送门
#include <iostream>
using namespace std;
typedef long long LL;
LL a,b,c,x1,x2,y1,y2,x,y,tmp,ans=;
LL mini = -361168601842738790LL;
LL maxi = 322337203685477580LL;
int extendedGcd(int a,int b){
if (b==){
x=;y=;
return a;
}
else{
int tmp = extendedGcd(b,a%b);
int t = x;
x=y;
y=t-a/b*y;
return tmp;
}
}
LL extendedGcd(LL a,LL b){
if (b == ){
x=;y=;
return a;
}
else{
LL TEMP = extendedGcd(b,a%b);
LL tt=x;
x=y;
y=tt-a/b*y;
return TEMP;
}
}
LL upper(LL a,LL b){
if (a<=)
return a/b;
return (a-)/b + ;
}
LL lower(LL a,LL b){
if (a>=)
return a/b;
return (a+)/b - ;
}
void update(LL L,LL R,LL wa){
if (wa<){
L=-L;R=-R;wa=-wa;
swap(L,R);
}
mini=max(mini,upper(L,wa));
maxi=min(maxi,lower(R,wa));
}
int main(){
cin >> a >> b >> c >> x1 >> x2 >> y1 >> y2;c=-c;
if (a== && b==){
if (c==) ans = (x2-x1+) * (y2-y1+);
}
else if (a== && b!=){
if (c % b==) {
tmp = c/b;
if (tmp>=y1 && tmp<=y2) ans = ;
}
}
else if (a!= && b==){
if (c % a==){
tmp = c/a;
if (tmp>=x1 && tmp<=x2) ans = ;
}
}
else{
LL d = extendedGcd(a,b);
if (c%d == ){
LL p = c/d;
update(x1-p*x,x2-p*x,b/d);
update(y1-p*y,y2-p*y,-a/d);
ans = maxi-mini+;
if (ans<) ans=;
}
}
cout << ans << endl;
}
SGU 106 The equation的更多相关文章
- SGU 106 The equation 扩展欧几里德
106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...
- 数论 + 扩展欧几里得 - SGU 106. The equation
The equation Problem's Link Mean: 给你7个数,a,b,c,x1,x2,y1,y2.求满足a*x+b*y=-c的解x满足x1<=x<=x2,y满足y1< ...
- SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...
- SGU 106 The Equation 扩展欧几里得应用
Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...
- SGU 106 The equation【扩展欧几里得】
先放一张搞笑图.. 我一直wa2,这位不认识的大神一直wa9...这样搞笑的局面持续了一个晚上...最后各wa了10发才A... 题目链接: http://acm.hust.edu.cn/vjudge ...
- The equation SGU - 106
题目链接:https://codeforces.com/problemsets/acmsguru/problem/99999/106 这个题是关于EXGCD特别好的一个题目.题目大意:有一个等式ax+ ...
- The equation - SGU 106(扩展欧几里得)
题目大意:有一个二元一次方程,给出系数值和x与y的取值范围,求出来总共有多少对整数解. 分析:有以下几点情况. 1,系数a=0, b=0, 当c != 0的时候结果很明显是无解,当c=0的时候x,y可 ...
- 扩展欧几里德 SGU 106
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=106 题意:求ax + by + c = 0在[x1, x2], [y1, y2 ...
- SGU 106.Index of super-prime
时间限制:0.25s 空间限制:4M 题目大意: 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...
随机推荐
- BZOJ2654 tree
Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...
- HDU2888 Check Corners
Description Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numbers ( 1 ...
- 洛谷P1262 间谍网络
本来只想刷道小题,没想到还有点麻烦 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美 ...
- Linux Network IO Model、Socket IO Model - select、poll、epoll
目录 . 引言 . IO机制简介 . 阻塞式IO模型(blocking IO model) . 非阻塞式IO模型(noblocking IO model) . IO复用式IO模型(IO multipl ...
- SQL多条件查询
SELECT a.tel,a.business_code,b.name AS business_name,a.register_time FROM T_RED_USER a LEFT JOIN T_P ...
- eclipse中建python项目并运行
1. Help → Install New Software 2.Enter http://pydev.org/updates 3.点击Click "Next" and " ...
- java 内存分析
Java堆内存(heap memory)的十个要点: 1. Java堆内存是操作系统分配给JVM的内存的一部分. 2. 当我们创建对象时,它们存储在Java堆内存中. 3. 为了便于垃圾回收,Java ...
- MyEclipse------File类的各种方法
usingFile.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- Struts2拦截器Interceptor执行顺序理解
invocation.invoke()方法是拦截器框架的实现核心,通过确定invocation.invoke()方法执行位置,来实现Action执行前后处理操作,在invocation.invoke( ...
- Spring依赖注入:注解注入总结
更多11 spring 依赖注入 注解 java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...