天天看點

求直線ax+by+c=0 上有多少個整點(x,y)滿足x∈[-100,100],y∈[-100,100]

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define DEBUG
const int maxn=1000+5,maxv=26,INF=0x3f3f3f3f,mod=100000000;
typedef int State[9];
const int maxstate=1000000,hashsize=1000003;
int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}
int exgcd(int a,int b,int& x,int& y){
    if(b==0){
        x=1;
        y=0;
        printf("%dx%d + %dx%d =%d\n",a,x,b,y,a*x+b*y);
        return a;
    }
    int ans=exgcd(b,a%b,x,y);
    int t=y;
    y=x-(a/b)*y;
    x=t;
    printf("%dx%d + %dx%d =%d\n",a,x,b,y,a*x+b*y);
    return ans;
}
int solve(int a,int b,int c,int x1,int x2,int y1,int y2){
    int x,y;
    int g=exgcd(a,b,x,y);
    if(c%g){
        return 0;
    }
    int ta=a/g;
    int tb=b/g;
    int t=c/g;
    for(int i=x,j=y;(i*t<=x2&&i*t>=x1&&j*t<=y2&&j*t>=y1);i+=tb,j-=ta){
        printf("------%dx%d + %dx%d + %d = 0\n",a,i*t,b,j*t,-c);
    }
    for(int i=x,j=y,k=1;(i*t<=x2&&i*t>=x1&&j*t<=y2&&j*t>=y1);i-=tb,j+=ta,k++){
        if(k==1)continue;
        printf("------%dx%d + %dx%d + %d = 0\n",a,i*t,b,j*t,-c);
    }
    return 1;
}
int main(){
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int a,b,c;
    cin>>a>>b>>c;
    // int a=exgcd(a,b,x,y);
    // printf("%d\n",a );
    solve(a,b,c,-100,100,-100,100);
#ifdef DEBUG
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}           

繼續閱讀