zoukankan      html  css  js  c++  java
  • 题解【luogu P2421 bzoj P1407 [NOI2002]荒岛野人】

    洛谷题目链接

    bzoj题目链接


    题目大意:给定(n)(C_i, P_i, L_i),求最小的(M)使得对于任意的(i,j (1 leq i, j leq n))

    [C_i + P_i imes x equiv C_j + P_j imes x pmod M ]

    不成立

    (这里的不成立指的是无解或者解出来的 (x<min(L_i,L_j)),即相遇之前有一人死掉

    其中(x)为正整数(就是走了(x)天相遇)

    分析

    从小到大枚举(M)注意没有单调性不能二分

    原式可变形为

    [C_i + P_i imes x = C_j + P_j imes x +M imes y ]

    [(P_i-P_j) imes x - M imes y = C_j-C_i ]

    若无解,则此时的(M)为所求
    若有解,用扩展欧几里得解出该方程的最小解(x_{min})
    如果(x_{min}<min(L_i,L_j)),问题解决;否则继续枚举

    代码:

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
     
    using namespace std;
     
    int n, C[20], p[20], l[20], mx = -1;
    inline int gcd(int a, int b)
    {
        if(!b) return a;
        else return gcd(b, a % b);
    }             
    inline void exgcd(int a, int b, int &x, int &y)
    {
        if(!b) { x = 1, y = 0; return ; }
        exgcd(b, a % b, x, y);
        int t = x; x = y, y = t - (a / b) * y;
    }
    inline bool check(int m)
    {
        for(int i = 1; i <= n; i++)
            for(int j = i + 1; j <= n; j++)
            {
                int a = p[j] - p[i], b = m, c = C[i] - C[j], x = 0, y = 0, g = gcd(a, b);
                if(c % g == 0)
                {
                    a /= g, b /= g, c /= g;
                    exgcd(a, b, x, y);
                    b = abs(b);
                    x = ((x * c) % b + b) % b;
                    if(!x) x += b;
                    if(x <= min(l[i], l[j])) return false;
                }
            }
        return true;
    }
    int main()
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &C[i], &p[i], &l[i]); 
            mx = max(mx, C[i]);
        }
        for(int i = mx;;i++)
            if(check(i))
            {
                printf("%d
    ", i);
                return  0;
            }
        return 1;//防抄
    }
    
    蒟蒻一枚
  • 相关阅读:
    java通过ST4使用模板字符串
    使用 docker创建redis实例并且连接
    Docker 认证成功后还是无法push构建好的镜像
    记录一次在openwrt中折腾docker
    全局模式、PAC模式、直连模式的区别
    Vue Router中调用this.$router.push() 时,location使用path无法传入params
    liunx之系统
    liunx之通配符&正则表达式
    liunx之基础
    liunx之find命令
  • 原文地址:https://www.cnblogs.com/acfunction/p/8858577.html
Copyright © 2011-2022 走看看