Three arrays

You are given three arrays a1n,b1n,c1n and two numbers M and K. Find a lexicographically minimum {x,y,z} such that there are exactly K indices i(1in) where xai+ybiciz=Mf for some integer f. Also, you are given ranges of xy, and z-- l1..3,r1..3((l1xr1,l2yr2,l3zr3). Here, a triplet of integers {x1,y1,z1} is considered to be lexicographically smaller than a triplet {x2,y2,z2} if sequence [x1,y1,z1] is lexicographically smaller than sequence [x2,y2,z2]. A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.

Example:

Input:
4 3 4 5 6 1 2 6 9 11 5 6 1 1 1 1 10 1 10 1 10
Output:
3 3 3

Approach

Java


public class ThreeArrays {
    public static void main(String[] args)  {
        int n = 4;
        int m = 3;
        int k = 4;

        long[] a = { 52112 };
        long[] b = { 6651 };
        long[] c = { 1961 };

        long xl = 1;
        long xr = 10;
        long yl = 1;
        long yr = 10;
        long zl = 1;
        long zr = 10;

        long xptr = xl, yptr, zptr;
        do {
            yptr = yl;
            do {
                zptr = zl;
                do {
                    int count = 0;
                    for (int i = 0; i < n; i++) {
                        long exp = xptr * a[i] + yptr * b[i] - zptr * c[i];
                        if (exp % m == 0)
                            count++;
                    }

                    if (count == k) {
                        System.out.println(xptr + " " + yptr + " " + zptr);
                        System.exit(0);
                    }

                    zptr++;
                } while (((zptr % m) != (zl % m)) && (zptr <= zr));
                yptr++;
            } while ((yptr % m) != (yl % m) && (yptr <= yr));
            xptr++;
        } while ((xptr % m) != (xl % m) && (xptr <= xr));

        System.out.println(-1);
    }
}


No comments:

Post a Comment