BZOJ1407 [Noi2002]Savage 【扩展欧几里得】

题目链接

BZOJ1407

题解

枚举\(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;
}

转载于:https://www.cnblogs.com/Mychael/p/9063443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值