Source: 2016-2017 ACM-ICPC Asia-Bangkok Regional Contest
Problem: 二维平面,给两个整数点A,B,找k个整数点C使得ABC是个非退化三角形,并且三角形的内部和边上都没有整数点。
Idea: 题目转化为找到离这条直线最近的有整数点的平行线,显然ax+by=gcd(a ,b)这条直线是离ax+by=0这条直线最近的直线,那么找k组解并平移一下即可。
Code:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define lson o<<1
#define rson o<<1|1
#define CLR(A, X) memset(A, X, sizeof(A))
#define bitcount(X) __builtin_popcountll(X)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9+7;
const auto INF = 0x3f3f3f3f;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }
const int MAXN = 1e5+5;
void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if(!b) { d = a; x = 1; y = 0; }
else { exgcd(b, a%b, d, y, x); y -= x*(a/b); }
}
int main() {
int X;
scanf("%d", &X);
while(X--) {
LL ax, ay, bx, by, x, y, g;
int k;
scanf("%lld%lld%lld%lld%d", &ax, &ay, &bx, &by, &k);
LL a = by-ay, b = ax-bx;
exgcd(a, b, g, x, y);
a /= g, b /= g;
while(k--) {
x += b;
y -= a;
printf("%lld %lld\n", ax+x, ay+y);
}
}
return 0;
}