给定两个整数n和m,其中n表示从1到n编号的某些绘画,m表示从1到m的任意数量的颜色。任务是找到多种绘画方式,以使两幅连续的绘画都不具有相同的颜色。
注意:答案必须以10 ^ 9 +7为模,因为答案可能非常大。
例子:
Input: n = 4, m = 2
Output: 2
Input: n = 4, m = 6
Output: 750
提问人:National Instruments
方法:
给定颜色的总数是m ,而绘画总数是1到n 。根据没有两个相邻绘画具有相同颜色的条件,任何人都可以用m种颜色绘画第一幅绘画,其余绘画可以用m-1种颜色绘画,但前一种绘画使用的颜色除外。那。因此,如果我们得出总数为方法的解决方案,
m * (m-1)^(n-1) is the actual answer.
现在,可以通过简单的迭代来计算,也可以通过在O(logn)时间内进行有效功率计算的方法来计算。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
#define modd 1000000007
using namespace std;
// Function for finding the power
unsigned long power(unsigned long x,
unsigned long y, unsigned long p)
{
unsigned long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to calculate the number of ways
int ways(int n, int m)
{
// Answer must be modulo of 10^9 + 7
return power(m - 1, n - 1, modd) * m % modd;
}
// Driver code
int main()
{
int n = 5, m = 5;
cout << ways(n, m);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
static final int modd = 1000000007;
// Function for finding the power
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y % 2 == 1)
{
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to calculate the number of ways
static int ways(int n, int m)
{
// Answer must be modulo of 10^9 + 7
return (int) (power(m - 1, n - 1, modd)
* m % modd);
}
// Driver code
public static void main(String[] args)
{
int n = 5, m = 5;
System.out.println(ways(n, m));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the
# above approach
modd = 1000000007
# Function for finding the power
def power(x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Function to calculate the number of ways
def ways(n, m):
# Answer must be modulo of 10^9 + 7
return power(m - 1, n - 1, modd) * m % modd
# Driver code
n, m = 5, 5
print(ways(n, m))
# This code is contributed
# by Mohit Kumar 29
C#
// C# implementation of the above approach
using System;
class GFG
{
static int modd = 1000000007;
// Function for finding the power
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y % 2 == 1)
{
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to calculate the number of ways
static int ways(int n, int m)
{
// Answer must be modulo of 10^9 + 7
return (int) (power(m - 1, n - 1, modd)
* m % modd);
}
// Driver code
static public void Main ()
{
int n = 5, m = 5;
Console.WriteLine(ways(n, m));
}
}
// This code is contributed by ajit
PHP
0)
{
// If y is odd, multiply
// x with result
if ($y & 1)
$res = ($res * $x) % $p;
// y must be even now
// y = $y/2
$y = $y >> 1;
$x = ($x * $x) % $p;
}
return $res;
}
// Function to calculate the number of ways
function ways($n, $m)
{
$modd =1000000007;
// Answer must be modulo of 10^9 + 7
return (power($m - 1, $n - 1,
$modd) * $m ) % $modd;
}
// Driver code
$n = 5;
$m = 5;
echo ways($n, $m);
// This code is contributed
// by Arnab Kundu
?>
输出:
1280