题解 P8854 【[POI2002] 超级马】
显然,只要我们通过这些向量拼出
上述两类向量本质相同,现在考虑拼出
但是直接考虑一堆向量拼起来又太麻烦,因此考虑通过若干个向量两两之间拼出若干
下面讨论两种情况:
- 本来就是
(0, x) 的形式
直接加入集合即可。
- 本来是由
(p_i, q_i), (p_j, q_j) 拼起来
设向量
由于
于是我们可以解出一组绝对值最小的解:
注意
代入
swap 一下
代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int p[107], q[107], absp[107];
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
inline bool solve(int n){
int d = 0;
bool negative = false, positive = false;
for (int i = 1; i <= n; i++){
absp[i] = abs(p[i]);
if (p[i] == 0){
d = gcd(d, abs(q[i]));
if (q[i] < 0){
negative = true;
} else if (q[i] > 0){
positive = true;
}
}
}
for (int i = 1; i < n; i++){
if (p[i] != 0){
for (int j = i + 1; j <= n; j++){
if (p[j] != 0){
int t = (p[i] * q[j] - p[j] * q[i]) / gcd(absp[i], absp[j]);
d = gcd(d, abs(t));
if ((p[i] > 0) != (p[j] > 0)){
if (p[i] < 0) t = -t;
if (t < 0){
negative = true;
} else if (t > 0){
positive = true;
}
}
}
}
}
}
return negative && positive && d == 1;
}
int main(){
int t;
cin >> t;
for (int i = 1; i <= t; i++){
int n;
cin >> n;
for (int j = 1; j <= n; j++){
cin >> p[j] >> q[j];
}
if (!solve(n)){
cout << "NIE" << endl;
continue;
}
swap(p, q);
if (!solve(n)){
cout << "NIE" << endl;
} else {
cout << "TAK" << endl;
}
}
return 0;
}