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的更多相关文章

  1. NOIP2012拓展欧几里得

    拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...

  2. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

  3. bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得

    这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...

  4. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  5. POJ1061 青蛙的约会-拓展欧几里得

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

  6. BZOJ-2242 计算器 快速幂+拓展欧几里得+BSGS(数论三合一)

    污污污污 2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2312 Solved: 917 [Submit][S ...

  7. BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)

    zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...

  8. poj2891 拓展欧几里得

    //Accepted 164 KB 16 ms //拓展欧几里得 //m=a1*x+b1 --(1) //m=a2*(-y)+b2 --(2) //->a1*x+a2*y=b2-b1 //由欧几 ...

  9. [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)

    Power of Fibonacci Time Limit: 5 Seconds      Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...

随机推荐

  1. UVa 1451 Average - 斜率优化

    A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the numbe ...

  2. 安装VS提示系统找不到指定路径

    解决办法:删除C:\ProgramData\Package Cache快捷方式

  3. github客户端上传代码

    在window下安装github客户端上传代码 第一步:创建Github新账户 第二步:新建仓库 第三步:安装Github shell程序,地址:http://windows.github.com/ ...

  4. JQuery插件模板

    (function($){ $.fn.插件名 = function(settings){ var defaultSettings = { } /* 合并默认参数和用户自定义参数 */settings ...

  5. Python3基础 str format 输出花括号{}

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. JSON类库Jackson与JSON-lib性能对比[转]

    Jackson:http://jackson.codehaus.org/ JSON-lib:http://json-lib.sourceforge.net/ Gson:http://code.goog ...

  7. [shiro] - 加入rememberMe功能

    shiro不加入rememberMe没事,一加入就出错. RememberMeAuthenticationToken : public interface RememberMeAuthenticati ...

  8. [Shiro] - Shiro之SpringBoot中的使用

    下载了运行项目后,访问路径:http://localhost/shiro/login 这篇应该在进阶后面的. shiro中的重中之重,一定要看. 基于springboot+thymeleaf+shir ...

  9. C#学习笔记(十八):数据结构和泛型

    数据结构 只有这四种 a.集合:数据之间没有特定的关系 b.线性结构:数据之间有一对一的前后联系 c.树形结构:数据之间有一对多的关系,一个父节点有多个子节点,一个子节点只能有一个父节点 d.图状结构 ...

  10. python 元组列表合并

    #create a tuple l = [(,), (,), (,)] print(list(zip(*l)))