1407: [Noi2002]Savage

5 篇文章 0 订阅

枚举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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值