__zac__'s blog

By __zac__, history, 4 years ago, In English

I'm trying to solve this problem on SPOJ using inclusion-exclusion principle but I can't get the correct answer. My code

    private static void solve(long n, long m, long a, long d) {
        long[] values = new long[5];

        for (int i = 0; i < 5; i++) {
            values[i] = a + i*d;
        }

        long left, right, sum = 0, diff = m-n+1;

        for (int i = 0; i < (1 << 5); i++) {
            int count = 0;
            long divisor = 1;
            for (int j = 0; j < 5; j++) {
                if ((i & (1 << j)) > 0) {
                    count++;
                    divisor = lcm(divisor, values[j]);
                }
            }
            left = (n-1)/divisor;
            right = m/divisor;
            if (count % 2 == 0) sum -= right-left;
            else sum += right - left;
        }
        System.out.println(diff-sum);
    }

    private static long lcm(long a, long b) {
        return (a*b)/gcd(a,b);
    }

    private static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a%b);
    }

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By __zac__, history, 6 years ago, In English

Hello geeks,

I'm software engineer looking also to become good at competitive programming. So how do you guys ended up to become good at this area. Is it by solving difficult problems? or you started by easy ones and getting better by time?

I mean, I saw many of you using crazy techniques to solve a non-trivial problem that can take to me days or even weeks. So I need your advise to get better and if you have suggestions for me in the way I should start thinking about problems I would really appreciate it.

I often practice using the codeforces' problemset(1300-1900). But I want to be able to solve mostly Div2 D, E, F problems.

Sorry for bothering you but any help is welcome

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it