lightoj 1306 - Solutions to an Equation

扩展欧几里得

通解:x+B/GCD*k,y+A/GCD*k

先缩小x1,x2范围,再缩y

#include<bits/stdc++.h>
using namespace std;

void extgcd(long long a,long long b,long long &d,long long &x,long long &y)//return d=gcd(a,b)(==a*x+b*y)
{
    if(!b)
        d=a,x=1,y=0;
    else
    {
        extgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
int main(void)
{
    //freopen("in","r",stdin);
    int t,Case=0;
    long long A,B,C,x1,x2,y1,y2;
    long long GCD,x,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld%lld%lld%lld%lld",&A,&B,&C,&x1,&x2,&y1,&y2);
        printf("Case %d: ",++Case);
        if(A==0&&B==0)
        {
            if(C==0)
                printf("%lld\n",(x2-x1+1)*(y2-y1+1));
            else
                printf("0\n");
            continue;
        }
        if(A==0)
        {
            if(C%B!=0)
            {
                printf("0\n");
                continue;
            }
            y=-C/B;
            if(y1<=y&&y<=y2)
                printf("%lld\n",x2-x1+1);
            else
                printf("0\n");
            continue;
        }
        if(B==0)
        {
            if(C%A!=0)
            {
                printf("0\n");
                continue;
            }
            x=-C/A;
            if(x1<=x&&x<=x2)
                printf("%lld\n",y2-y1+1);
            else
                printf("0\n");
            continue;
        }
        extgcd(A,B,GCD,x,y);
        x*=-C/GCD;
        y*=-C/GCD;
        if(C%GCD!=0)
        {
            printf("0\n");
            continue;
        }
        long long base1=B/GCD;
        long long base2=A/GCD;
        long long l,r;
        x1=((x-x1)%base1+abs(base1))%abs(base1)+x1;
        x2=((x-x2)%base1-abs(base1))%abs(base1)+x2;
        if(x1>x2)
        {
            printf("0\n");
            continue;
        }
        l=(-base2)*((x1-x)/base1)+y;
        r=(-base2)*((x2-x)/base1)+y;
        if(l>r)
            swap(l,r);
        y1=max(y1,l);
        y2=min(y2,r);
        y1=((y-y1)%base2+abs(base2))%abs(base2)+y1;
        y2=((y-y2)%base2-abs(base2))%abs(base2)+y2;
        if(y1>y2)
            printf("0\n");
        else
            printf("%lld\n",(y2-y1)/abs(base2)+1);
    }
    return 0;
}
时间: 2024-07-30 20:27:18

lightoj 1306 - Solutions to an Equation的相关文章

1306 - Solutions to an Equation

   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You have to find the number of solutions of the following equation: Ax + By + C = 0 Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. Input Input starts wit

[LeetCode] Solve the Equation

Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and its coefficient. If there is no solution for the equation, return "No solution". If th

[LeetCode] Solve the Equation 解方程

Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and its coefficient. If there is no solution for the equation, return "No solution". If th

[leetcode-640-Solve the Equation]

Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable xand its coefficient. If there is no solution for the equation, return "No solution". If the

[Mathematics][MIT 18.03] Proof of a Theory about the Solution to Second-order Linear Homogeneous Differential Equation

Edit: I suddenly found that this theorem is called Abel Identity. And the proof here is exactly the same as what he did. At first, I'd like to say thank you to MIT open courses which give me the privilege to enjoy the most outstanding education resou

hdu 2578 Dating with girls(1)

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2578 Dating with girls(1) Description Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with

hdu3699 A hard Aoshu Problem 暴搜

http://acm.hdu.edu.cn/showproblem.php?pid=3699 A hard Aoshu Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 968    Accepted Submission(s): 490 Problem Description Math Olympiad is calle

HDU 2578 Dating with girls(1) [补7-26]

Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3869    Accepted Submission(s): 1196 Problem Description Everyone in the HDU knows that the number of boys is larger than the

The Circle Method

The Circle Method By Andrew Granville A method of analysis that plays a prominent role in Analytic Number Theory is the so-called circle method, which goes    back to Hardy and Littlewood. This method uses the fact that, for any integer $n$, \[\int_{