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< ...
随机推荐
- 使用 SendARP 获取 MAC 地址(使用SendARP API函数,很多相关文章)
ARP 协议地址解析协议(ARP)是通过解析网路层地址来找寻数据链路层地址的一个在网络协议包中极其重要的网络传输协议.ARP 最初在 1982 年的 RFC 826 中提出并纳入互联网标准 STD 3 ...
- swiper跳转制定页面
haha(){ var that=this; that.$refs.mySwiper.swiper.slideTo(1, 1000, false); } //以上代码是 获取ref值为myswipe ...
- html_parser
import json from lxml import etree class HtmlParser(object): """这是HtmlParser"&qu ...
- 【剑指offer】替换空格
一.题目: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 二.思路: Python代码,先 ...
- [vue]js模块导入导出export default
webstrom调试未授权问题解决 分es6语法和node语法 参考 参考 - export default s1 1.仅能出现1次default 2.导入时候可以随便命名 3,导出时候不必写{} - ...
- POJ3009:Curling 2.0(dfs)
http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...
- AngularJS2.0起步
ES6工具链 要让Angular2应用跑起来不是件轻松的事,因为它用了太多还不被当前主流浏览器支持的技术.所以,我们需要一个工具链:
- ubuntu vim python配置
参考https://www.cnblogs.com/cjy15639731813/p/5886158.html 但是后面打开文件的时候会报错,参考https://blog.csdn.net/jeff_ ...
- iOS 开发笔记-UILable/UIFont/UIButton常见设置
UILabel的常见设置 @property(nonatomic,copy) NSString *text; 显示的文字 @property(nonatomic,retain) UIFont *fon ...
- <<Joint Deep Modeling of Users and Items Using Reviews for Recommendation>> 评论打分预测
综述: 本文将 CNN 与 FM(Factorization Machine) 结合,基于评论文本来进行评分预测. 简介: 目前将神经网络应用推荐系统的研究工作中,有一类思路是把如CNN等神经网络作为 ...