BZOJ1407 [Noi2002]Savage 【扩展欧几里得】
题目链接
题解
枚举\(m\)用扩欧判即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define REP(i,n) for (int i = 1; i <= (n); i++)
using namespace std;
const int maxn = 20,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int C[maxn],P[maxn],L[maxn],n;
inline void exgcd(int a,int b,int& d,int& x,int& y){
if (!b){d = a; x = 1; y = 0;}
else exgcd(b,a % b,d,y,x),y -= (a / b) * x;
}
inline bool ok(int u,int v,int m){
int a = P[u] - P[v],b = m,c = C[v] - C[u],d,x,y;
exgcd(a,b,d,x,y);
if (c % d) return true;
b = abs(b / d);
x = (x * c / d) % b;
if (x <= 0) x += b;
return x > min(L[u],L[v]);
}
inline bool check(int m){
for (register int i = 1; i < n; i++)
for (register int j = i + 1; j <= n; j++){
if (!ok(i,j,m)) return false;
}
return true;
}
int main(){
n = read(); int Max = 0;
bool tag = true;
REP(i,n){
C[i] = read(),P[i] = read(),L[i] = read(),Max = max(Max,C[i]);
C[i]--;
if (P[i] != 1) tag = false;
}
if (tag){printf("%d",Max); return 0;}
for (register int m = Max; ; m++){
if (check(m)) {printf("%d\n",m); return 0;}
}
return 0;
}