1 条题解

  • 0
    @ 2026-4-19 0:09:04

    因为LATEX挂了,所以重新交一遍

    分析

    我们设原向量为(a1,b1)(a_1, b_1),(a2,b2)(a_2, b_2)。 设新向量为(a3,b3)(a_3, b_3)(0,a4)(0, a_4)

    \therefore a3=gcd(a1,a2)a_3=gcd(a_1,a_2)

    \therefore a1x+a2y=a3a_1x + a_2y = a_3

    \therefore a1x+a2y=gcd(a1,a2)a_1x + a_2y = gcd(a_1,a_2) b1x+b2y=b3b_1x + b_2y = b_3

    $\begin{cases}a_1x + a_2y = 0\\b_1x + b_2y = b_4\end{cases}$

    \therefore y=a1xa2-y = \dfrac{a_1x}{a_2}

    \because y-y是整数

    \therefore a2a1xa_2|a_1x

    即$\dfrac{a_2}{gcd(a_1,a_2)}|\dfrac{a_1}{gcd(a_1,a_2)x}$

    \therefore $\dfrac{a_2}{gcd(a_1,a_2)}|\dfrac{a_1}{gcd(a_1,a_2)x}$互质

    \thereforea2gcd(a1,a2)x\dfrac{a_2}{gcd(a_1,a_2)}|x

    x=a2gcd(a1,a2)x=\dfrac{a_2}{gcd(a_1,a_2)}

    \therefore a1gcd(a1,a2)-\dfrac{a_1}{gcd(a_1,a_2)}

    \therefore b4=b1a2b2a1gcd(a1,a2)b_4 = \dfrac{|b_1a_2-b_2a_1|}{gcd(a_1,a_2)}

    我们只需要不断的将向量转变到y轴上使得最终至多一个向量不再y轴上就行了。

    代码:

    #include <cstdio>
    int a[510], b[510];
    template<class type>type _gcd(type __, type ___) {
    	return (!___) ? __ : _gcd(___, __ % ___);
    }
    template<class type>type _abs(type __) {
    	return __ < 0 ? -__ : __;
    }
    inline void exgcd(int a, int b, int &x, int &y) {
    	if (b == 0) {
    		x = 1;
    		y = 0;
    		return;
    	}
    	exgcd(b, a%b, x, y);
    	int z = x;
    	x = y;
    	y = z - (a / b)*y;
    }
    int main() {
    	int n, m, x, y;
    	scanf("%d", &n);
    	for (int i = 1; i <= n; i++)
    		scanf("%d%d", &a[i], &b[i]);
    	int a1, b1, a2, b2;
    	if (!a[1]) { a1 = a[2]; b1 = b[2]; b2 = b[1]; }
    	else if (!a[2]) { a1 = a[1]; b1 = b[1]; b2 = b[2]; }
    	else { a1 = _gcd(a[1], a[2]); exgcd(a[1] / a1, a[2] / a1, x, y); b1 = b[1] * x + b[2] * y; b2 = _abs(b[1] * a[2] - b[2] * a[1]) / a1; }
    	for (int i = 3; i <= n; i++) {
    		if (!a1) { a1 = a[i]; b2 = _gcd(b2, b1); b1 = b[i]; }
    		else if (!a[i])b2 = _gcd(b2, b[i]);
    		else { int be = a1, be2 = b1; a1 = _gcd(a1, a[i]); exgcd(be / a1, a[i] / a1, x, y); b1 = b1 * x + b[i] * y; b2 = _gcd(b2, _abs(be2*a[i] - b[i] * be) / a1); }
    	}
    	printf("%d %d\n", a1, b1);
    	putchar('0'), putchar(' ');
    	printf("%d\n", b2);
    	return 0;
    }
    

    码风较丑,见谅

    • 1

    信息

    ID
    3185
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    4
    已通过
    1
    上传者