P2421 [NOI2002]荒岛野人 扩展欧几里得 枚举
Code:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int N; struct Node{ ll C,P,L; }nodes[20]; ll abss(ll a){return a<0?-a:a;} ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){x=1,y=0;return a;} ll ans=exgcd(b,a%b,x,y); ll tmp=x; x=y,y=tmp-a/b*y; return ans; } int check(int M){ for(int i=1;i<N;++i) for(int j=i+1;j<=N;++j) { ll a=nodes[i].P-nodes[j].P,b=M,c=nodes[j].C-nodes[i].C; ll x,y,mod,ans; ans=exgcd(a,b,x,y); if(c%ans!=0)continue; x*=(c/ans); mod=abss(b/ans); x=((x%mod)+mod)%mod; if(x<=nodes[i].L&&x<=nodes[j].L)return 0; } return 1; } int main(){ //freopen("in.txt","r",stdin); ll u=0; scanf("%d",&N); for(int i=1;i<=N;++i){ scanf("%lld%lld%lld",&nodes[i].C,&nodes[i].P,&nodes[i].L); u=max(u,nodes[i].C); } int ans=u; for(;;++ans)if(check(ans))break; printf("%d",ans); return 0; }