combinations - 具有不同值的连续元素的数组计数
问题描述
给定两个正整数 n , m。任务是计算可以形成大小为 n的不同数组的数量,使得每个元素在1到m之间并且两个连续元素不同。此外,每个数组的第一个和最后一个元素应该相同。
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.
如果我错了,请纠正我。
解决方案
推荐阅读
- javascript - 如何将 jquery addClass() 方法添加到元素 ng-repeat AngularJS
- c# - 将数据从控制器传递到 Razor 页面,“对象引用未设置为对象实例”的错误
- javascript - 获取浏览器+平台键盘修饰符
- flutter - 屏幕弹出后 Flutter WebviewScaffold 弹出
- javascript - .net mvc @foreach 表/单元格的 id
- python - 如何使用 BeautifulSoup 从 div 类中的特定文本中获取数据
- python-3.x - 我应该计算一个数字中的唯一数字,但是让这个 int 对象在第 4 行不可迭代并且不知道如何修复它
- java - 如何将嵌套数组中的值映射到属性
- excel - 如何在行太长的地方应用续行?
- python - S3ToSFTP:将多个文件从同一个 S3 密钥移动到 SFTP 路径