第六章 数学问题(下)

1、一维世界的爱情

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

    y + km = t2×l +余数

两个相减,把列成ax+by = c的形式,,三个数字带进去

重要:写这个提一定要注意了,有内部私有类 主类一定得是public class,另一个是private static,,不然会编译错误的

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long x = sc.nextInt();//坐标
        long y = sc.nextInt();//坐标
        long m = sc.nextInt();//A一次跳
        long n = sc.nextInt();//B一次跳
        long L = sc.nextInt();//维度总线
        long a = m - n;
        long b = L;
        m = y - x;
        long d = 0;
        try{
            d= ext.linearequation(a,b,m);
            long x0 = ext.x;
            b/=d;
            b = Math.abs(b);
            x0 =(x0%b+b)%b;
            System.out.println(x0);

        }catch (Exception e){
            System.out.println("-1");
        }
    }
    private static class ext{
        static long x;//这儿要记录x 是因为要算出需要跳几步,要求出变量x和y的值,但是不用去两个类之间传递参数,只需要把作为static就可以类.变量了
        static long y;
        static long linearequation(long a,long b,long m) throws Exception{
            long d = ext_gcd(a,b);
            if(m%d!=0) throw new Exception("-1");

            long n = m/d;
            x*=n;
            y*=n;
            return d;
        }
        static long ext_gcd(long a,long b){
            if(b==0){
                x =1;
                y =0;
                return a;
            }
            else {
                long res = ext_gcd(b,a%b);
                long x1 = x;//备份x
                x = y;//更新x
                y = x1 - a/b*y;//更新y
                return res;
            }
        }
    }

}

2、

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       long []a = new long[n];
       long []b = new long[n];
       for(int i=0;i<n;i++){
           a[i] =sc.nextLong();
           b[i] = sc.nextLong();
       }
       try{
            long res =ext.linearEquationGroup(b,a);
           System.out.println(res);
       }catch (Exception e){
           System.out.println("-1");
       }
    }
    private static class ext{
        static long x;//这儿要记录x 是因为要算出需要跳几步,要求出变量x和y的值,但是不用去两个类之间传递参数,只需要把作为static就可以类.变量了
        static long y;
        static long linearequation(long a,long b,long m) throws Exception{
            long d = ext_gcd(a,b);
            if(m%d!=0) throw new Exception("-1");

            long n = m/d;
            x*=n;
            y*=n;
            return d;
        }
        static long ext_gcd(long a,long b){
            if(b==0){
                x =1;
                y =0;
                return a;
            }
            else {
                long res = ext_gcd(b,a%b);
                long x1 = x;//备份x
                x = y;//更新x
                y = x1 - a/b*y;//更新y
                return res;
            }
        }
        public static long linearEquationGroup(long[] r, long[] m) throws Exception {
            int len = r.length;
            if (len == 0 && r[0] == 0) return m[0];
            long R = r[0];
            long M = m[0];
            for (int i = 1; i < len; i++) {
                //这里往前看是两个方程
                long c = r[i] - R;
                long d = linearequation(M, m[i], c);
                //现在的static x是k1,用k1求得一个特解
                long x0 = R + M * x;//特解-》解系:X=x0+k*lcm(m1,m2)->得新方程: X 三 x0 mod lcm
                long lcm = M * m[i] / d;//这是新的m
                M = lcm;
                R = x0 % lcm;//x0变成正数
            }
            //合并完之后,只有一个方程 : X mod M = R
            while (R < 0)
                R += M;
            return R;
        }


    }

}

猜你喜欢

转载自blog.csdn.net/h_666666/article/details/86991384