sgu106.The equation 拓展欧几里得 难度:0
106. The equation
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
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 思路:
1 使用欧几里得构造出一组解使ax+by=gcd(a,b),然后(明显c%gcd!=0无解.)两边同乘以(c/gcd)
2 设k1=a/gcd,k2=b/gcd,(x,y)为原方程一组解,那么((x-n*k1),(y+n*k2))也是解(n为任意数)
3 于是不断寻找满足x1<=x<=x2,y1<=y<=y2的解,计数
4 这道题会爆int
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7ffffff;
long long tx1,tx2,ty1,ty2,a,b,c,tx,ty,minn,maxn;
void limit(long long L,long long R,long long d){//注意取区间端点
if(d<0){L=-L;R=-R;d=-d;swap(R,L);}
minn=max(minn,(long long)ceil((double)L/d));
maxn=min(maxn,(long long)floor((double)R/d));
}
long long extgcd(long long a,long long b,long long &x,long long &y){
long long d=a;
if(b!=0){
d=extgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else {
x=1;y=0;
}
return d;
}
int main(){
while(scanf("%I64d%I64d%I64d",&a,&b,&c)==3){
scanf("%I64d%I64d%I64d%I64d",&tx1,&tx2,&ty1,&ty2);
if(tx1>tx2||ty1>ty2){
puts("0");continue;
}
long long ans=0;
if(a==0&&b==0){
if(c==0)ans=(tx2-tx1+1)*(ty2-ty1+1);
}
else if(a==0&&b){
if(c%b==0&&(-c/b)>=ty1&&(-c/b)<=ty2){
ans=(tx2-tx1+1);
}
}
else if(b==0&&a){
if(c%a==0&&(-c/a)>=tx1&&(-c/a)<=tx2){
ans=(ty2-ty1+1);
}
}
else {
int d=extgcd(a,b,tx,ty);
if((-c)%d==0){
tx=-tx*c/d;
ty=-ty*c/d;
minn=-inf;maxn=inf;
limit(tx1-tx,tx2-tx,b/d);
limit(ty1-ty,ty2-ty,-a/d);
if(minn<=maxn)ans=maxn-minn+1;
}
}
printf("%I64d\n",ans);
}
return 0;
}
sgu106.The equation 拓展欧几里得 难度:0的更多相关文章
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
- bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- POJ1061 青蛙的约会-拓展欧几里得
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- BZOJ-2242 计算器 快速幂+拓展欧几里得+BSGS(数论三合一)
污污污污 2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2312 Solved: 917 [Submit][S ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- poj2891 拓展欧几里得
//Accepted 164 KB 16 ms //拓展欧几里得 //m=a1*x+b1 --(1) //m=a2*(-y)+b2 --(2) //->a1*x+a2*y=b2-b1 //由欧几 ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
随机推荐
- 2017.7.4 ACM校内赛 Round 2
这是一个向导 A - hdu 3652 B - bzoj 4152 C - bzoj 2429 D - bzoj 1087 E - bzoj 1566 F - bzoj 4043 G - bzoj 1 ...
- Python3基础 tuple 通过拆分元组向元组中加入新的元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Go第八篇之包的使用
Go 语言的源码复用建立在包(package)基础之上.Go 语言的入口 main() 函数所在的包(package)叫 main,main 包想要引用别的代码,必须同样以包的方式进行引用,本章内容将 ...
- Python的递归
递归 是指函数/过程/子程序在运行过程序中直接或间接调用自身而产生的重入现象.在计算机编程里,递归指的是一个过程:函数不断引用自身,直到引用的对象已知.使用递归解决问题,思路清晰,代码少.但是在主流高 ...
- goole机器学习视频链接【学习笔记】
作者:庄泽彬 说明:在youtu上观看的google的机器学习相关的视频,如何fangqiang请自己解决 机器学习简介:https://www.youtube.com/watch?time_cont ...
- MVC5 一套Action的登录控制流程
流程: 用拦截器控制每一个页面请求和ajax请求,根据请求体的cookie里面是否有token判断是否登录,还必须判断该token在redis里面的缓存是否存在 组成部分: 拦截器: using Sy ...
- 总结java中的super和this关键字
知识点: 在java类中使用super引用父类的成分,用this引用当前对象 this可以修饰属性.构造器.方法 super可以修饰属性.构造器.方法 关于子类实例化过程中的内存分配,在下一篇博客中说 ...
- Python time strptime()与time strftime()
time strftime()接收时间元组,返回表示时间的字符串. time strptime()把时间字符串,解析成一个时间元组. import time t = time.strftime('%Y ...
- 《EMCAScript6入门》读书笔记——24.编程风格
- python enumerate用法总结--转载
enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...