Hi community. I was giving test for some hiring test. The problem says given an array of size N and numbers from 1...M find number of ways to create the array with the followings restrictions. 1) a[i]≠a[i+1] 2) a[1]=a[N] (1 based index) Constraints 3<=N,M<=10^18 I was came up with formula M*(M-1)^(N-3)*(M-2) n>3 and M*(M-1) n==3 But it gave WA. Any suggestions. Thankyou in advance.
Problem link?
if(n is odd) m*(m-1)^(n-2)%mod else m*(m-3)^(n-3)*(m-2)%mod