解方程大法好!
我们将题中 x ∗ x 1 + y ∗ y 1 + z ∗ z 1 = 0 x*x_1+y*y_1+z*z_1 = 0 x∗x1+y∗y1+z∗z1=0 称为 ① 式,
将 x ∗ x 2 + y ∗ y 2 + z ∗ z 2 = 0 x*x_2+y*y_2+z*z_2 = 0 x∗x2+y∗y2+z∗z2=0 称为 ② 式。
再将 ① 式乘上 x 2 x_2 x2,将 ② 式乘上 x 1 x_1 x1,得:
{ x ∗ x 1 ∗ x 2 + y ∗ y 1 ∗ x 2 + z ∗ z 1 ∗ x 2 = 0 x ∗ x 2 ∗ x 1 + y ∗ y 2 ∗ x 1 + z ∗ z 2 ∗ x 1 = 0 \left\{ \begin{aligned} x*x_1*x_2+y*y_1*x_2+z*z_1*x_2 = 0 \\ x*x_2*x_1+y*y_2*x_1+z*z_2*x_1 = 0 \\ \end{aligned} \right. {x∗x1∗x2+y∗y1∗x2+z∗z1∗x2=0x∗x2∗x1+y∗y2∗x1+z∗z2∗x1=0
将 ① 减去 ②,得:
y ∗ ( y 1 ∗ x 2 − x 1 ∗ y 2 ) + z ∗ ( z 1 ∗ x 2 − z 2 ∗ x 1 ) = 0 y*(y_1*x_2-x_1*y_2)+z*(z_1*x_2-z_2*x_1) = 0 y∗(y1∗x2−x1∗y2)+z∗(z1∗x2−z2∗x1)=0
发现, y = p ( y 1 ∗ x 2 − x 1 ∗ y 2 ) y=p(y_1*x_2-x_1*y_2) y=p(y1∗x2−x1∗y2) 且 z = q ( z 1 ∗ x 2 − z 2 ∗ x 1 ) z=q(z_1*x_2-z_2*x_1) z=q(z1∗x2−z2∗x1) 时,等式成立。
其中, p = 1 p=1 p=1 , q = 1 q=1 q=1就是一组解。
于是,这题就做完了。
注意:要特判 x 1 + x 2 = 0 x1+x2=0 x1+x2=0 的情况。
CODE:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define int long long
int x1,x2,y1,y2,z1,z2;
signed main(){
while(~scanf("%lld%lld%lld%lld%lld%lld",&x1,&y1,&z1,&x2,&y2,&z2)){
if(x1+x2==0){
int t1=y1+y2;
int t2=z1+z2;
int t3=__gcd(t1,t2);
t1/=t3;
t2/=t3;
int x0,y0,z0;
y0=t2;
z0=-t1;
x0=0;
printf("%lld %lld %lld\n",x0,y0,z0);
return 0;
}
int t1=x2*y1-x1*y2;
int t2=x2*z1-x1*z2;
int t3=__gcd(t1,t2);
if(t3==0){
printf("1 -1 0\n");
continue;
}
t1=t1/t3;
t2=t2/t3; //将两式化简
int x0=0,y0=t2,z0=-t1;
while((-y0*y1-y0*y2-z0*z1-z0*z2)%(x1+x2)!=0)
y0+=t2,z0-=t1;
x0=(-y0*y1-y0*y2-z0*z1-z0*z2)/(x1+x2); //求 x0
printf("%lld %lld %lld\n",x0,y0,z0);
}
return 0;
}