首页 > 解决方案 > 具有不同值的连续元素的数组计数

问题描述

给定两个正整数 n , m。任务是计算可以形成大小为 n的不同数组的数量,使得每个元素在1m之间并且两个连续元素不同。此外,每个数组的第一个和最后一个元素应该相同。

I/p                          O/p
3   3        -->             6
3   4        -->            12
4   4        -->            24

这就是我所拥有的

如果n == 3 输出是m * (m - 1)
如果n != 3输出是m * ((m - 1)^(n - 3)) * (2 * m - 3)

解释

At first place there are m choices.
From 2nd to (n - 2) places there is there  are (m - 1) choices. 
For (n - 1) place there are 2 cases: 
    1st is if (n - 2) and nth place are same then (m - 1) choices
    2nd is if (n - 2) and nth place are not same then (m - 2) choices

So total (2 * m - 3) choices. 

如果我错了,请纠正我。

标签: combinationspermutation

解决方案


推荐阅读