这题的时间复杂度真玄学。。。 O(m*n^2)。1e8也能过啊。。。

首先题目保证m<=1e6. 这启发我们枚举或者二分答案?

但是答案不满足单调性,考虑从小到大枚举m。

对于每一个m,枚举两个野人在有生之年能否住在一起。可以推出一个同余方程,用扩欧可以求出最小整数解x,或者没有解。

如果x<=life[i]&&x<=life[j]那么当然不满足条件。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int C[N], P[N], L[N], n; int extend_gcd(int a, int b, int &x, int &y){
if (a==&&b==) return -;
if (b==){x=; y=; return a;}
int d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
bool check(int ans){
int d, x, y;
FOR(i,,n) FOR(j,i+,n) {
d=extend_gcd(P[i]-P[j],-ans,x,y);
if ((C[j]-C[i])%d) continue;
x*=((C[j]-C[i])/d);
int k=abs(-ans/d);
x=(x%k+k)%k;
if (x<=L[i]&&x<=L[j]) return false;
}
return true;
}
int main ()
{
int ans=;
scanf("%d",&n);
FOR(i,,n) scanf("%d%d%d",C+i,P+i,L+i), ans=max(ans,C[i]);
for (;;++ans) if (check(ans)) break;
printf("%d\n",ans);
return ;
}
05-11 21:41