2022 China Collegiate Programming Contest (CCPC) Guilin Site - E. Draw a triangle Exgcd

E. Draw a triangle

time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output

题目描述

Little Desprado2 is a student of Springfield Flowers Kindergarten. On this day, he had just learned how to draw triangles on grid coordinate paper. However, he soon found it very dull, so he came up with a more interesting question:

He had drawn two integral points of the triangle on the grid paper, and he denotes them (x1,y1) and (x2,y2). Now, he wanted to know the answer to the following question: where can he draw the third point (x3,y3) so that the area of the triangle is positive but minimized?

Obviously, he can’t solve this problem because he is too young and simple. Can you tell him the answer?

Please note that your answer’s coordinates must consist of integers because he is drawing on grid paper, and the triangle shouldn’t be a degenerated triangle to keep the area positive.

输入描述

The first line contains one integer T (1≤T≤50000), denoting the number of Little Desprado2’s queries.

For each test case, there’s a single line contains four integers x1, y1, x2, y2 (−109≤x1, y1, x2, y2≤109) seperated by spaces, denoting two points are at (x1,y1) and (x2,y2), respectively.

It is guaranteed that the two points won’t coincide.

输出描述

For each test case, print two integers x3, y3 (−1018≤x3, y3≤1018) in a separated line, denoting your answer.

If there are multiple answers, you can print any one of them. It is guaranteed that there exists a solution in the above range.

题意

给定两个点 求第三个点使得三个点构成一个面积大于 0 的面积最小的三角形

思路

设第三个点的坐标为 (xc,yc) 使用叉积公式表示出三角形面积 S=abs((yc-ya)(xb-xa)-(xc-xa)(yb-ya))/2;
可以发现将 (xc-xa) 含未知参数 xa 的这一项看作 x
将 (yc-ya) 含未知参数 ya 的这一项看作 y

即可发现这个式子的形式满足 ax+by=c
通过 exgcd 可以解出一组解 且 gcd(a,b)|c
即面积最小值为 gcd(a,b)/2
而 exgcd(a,b) 需要 a,b 为正整数 故我们需要注意提取负号使得 a,b>0

Code

#include<bits/stdc++.h>
using namespace std;
#define __T int csT;scanf("%d",&csT);while(csT--)
const int mod=1e9+7;
const int maxn=2e5+3;

int xa,ya,xb,yb,xc,yc;

void exgcd(int a,int b)
{
    if(b==0)
    {
        xc=1;
        yc=0;
        return;
    }
    exgcd(b,a%b);
    int t=xc;
    xc=yc;
    yc=t-a/b*yc;
}
//ax+by=gcd(a,b)
//x=x+sdx  dx=b/gcd(a,b)
//y=y-sdy  dy=a/gcd(a,b)
inline void sol()
{
    scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
    int ga=yb-ya,gb=-(xb-xa);
    int fa=1,fb=1;
    if(ga<0)
    {
        ga=-ga;
        fa=-1;
    }
    if(gb<0)
    {
        gb=-gb;
        fb=-1;
    }
    exgcd(ga,gb);
    xc*=fa;
    yc*=fb;
    xc+=xa;
    yc+=ya;
    printf("%d %d\n",xc,yc);
}

int main()
{
    __T
    sol();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柯西可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值