CodeForces Gym101161D Find C

本文针对ACM-ICPC亚洲区域赛中的一道几何问题进行解答,通过寻找特定条件下的整数点来构造非退化的三角形,并提供了一个有效的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值