枚举m 用exgcd来check一下就好了
#include<bits/stdc++.h>
using namespace std;
const int N=16;
inline int read(){
char ch=getchar(); int x=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}
return x*f;
}
int exgcd(int a,int b,int &x,int &y){
if(!b){x=1,y=0; return a;}
int tx,ty,d=exgcd(b,a%b,tx,ty);
y=tx-a/b*ty,x=ty; return d;
}
int n,c[N],p[N],l[N],m;
bool check(int x,int y){
int a=p[x]-p[y],b=m,C=c[y]-c[x],tx,ty;
if(!a)return !C;
int d=exgcd(a,b,tx,ty);
if(C%d)return 0;
tx*=C/d,tx=tx%(b/d); if(tx<0)tx+=abs(b/d);
if(tx>l[x]||tx>l[y])return 0;
return 1;
}
int main()
{
n=read();
for(int i=1;i<=n;++i)c[i]=read(),p[i]=read(),l[i]=read(),m=c[i]>m?c[i]:m;
for(;;m++){
bool ok=1;
for(int i=1;i<n&&ok;++i)for(int j=i+1;j<=n;++j)
if(check(i,j)){ok=0; break;}
if(ok)break;
}
printf("%d\n",m);
return 0;
}