RaFiN_

Palindrome-less Arrays cf-1140E

Jul 9th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. Consider a -1 -1 ...ltimes... -1 -1 b
  2.  
  3. You need to find the no. of arrays in which no two consecutive elements are equal.
  4.  
  5. So first if you assign the value to the lth "-1" element i.e. element before b, obviously you can assign all the values to it from 1 to k, except b. You can't assign b to it because no two consecutive elements should be equal. So there are (k-1) ways in which you can assign the value to lth "-1" element.
  6.  
  7. Now I hope you know the meaning of cntSame(l), and cntDiff(l).
  8.  
  9. To calculate cntSame(l), consider what happens when a=b.
  10.  
  11. Suppose you assign value to p to the lth "-1" element. Obviously, p!=b.
  12.  
  13. Since a=b, therefore p!=a. So, remaining number of values will be (k-1) i.e. except a(or b, since a=b). Therefore cntSame(l) = (k-1)*cntDiff(l-1)
  14.  
  15. To calculate cntDiff(l), consider what happens when a!=b.
  16.  
  17. Suppose you assign value to p to the lth "-1" element. Obviously, p!=b.
  18.  
  19. Since a!=b, You can either assign p=a or p!=a. There will be exactly one case to assign p=a and (k-2) cases to assign p!=a. (k-2 because you cannot also assign p=b)
  20.  
  21. Therefore cntDiff(l) = cntSame(l-1) + (k-2)*cntDiff(l-1)
Add Comment
Please, Sign In to add comment