• [扩展欧几里得]Draw a triangle 2022年桂林站E


    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)(x1,y1) and (x2,y2)(x2,y2). Now, he wanted to know the answer to the following question: where can he draw the third point (x3,y3)(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.

    Input

    The first line contains one integer TT (1≤T≤500001≤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, y2x1, y1, x2, y2 (−109≤x1, y1, x2, y2≤109−109≤x1, y1, x2, y2≤109) seperated by spaces, denoting two points are at (x1,y1)(x1,y1) and (x2,y2)(x2,y2), respectively.

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

    Output

    For each test case, print two integers x3, y3x3, y3 (−1018≤x3, y3≤1018−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.

    Example

    input

    3

    1 0 1 4

    0 1 0 9

    0 0 2 2

    output

    2 0
    1 1
    -1 0

    题意: 给出两点坐标,需要输出第三个点坐标,使这三个点构成一个面积最小的三角形,要求点坐标必须为整数。

    分析: 设已给出点A(x1, y1)和点B(x2, y2)的坐标,并且设向量AC为(x, y),则三角形面积为向量AB叉乘向量AC,将向量AB表示为(x2-x1, y2-y1),令a = x2-x1,b = y2-y1,则只需求出令a*y-b*x最小的整数解,而这个问题可以通过扩展欧几里得解决,根据扩展欧几里得可知a*y-b*x=c有整数解的条件是c = k*gcd(a, -b),也就是说该三角形面积只能是k*gcd(a, -b)中某一个取值,最小情况下自然是k = 1了,解出x和y后再加上A点坐标就是C点坐标了。

    具体代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #define int long long
    8. using namespace std;
    9. int exgcd(int a, int b, int &x, int &y) //扩展欧几里得
    10. {
    11. if(b == 0)
    12. {
    13. x = 1;
    14. y = 0;
    15. return a;
    16. }
    17. int eax = exgcd(b, a%b, x, y);
    18. int t = x;
    19. x = y;
    20. y = t - (a/b)*y;
    21. return eax;
    22. }
    23. signed main()
    24. {
    25. int T;
    26. cin >> T;
    27. while(T--){
    28. int x1, x2, y1, y2;
    29. scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
    30. int a = x2-x1, b = y2-y1;
    31. if(a == 0){
    32. printf("%lld %lld\n", x1+1, y1);
    33. continue;
    34. }
    35. if(b == 0){
    36. printf("%lld %lld\n", x1, y1+1);
    37. continue;
    38. }
    39. int x, y;
    40. exgcd(-b, a, x, y);
    41. printf("%lld %lld\n", x+x1, y+y1);
    42. }
    43. return 0;
    44. }

  • 相关阅读:
    第三讲 测量项目代码撰写1
    Day6、7 计算机网络——物理层
    冥想第四百九十三天
    人生规划(Flag)
    PyTorch ReLU6网络层
    扫雷游戏分析实现(完整代码)
    linux 开发板以太网通过Ubuntu上外网方法
    知识点2--CMS项目首页
    clickhouse 多维分析函数
    Nginx:防盗链原理和配置
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/127706671