zoukankan      html  css  js  c++  java
  • BZOJ 1407 exgcd

    枚举 山洞个数,每次n^2  枚举两个野人之间是否会发生冲突.

      相当于求p[i]*x+c[i] = p[j]*x+c[j] mod m (天数)

        ----------> (p[i]-p[j])*x-m*y = c[j]-c[i] .. exgcd 求解.

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <stdio.h>
    #define MAXN 17
    using std::max;
    int n,p[MAXN],c[MAXN],l[MAXN],mx;
     
     
    template<typename _t>
    inline _t read(){
        _t x=0,f=1;
        char ch=getchar();
        for(;ch>'9'||ch<'0';ch=getchar())if(ch=='-')f=-f;
        for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+(ch^48);
        return x*f;
    }
     
    inline int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
     
    void gcd(int a,int b,int &x,int &y){
    	if(b==0){x=1;y=0;return;}
    	gcd(b,a%b,x,y);
    	int t=x;x=y;y=t-a/b*y;
    }
     
    inline bool Judge(int tot){
        // (p[i]-p[j])*x -my = c[j]-c[i];
        for(register int i=1;i<=n;i++){
            for(register int j=i+1;j<=n;j++){
                int A,B,C,x,y;
                A = p[i]-p[j],B=c[j]-c[i],C=tot;
                int t = gcd(A,C);
                if(B%t==0){
                    A/=t;B/=t;C/=t;
                    gcd(A,C,x,y);
                    if(C<0)C=-C;
                    x = ((B*x)%C+C)%C;
                    if(!x)x+=C;
                    if(x<=l[i]&&x<=l[j])return 0;
                }
            }
        }
        return 1;
    }
     
    int main(){
        n=read<int>();
        for(int i=1;i<=n;i++){
            c[i]=read<int>(),
            p[i]=read<int>(),
            l[i]=read<int>();
            mx=max(mx,c[i]);
        }
        while(!Judge(mx++));mx--;
        printf("%d
    ",mx);
    }



  • 相关阅读:
    Apache Commons Configuration的应用
    JAVA单例MongoDB工具类
    配置SpringMVC返回JSON遇到的坑
    DRF---序列化器-Serializer
    面试题--web(django)
    DRF----web应用模式
    面试题--网络编程
    Vue-项目之免费课和购物车实现
    面试题--面向对象
    vue- 项目之前端页面搭建1
  • 原文地址:https://www.cnblogs.com/Cooook/p/7738484.html
Copyright © 2011-2022 走看看