zoukankan      html  css  js  c++  java
  • exgcd

    P1516 青蛙的约会

    链接:https://www.luogu.org/problemnew/show/P1516

    题目描述

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。

    我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。

    输入输出格式

    输入格式:

    输入只包括一行5个整数x,y,m,n,L

    其中0<x≠y < =2000000000,0 < m、n < =2000000000,0 < L < =2100000000。

    输出格式:

    输出碰面所需要的天数,如果永远不可能碰面则输出一行"Impossible"。

    输入输出样例

    输入样例#1: 
    1 2 3 4 5
    
    输出样例#1: 
    4
    

    说明

    各个测试点2s

    题解:(x + m * t )%L = (y+n*t)%L  <=> (m-n)*t + L*y = y - x, 然后exgcd,注意要求出最小正整数解

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    #define maxn 2100000000 +5
    #define ll long long
    ll nx, ny;
    void exgcd(ll &nx, ll &ny, ll a, ll b, ll &d){
        if(b == 0){
            d = a, nx = 1, ny = 0;
            return;
        }
    
        ll x0, y0;
        exgcd(x0, y0, b, a%b, d);
        nx = y0;
        ny = x0 - (a/b) * y0;
    }
    
    int main()
    {
        ll x,y,d,m,n,L;
        cin>>x>>y>>m>>n>>L;
        ll a = n - m, b = L, c = x - y;
        exgcd(nx, ny, a, b, d);
        if(c % d)cout<<"Impossible"<<endl;
        else {
            ll nb = b/d;
            if(nb > 0)nx = (c/d*nx%nb + nb) % nb;
            else nx = (c/d*nx%(-nb) - nb) % (-nb);
            cout<<nx<<endl;
        }
        return 0;
    }
    View Code

    1407: [Noi2002]Savage

    Time Limit: 5 Sec  Memory Limit: 64 MB
    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1407

    Description

    Input

    第1行为一个整数N(1<=N<=15),即野人的数目。
    第2行到第N+1每行为三个整数Ci, Pi, Li表示每个野人所住的初始洞穴编号,每年走过的洞穴数及寿命值。
    (1<=Ci,Pi<=100, 0<=Li<=10^6 )

    Output

    仅包含一个数M,即最少可能的山洞数。输入数据保证有解,且M不大于10^6。

    Sample Input

    3
    1 3 4
    2 7 3
    3 2 1

    Sample Output

    6
    //该样例对应于题目描述中的例子。
    题解:题中求的是 (Ci + Pi*t) % L != (Cj + Pj*t) % L, 而我们只能解决等于的情况,变形得:(Pi-Pj) * t + L*y = Cj -Ci ,;
    若解得t > min(Li, Lj)或无解,则说明死了才能碰到或永远碰不到;对于L可以枚举
    写的时候把特解的公式记错了O__O"…
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    #define ll long long
    int c[20],p[20],l[20];
    int exgcd(int a, int b, int &x, int &y){
        if(!b){
            x = 1;
            y = 0;
            return a;
        }
        int x0, y0;
        int d = exgcd(b, a%b, x0, y0);
        x = y0;
        y = x0 - (a/b)*y0;
        return d;
    }
    int main()
    {
        int n, mx = 0;
        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]);
        }
        while(mx){
            int f = 0;
            for(int i = 1; i <= n; i++){  
                for(int j = i+1; j <= n; j++){
                    int a = p[i] - p[j], b = mx, g = c[j] - c[i];
                    int xx, yy;
                    int d = exgcd(a, b, xx, yy);
                    if(g%d)continue;
                    int dx = b/d;
                    //printf("%d %d %d %d %d %d %d
    ",i,j,a,b,xx,g,dx);
                    if(dx>0)xx = (g/d*xx%dx + dx)%dx;
                    else xx = (g/d*xx%(-dx) - dx)%(-dx);
                    if(xx <= min(l[i],l[j])){f=1;break;}
                }
                if(f)break;
            }
            if(f)mx++;
            else break;
        }
        printf("%d
    ",mx);
        return 0;
    }
    
    View Code
  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8873452.html
Copyright © 2011-2022 走看看