The equation (扩展欧几里得)题解
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
思路:
又是一道很明显的exgcd题,这次做完,感觉对exgcd了解更加多了。
对a要进行符号判断,负号就要变为正号,相对应的区间x1,x2也要取对称区间;b同理。c变换a,b也要一起变换(二元一次方程)。其他的可以参考之前写过的题:循环狂魔 和 青蛙也要找女朋友
一道解二元一次方程的题,最后一点并集那里画个图应该就能解决了。中间还有一些判断要分布讨论。
又找到一个ceil()用来求向上取整(里面必须要double,和floor()一样,不然提交就会CE...)
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define INF 0x3f3f3f3f
#define ll long long
const int N=10005;
const ll MOD=998244353;
using namespace std;
ll ex_gcd(ll a,ll b,ll &x,ll &y){
ll d,t;
if(b==0){
x=1;
y=0;
return a;
}
d=ex_gcd(b,a%b,x,y);
t=x-a/b*y;
x=y;
y=t;
return d;
}
int main(){
ll a,b,c,x1,x2,y1,y2,x,y;
cin>>a>>b>>c>>x1>>x2>>y1>>y2;
c=-c;
if(c<0){
c=-c;
a=-a;
b=-b;
}
if(a<0){
a=-a;
swap(x1,x2);
x1=-x1;
x2=-x2;
}
if(b<0){
b=-b;
swap(y1,y2);
y1=-y1;
y2=-y2;
}
ll d=ex_gcd(a,b,x,y);
if(a==0 || b==0){ //ax+by=-c
if(a==0 && b==0){
if(c==0){
cout<<(x2-x1+1)*(y2-y1+1)<<endl;
return 0;
}
else{
cout<<0<<endl;
return 0;
}
}
else if(a==0){
if(c%b==0 && c/b>=y1 && c/b<=y2){
cout<<(x2-x1+1)<<endl;
return 0;
}
else{
cout<<0<<endl;
return 0;
}
}
else if(b==0){
if(c%a==0 && c/a>=x1 && c/a<=x2){
cout<<(y2-y1+1)<<endl;
return 0;
}
else{
cout<<0<<endl;
return 0;
}
}
}
x=x*c/d;
y=y*c/d;
ll k1=b/d,k2=a/d;
if(c%d!=0){
cout<<0<<endl;
return 0;
}
else{
ll r=min(floor((x2-x)*1.0/k1),floor((y-y1)*1.0/k2)) ,l=max(ceil((x1-x)*1.0/k1),ceil((y-y2)*1.0/k2));
if(r>=l){
cout<<r-l+1<<endl;
}
else{
cout<<0<<endl;
}
}
return 0;
}
The equation (扩展欧几里得)题解的更多相关文章
- 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 & ...
- 【数学】【NOIp2012】同余方程 题解 以及 关于扩展欧几里得与同余方程
什么是GCD? GCD是最大公约数的简称(当然理解为我们伟大的党也未尝不可).在开头,我们先下几个定义: ①a|b表示a能整除b(a是b的约数) ②a mod b表示a-[a/b]b([a/b]在Pa ...
- Codeforces7C 扩展欧几里得
Line Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- UVA 10673 扩展欧几里得
题意:给出x 和k,求解p和q使得等式x = p[x / k] + q [ x / k], 两个[x / k]分别为向下取整和向上取整 题解:扩展欧几里得 //meek///#include<b ...
- POJ2115 - C Looooops(扩展欧几里得)
题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...
- 【扩展欧几里得】NOIP2012同余方程
题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输出只有一行,包含一个正 ...
- hdu_1576A/B(扩展欧几里得求逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others) Me ...
- [P1516]青蛙的约会 (扩展欧几里得/中国剩余定理?)
每日做智推~ 一看就是一道数学题. 再看是一道公约数的题目. 标签是中国孙子定理. 题解是扩展欧几里得 (笑) 一开始没看数据范围 只有50分 开一个longlong就可以了 #include< ...
随机推荐
- 深入浅出REST(转载)
add by zhj: 参考http://zh.wikipedia.org/zh/REST 需要注意的是,REST是设计风格而不是标准,它也并没有与哪种协议绑定.不过,我们常按REST设计风格来使用H ...
- Andrew Ng-ML-第十七章-推荐系统
1.问题规划 图1.推荐系统在研究什么? 例子:预测电影的评分. 当知道n_u用户数,n_m电影数:r(i,j)用户j评价了电影i,那么就是1:y(i,j)如果r(i,j)为1,那么就给出评分. 问 ...
- PAT World Cup Betting[非常简单]
1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...
- [LeetCode] 832. Flipping an Image_Easy
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- pandas.drop/isnull/fillna/astype的用法
删除表中的某一行或者某一列更明智的方法是使用drop,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. (1)清理无效数据 df[df.isnull()] #返回的是个 ...
- Qt setMargin()和setSpacing() 的含义
mainLayout=newQVBoxLayout(this); mainLayout->setMargin(30); //表示控件与窗体的左右边距 mainLayout->setSpac ...
- OBV_X3
{OBV_X3[背景]考虑到OBV_X03在情况1的时候,采用的是寻找波段线的同价K线,但是由于此种情况下必须使用CONST(C)或通过输入参数CONSTCC设定固定值,无法当前K线的CLOSE同时变 ...
- c#中ref和out使用及区别
在c#中,使用方法获得返回值时,只能获取一个返回值.当使用ref和out关键字后,可以获取多个返回值. MSDN对ref和out关键字的说明如下: ref 关键字: 使参数按引用传递.其效果是,当控制 ...
- redis error It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING
应用redis出现如下错误 It was not possible to connect to the redis server(s); to create a disconnected multip ...
- NC_Verilog中的工具ICC
Cadence中的Incisive Comprehensive Coverage(ICC) solusion提供在仿真中的覆盖率分析. ICC中的覆盖率类型有两大类: 1)Code Coverage: ...