Today SGU 5.6

SGU 106

The meaning of the question: ask you how many <x,y>, satisfy ax+by+c=0, x1<=x<=x2, y1<=y<=y2

Receipt: Extended Euclid solves this equation, ax+by=1, gcd(a,b)=1

If gcd(a,b) is not equal to 1, then you directly pass the x and y obtained in the egcd function to the solution of a1x+b1y=1, a1=a/gcd(a,b), b1=b/ gcd(a,b)

Also note that y1, x0, y0 will conflict with the variables in the system

#include<bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl;
#define dd(x) cout<<#x<<"="<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define repd(i,a,b) for(int i=a;i>=(b);--i)
#define repp(i,a,b,t) for(int i=a;i<(b);i+=t)
#define ll long long
#define mt(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define mp(u,v) make_pair(u,v)
#define sz(a) (int)a.size()
#define ull unsigned long long
#define ll long long
#define pb push_back
#define PI acos(-1.0)
#define qc std::ios::sync_with_stdio(false)
#define db double
#define all(a) a.begin(),a.end()
const int mod = 1e9+7;
const int maxn = 1e5+5;
const double eps = 1e-6;
using namespace std;
bool eq(const db &a, const db &b) { return fabs(a - b) < eps; }
bool ls(const db &a, const db &b) { return a + eps < b; }
bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); }
ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); };
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//inv[1]=1;
//for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
void egcd(ll a,ll b,ll &d,ll &x,ll &y){
    if(!b) d=a ,x=1 ,y=0 ;
    else{
        egcd(b,a%b,d,y,x);
        y -=a/b* x;
    }
}
ll x,y,d;
ll a,b,c,x1,x2,yy1,yy2;
int main(){
     scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&x1,&x2,&yy1,&yy2);
    c=-c;
    if(c<0) a=-a,b=-b,c=-c;
    if(a<0) a=-a,swap(x1,x2),x1=-x1,x2=-x2;
    if(b<0) b=-b,swap(yy1,yy2),yy1=-yy1,yy2=-yy2;
    if(a==0||b==0){
        if(a==0&&b==0){
            if(c!=0) puts("0");
            else printf("%lld\n",(x2-x1+1)*(yy2-yy1+1));
        }else if(a==0){
            if(c%b==0&&c/b>=yy1&&c/b<=yy2) printf("%lld\n",x2-x1+1);
            else return puts("0"),0;
        }else {
            if(c%a==0&&c/a>=x1&&c/a<=x2) printf("%lld\n",yy2-yy1+1);
            else puts("0");
        }
        return 0;
    }
    egcd(a,b,d,x,y); // The solution here is a/d*x+b/d*y=1(d=gcd(a,b)), then multiply by c /gcd(a,b) will get a solution of a/d*x+b/d*y=c/d 
    if (c%d) return puts( " 0 " ), 0 ;
     double aa = a/d ,bb = b/ d;
    ll cc = c/d;
    x*=cc,y*=cc;
    ll r=min(floor((x2-x)/bb),floor((y-yy1)/aa)) ,l=max(ceil((x1-x)/bb),ceil((y-yy2)/aa));
    if(r>=l) printf("%lld\n",r-l+1);
    else puts("0");
    return 0;
}
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474148&siteId=291194637
5.6