SGU106 The equation[扩展欧几里德算法]

A - The equation
Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u
Submit  Status

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


题意:

求出 ax+by=-c中的解 x y  符合    x1<=x<=x2  y1<=y<=y2 的解的个数


自己真的挺难理解这个的..然后参考了两个博客

都写的很详细. 直接贴出来去看吧..我感觉自己没这个水平去讲这个

http://blog.csdn.net/volzkzg/article/details/7427233

http://www.cnblogs.com/Rinyo/archive/2012/11/25/2787419.html

主要用到了扩展欧几里德算法  变形一下, 这些博客里面都有说道



#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}
long long exgcd(long long a,long long b,long long &x,long long &y)
{
    if (!b)
    {
        x=1;
        y=0;
        return a;
    }
    long long gcd=exgcd(b,a%b,x,y);
    long long temp=x;
    x=y;
    y=temp-a/b*y;
    return gcd;
}
long long upper(long long a,long long b)//往上取整
{
    if (a<=0)
        return a/b;
    return (a-1)/b+1;
}
long long lower(long long a,long long b)//往下取整
{
    if (a>=0)
        return a/b;
    return (a+1)/b-1;
}
long long get(long long l,long long r,long long d,long long &k1,long long &k2)
{
    if (d<0)
    {
        l=-l;
        r=-r;
        d=-d;
        swap(l,r);
    }
    k1=upper(l,d);
    k2=lower(r,d);
}
int main()
{
    long long a,b,c,x1,y1,x2,y2;
    while (cin>>a>>b>>c)//ax+by+c=0 >>ax+by=-c
    {
        bool flag=0;
        c=-c;
        cin>>x1>>x2>>y1>>y2;
        long long ans,x,y;
        if (!a && !b && !c)
        {
            cout<<(x2-x1+1)*(y2-y1+1)<y2)
                ans=0;
            else
                ans=x2-x1+1;
            cout<x2)
                ans=0;
            else
                ans=y2-y1+1;
            cout<





你可能感兴趣的:(数论,数论---扩展欧几里德算法)