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) ...
随机推荐
- Application和Page详解
一.Application 1.该对象起始于服务器的启动,是ServletContext的实例. 2.可以获得tomcat的版本号等. 二.Page 是lang包的实例. 主要方法和lang一样,pa ...
- 【uoj222】 NOI2016—区间
http://uoj.ac/problem/222 (题目链接) 题意 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. Solution 线段树+滑动窗口.这道题很 ...
- codeforces 715B:Complete The Graph
Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...
- BZOJ3172 后缀数组
题意:求出一篇文章中每个单词的出现次数 对样例的解释: 原文是这样的: a aa aaa 注意每个单词后都会换行 所以a出现次数为6,aa为3 (aa中一次,aaa中两次),aaa为1 标准解法好像是 ...
- IOS基础之 (九) Foundation框架
一NSNumber 类型转换 NSNumber 把基本数据类型包装成一个对象类型.NSNumber之所以可以(只能)包装基本数据类型,是因为继承了NSValue. @20 等价于 [NSNumber ...
- MyEclipse------execute()使用方法
execute()方法应该仅在语句能返回多个ResultSet对象,多个更新计数或ResultSet对象与更新计数的组合时使用. testExecute.jsp <%@ page languag ...
- Laravel教程 四:数据库和Eloquent
Laravel教程 四:数据库和Eloquent 此文章为原创文章,未经同意,禁止转载. Eloquent Database 上一篇写了一些Laravel Blade的基本用法和给视图传递变量的几种方 ...
- 用Nikto探测一个网站所用到的技术
Nikto是一款开源的(GPL)网页服务器扫描器,它可以对网页服务器进行全面的多种扫描,包含超过3300种有潜在危险的文件/CGIs:超过 625种服务器版本:超过230种特定服务器问题,包括多种有潜 ...
- svn://59.46.115.123:13690/IOS/trunk/02.DevelopLib/04.Coding/uuridesharing
svn://59.46.115.123:13690/IOS/trunk/02.DevelopLib/04.Coding/uuridesharing
- Max批量导出工具
Max批量导出工具 http://www.paulneale.com/scripts/batchItMax/batchItMax.htm Scripts Batch It Max: Batch It ...