Saturday, 26 March 2016

Solution of LIGHT OJ 1136 :: Division by 3


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int a,b,c,d,e,f,g,h,i,j,k,l,m,n;
    scanf("%d", &a);
    for(b=1; b<=a; b++)
    {
        scanf("%d%d", &c, &d);
        e=(c/3)*2;
        f=c%3;
        if(f==2)
            e+=1;
        i=(d/3)*2;
        j=d%3;
        if(j==2)
            i+=1;
        m=i-e;
        m+=1;
        if(f==1)
            m-=1;
        printf("Case %d: %d\n",b,m);
    }
    return 0;
}

5 comments:

  1. I cant get this "e=(c/3)*2". Would you plz explain!

    ReplyDelete
  2. Yes,,i also want to know the reason of (c/3)*2;
    fact.

    ReplyDelete
    Replies
    1. Write down the numbers up to 10,you can see there is a pattern among the numbers.Among 3 numbers,2 is always divisible by 3.So if you have 9 numbers,you can create 3 sets(9/3=3).Now each set have 2 numbers that are divisible by 3.For 3 sets,the total numbers that would be divisible by 3 would be 6(3*2).

      Delete
  3. I didn't understand anything . Can u please explain the whole code ?

    ReplyDelete
    Replies
    1. Series: 1,12,123,1234,12345,123456......
      in this code it applied a technique. The technique is find all divisible number by 3. from 0 to d and remove them from 0 to c-1 all divisible number by 3.

      Now if you close look at the number of series, you will find that for each 3 numbers there is 2 numbers whose are divisible by 3. [e=(c/3)*2] by this line assigned total number of divisible up to c. now if we get reminder 2 that's mean there missed one divisible number. So that is added next line. Same technique is applied for the d(upper bound). Then it removed from ) to c-1 all divisible number as I mentioned before. But if(f==1) then the it adds extra number so it needs to remove.

      Delete

Note: only a member of this blog may post a comment.