在 C 语言中绘制 N 幅画的方式,使得相邻的画作没有相同的颜色
在这个问题中,我们给定 N 幅画,还有 m 个颜色,我们可以用 m 个颜色绘制画作,我们需要找出可以绘制画作的方式,使得没有相同颜色的画作彼此相邻。
程序输出的值可能很大,处理这些值有点问题,因此我们将以标准模 109 +7 计算其答案。
找到方法数的公式是
Ways = n*(m-1)(n-1)
描述问题的示例,这需要绘画数量 n 和颜色数量 m
输入
n = 5 ,m = 6
输出
3750
示例
#include <iostream>
#include<math.h>
#define modd 1000000007
using namespace std;
unsigned long find(unsigned long x,
unsigned long y, unsigned long p) {
unsigned long res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int ways(int n, int m) {
return find(m - 1, n - 1, modd) * m % modd;
}
int main() {
int n = 5, m = 6;
cout<<"There are"<<ways(n, m)<<"ways";
return 0;
}输出
There are 3750 ways
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP