Monday, August 18, 2014

COJ 1051. Div 3

  let's have a closer look at our sequence,
  1,12,123,1234,12345,123456
 So, every time the previous number is taken and the current index is being attached to the end of that number. The 3 divisibility rule is the following : if the sum of digits of a number is divisible by 3 then the number itself is divisible by 3.  So, let's notice something. The first number is not divisible by 3 , and if divided by 3 it gives 1 remainder. Then the second index (which is number 2) which is going to be attached to the previous number is not divisible by 3 either but it gives remainder 2 when divided. So when this new index is attached to the previous number we get a number divisible by 3. And the third index is divisible to 3 so the third number will be divisible as well. So, when it comes to 4th, it again gives remainder 1 when divided on 3 so when we attach this to the previous number which was already divisible by 3 we will get a number which gives 1 remainder. Then again, in the same way the 5th (which has 2 remainder when divided on 3) is being added the number is again divisible. Now if we numerate this sequence, only the all the numbers of 1+3*K form are not divisible on 3. The basic formula for this would be ANS=N- round_up(N/3) .


       FULL SOURCE CODE

No comments:

Post a Comment