绘制N幅画的方式,以使相邻的画在C编程中没有相同的颜色
在这个问题中,给您N幅画,并且我们可以使用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
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短