# 洛谷P2421 [NOI2002]荒岛野人(扩展欧几里得)

## 输入输出样例

3
1 3 4
2 7 3
3 2 1

6

## 说明

$a_i+y_i * T_i \not \equiv a_j + y_j + T_j \pmod b$，$T$表示第几年。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 16, B = 31;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N;
struct Node {
int bg, step, life;
bool operator < (const Node &rhs) const {
return this -> step < rhs.step;
}
}a[MAXN];
int x, y;
int exgcd(int a, int b, int &x, int &y) {
if(b == 0) {
x = 1, y = 0; return a;
}
int r = exgcd(b, a % b, x, y);
int tmp = x; x = y, y = tmp - (a / b) * y;
return r;
}
bool check(int X) {
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= i - 1; j++) {
int B = X;
int A = a[i].step - a[j].step, C = a[j].bg - a[i].bg, r = __gcd(A, B);
if(C % r != 0) continue;
A = A / r; B = B / r; C = C / r;
exgcd(A, B, x, y);
x = (x * C) % B;
while(x < 0) x += B;
if(x <= a[i].life && x <= a[j].life) return 0;
}
}
return 1;
}
main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
int fuck = 0;
for(int i = 1; i <= N; i++)
fuck = max(fuck, a[i].bg);
sort(a + 1, a + N + 1);
for(int i = fuck; i <= 1e6; i++)//一定要从最大值开始，，好坑。。
if(check(i))
{printf("%d\n", i); exit(0);}
}

posted @ 2018-06-19 19:57  自为风月马前卒  阅读(...)  评论(...编辑  收藏

……