zoukankan      html  css  js  c++  java
  • POJ1061(线性同余方程)

    青蛙的约会
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 105587   Accepted: 20789

    Description

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 
    我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。 

    Input

    输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

    Output

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

    Sample Input

    1 2 3 4 5

    Sample Output

    4
    扩展欧几里得求不定方程:ax+by=gcd(a,b)的解.不定方程 ax+by=c。若gcd(a,b)不能整除c,那么不定方程无解。
    代码一:
    #include <cstdio>
    using namespace std;
    typedef long long LL;
    LL extgcd(LL a,LL b,LL &x,LL &y)
    {
        LL d=a;
        if(b!=0)
        {
            d=extgcd(b,a%b,y,x);
            y-=(a/b*x);
        }
        else
        {
            x=1;y=0;
        }
        return d;
    }
    LL s1,s2,v1,v2,l;
    int main()
    {
        while(scanf("%lld%lld%lld%lld%lld",&s1,&s2,&v1,&v2,&l)!=EOF)
        {
            LL a,b,c,x,y;
            a=v1-v2;
            b=l;
            c=s2-s1;
            if(a<0)    a+=l;
            LL gcd=extgcd(a,b,x,y);
            if(c%gcd!=0)
            {
                printf("Impossible
    ");
            }
            else
            {
                LL mod=b/gcd;
                x=(x*(c/gcd))%mod;//注意扩大 c/gcd 倍 
                while(x<0)    x+=mod;
                printf("%lld
    ",x);    
            }
        }
        return 0;
    } 

    代码二:

    #include <iostream>
    using namespace std;
    typedef __int64 LL;//int前双'_' 
    LL extgcd(LL a,LL b,LL &x,LL &y)
    {
        LL d=a;
        if(b!=0)
        {
            d=extgcd(b,a%b,y,x);
            y-=(a/b*x);
        }
        else
        {
            x=1;y=0;
        }
        return d;
    }
    LL gcd(LL a,LL b)
    {
        if(b==0)    return a;
        else    return gcd(b,a%b);
    }
    LL s1,s2,v1,v2,m;
    int main()
    {
        while(cin>>s1>>s2>>v1>>v2>>m)
        {
            //两者相遇的条件 s1+v1*t=s2+v2*t-k*m => (v1-v2)*t+m*k=s2-s1
            //得线性同余方程 ax+by=c (a:v1-v2,x:t,b:m,k:y,c:s1-s1) 
            LL a=v1-v2;
            if(a<0)    a+=m;
            LL b=m;
            LL c=s2-s1;
            if(c<0)    c+=m;
            LL div=gcd(a,b); 
            if(c%div!=0)    //同余方程ax+by=c.有解的充要条件是 c|gcd(a,b).
            {
                cout<<"Impossible"<<endl;
                continue;
            }     
            a/=div;//将各个系数均缩小div倍
            b/=div;//ax+by=c => a'x+b'y=c' 
            c/=div; 
            LL x=0,y=0;
            extgcd(a,b,x,y);//求解线性同余方程 ax+by=1 
            x=(x*c)%b;//扩展欧几里得求的是ax+by=1中的x,结果需要将x扩大c倍 
            while(x<0)    x+=b;
            cout<<x<<endl;
        }
        return 0;
    }

     java版

    import java.util.Scanner;
    import static java.lang.System.out;
    public class Main{
        static Scanner in = new Scanner(System.in);    
        static long s1,s2,v1,v2,l;
        static class LL{
            private long value;
            public LL(long value)
            {
                this.value=value;
            }
            public long getValue()
            {
                return this.value;
            }
            public void setValue(long value)
            {
                this.value=value;
            }
        }
        static long extgcd(long a,long b,LL x,LL y)
        {
            long d=a;
            if(b!=0)
            {
                d=extgcd(b,a%b,y,x);
                long buf = y.getValue();
                buf-=(a/b*x.getValue());
                y.setValue(buf);
            }
            else
            {
                x.setValue(1);y.setValue(0);;
            }
            return d;
        }
        public static void main(String args[]){
            
            while(in.hasNext())
            {
                s1=in.nextLong();
                s2=in.nextLong();
                v1=in.nextLong();
                v2=in.nextLong();
                l=in.nextLong();
                long a=v1-v2,b=l,c=s2-s1;
                if(a<0)    a+=l;
                LL x = new LL(0),y = new LL(0);
                long div=extgcd(a,b,x,y);
                if(c%div!=0)
                {
                    out.println("Impossible");
                    continue;
                }
                long mod=b/div;
                long res=x.getValue();
                res=(res*(c/div))%mod;//最小正整数解
                while(res<0)
                    res+=mod;
                out.println(res);
            }
        }
    }
  • 相关阅读:
    【leetcode】ZigZag——easy
    联合
    AddChildViewController
    imageWithRender(图片的渲染模式)
    13.范型函数的使用:将字典合并到当前字典
    12.代码获取命名空间名称,并创建相应的类(anyClass 应用)
    13.static 面向对象
    11.swift 单例
    swift String 扩展
    swift UIView 扩展
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5701139.html
Copyright © 2011-2022 走看看