其他分享
首页 > 其他分享> > lightoj1306 细节+exgcd+待补

lightoj1306 细节+exgcd+待补

作者:互联网

题意很好解...

直接exgcd  求最小值,然后对于xa----xb区间......求一波x多少可以满足

然后y同理,然后取一波最小值...

就搞定了...

不知道为什么莫名RE

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

int T,cishu;
long long a,b,c,xa,ya,xb,yb,x,y,gcd,minlx,minly,maxlx,maxly,dx,dy,ans1,ans2;

long long exgcd(long long al,long long bl,long long &xl,long long &yl){
    if(bl==0){
        xl=1,yl=0;
        return al;
    }
    long long zz = exgcd(bl,al%bl,xl,yl);
    xl-=(al/bl)*yl;
    swap(xl,yl);
    return zz;
}

int main(){
    cin>>T;
    while(T--){
        cishu++;cout<<"Case "<<cishu<<": ";
        cin>>a>>b>>c>>xa>>xb>>ya>>yb;
        if(a==0&&b==0){
            if(c==0)cout<<(xb-xa+1)*(yb-ya+1)<<endl;
            else cout<<0<<endl;
            continue;
        }
        
        gcd = exgcd(a,b,x,y);
        if(abs(c)%abs(gcd)!=0){cout<<0<<endl;continue;}
        x=x*c/(gcd),y=y*c/(gcd),dx=abs(b/gcd),dy=abs(a/gcd);
        minlx = (x%dx+dx)%dx,minly = (y%dy+dy)%dy;
        ans1=((xb-xa)-minlx)/dx;
        ans2=((yb-ya)-minly)/dy;
        if(((xb-xa)-minlx)%dx==0)ans1++;
        if(((yb-ya)-minly)%dy==0)ans2++;
        cout<<min(ans1,ans2)<<endl;
    }
}
View Code

好像是正负号?

标签:yl,xl,待补,long,bl,exgcd,lightoj1306,xb
来源: https://www.cnblogs.com/shatianming/p/12347763.html