📜  使用M种颜色为N个盒子上色,以使K个盒子的颜色与其左侧的盒子不同

📅  最后修改于: 2021-05-06 08:13:54             🧑  作者: Mango

给定N个连续排列的盒子和M个颜色。任务是找到使用M种颜色绘制N个盒子的方式的方法,以使确切有K个盒子的颜色与其左侧盒子的颜色不同。以模998244353打印此答案。

例子:

先决条件:动态编程

方法:可以使用动态编程来解决此问题,其中dp [i] [j]表示使用M种颜色绘制i盒的方式的数量,这样,恰好有j个盒的颜色与其盒上的颜色不同左边。对于除一个框外的每个当前框,我们可以绘制与在其左侧框上绘制的颜色相同的颜色并求解dp [i – 1] [j],或者我们可以用剩余的M – 1个颜色对其进行绘制并求解dp [i – 1] [j – 1]递归。

下面是上述方法的实现:

C++
// CPP Program to Paint N boxes using M
// colors such that K boxes have color
// different from color of box on its left
#include 
using namespace std;
  
const int M = 1001;
const int MOD = 998244353;
  
int dp[M][M];
  
// This function returns the required number
// of ways where idx is the current index and
// diff is number of boxes having different
// color from box on its left
int solve(int idx, int diff, int N, int M, int K)
{
    // Base Case
    if (idx > N) {
        if (diff == K)
            return 1;
        return 0;
    }
  
    // If already computed
    if (dp[idx][ diff] != -1)
        return dp[idx][ diff];
  
    // Either paint with same color as
    // previous one
    int ans = solve(idx + 1, diff, N, M, K);
  
    // Or paint with remaining (M - 1)
    // colors
    ans += (M - 1) * solve(idx + 1, diff + 1, N, M, K);
  
    return dp[idx][ diff] = ans % MOD;
}
  
// Driver code
int main()
{
    int N = 3, M = 3, K = 0;
    memset(dp, -1, sizeof(dp));
  
    // Multiply M since first box can be
    // painted with any of the M colors and
    // start solving from 2nd box
    cout << (M * solve(2, 0, N, M, K)) << endl;
  
    return 0;
}


Java
// Java Program to Paint N boxes using M
// colors such that K boxes have color
// different from color of box on its left
  
class GFG
{
      
    static int M = 1001;
    static int MOD = 998244353;
  
    static int[][] dp = new int[M][M];
  
    // This function returns the required number
    // of ways where idx is the current index and
    // diff is number of boxes having different
    // color from box on its left
    static int solve(int idx, int diff,
                        int N, int M, int K)
    {
        // Base Case
        if (idx > N) 
        {
            if (diff == K)
                return 1;
            return 0;
        }
  
        // If already computed
        if (dp[idx][ diff] != -1)
            return dp[idx][ diff];
  
        // Either paint with same color as
        // previous one
        int ans = solve(idx + 1, diff, N, M, K);
  
        // Or paint with remaining (M - 1)
        // colors
        ans += (M - 1) * solve(idx + 1, 
                diff + 1, N, M, K);
  
        return dp[idx][ diff] = ans % MOD;
    }
  
    // Driver code
    public static void main (String[] args) 
    {
        int N = 3, M = 3, K = 0;
        for(int i = 0; i <= M; i++)
            for(int j = 0; j <= M; j++)
                dp[i][j] = -1;
      
        // Multiply M since first box can be
        // painted with any of the M colors and
        // start solving from 2nd box
        System.out.println((M * solve(2, 0, N, M, K)));
    }
}
  
// This code is contributed by mits


Python3
# Python3 Program to Paint N boxes using M 
# colors such that K boxes have color 
# different from color of box on its left 
  
M = 1001; 
MOD = 998244353; 
  
dp = [[-1]* M ] * M
  
# This function returns the required number 
# of ways where idx is the current index and 
# diff is number of boxes having different 
# color from box on its left 
def solve(idx, diff, N, M, K) :
      
    # Base Case 
    if (idx > N) : 
        if (diff == K) :
            return 1 
        return 0
  
    # If already computed 
    if (dp[idx][ diff] != -1) :
        return dp[idx]; 
  
    # Either paint with same color as 
    # previous one 
    ans = solve(idx + 1, diff, N, M, K); 
  
    # Or paint with remaining (M - 1) 
    # colors 
    ans += (M - 1) * solve(idx + 1, diff + 1, N, M, K); 
  
    dp[idx][ diff] = ans % MOD; 
      
    return dp[idx][ diff]
  
# Driver code 
if __name__ == "__main__" : 
  
    N = 3
    M = 3
    K = 0 
  
    # Multiply M since first box can be 
    # painted with any of the M colors and 
    # start solving from 2nd box 
    print(M * solve(2, 0, N, M, K)) 
  
# This code is contributed by Ryuga


C#
// C# Program to Paint N boxes using M
// colors such that K boxes have color
// different from color of box on its left
using System;
class GFG
{
      
static int M = 1001;
static int MOD = 998244353;
  
static int[,] dp = new int[M, M];
  
// This function returns the required number
// of ways where idx is the current index and
// diff is number of boxes having different
// color from box on its left
static int solve(int idx, int diff,
                 int N, int M, int K)
{
    // Base Case
    if (idx > N) 
    {
        if (diff == K)
            return 1;
        return 0;
    }
  
    // If already computed
    if (dp[idx, diff] != -1)
        return dp[idx, diff];
  
    // Either paint with same color as
    // previous one
    int ans = solve(idx + 1, diff, N, M, K);
  
    // Or paint with remaining (M - 1)
    // colors
    ans += (M - 1) * solve(idx + 1, 
                diff + 1, N, M, K);
  
    return dp[idx, diff] = ans % MOD;
}
  
// Driver code
public static void Main () 
{
    int N = 3, M = 3, K = 0;
    for(int i = 0; i <= M; i++)
        for(int j = 0; j <= M; j++)
            dp[i, j] = -1;
  
    // Multiply M since first box can be
    // painted with any of the M colors and
    // start solving from 2nd box
    Console.WriteLine((M * solve(2, 0, N, M, K)));
}
}
  
// This code is contributed by chandan_jnu


PHP
 $N) 
    {
        if ($diff == $K)
            return 1;
        return 0;
    }
  
    // If already computed
    if ($dp[$idx][$diff] != -1)
        return $dp[$idx][$diff];
  
    // Either paint with same color 
    // as previous one
    $ans = solve($idx + 1, $diff, $N, $M, $K);
  
    // Or paint with remaining (M - 1)
    // colors
    $ans += ($M - 1) * solve($idx + 1,
             $diff + 1, $N, $M, $K);
  
    return $dp[$idx][$diff] = $ans % $MOD;
}
  
// Driver code
$N = 3;
$M = 3;
$K = 0;
  
// Multiply M since first box can be
// painted with any of the M colors and
// start solving from 2nd box
echo ($M * solve(2, 0, $N, $M, $K));
  
// This code is contributed by chandan_jnu
?>


输出:
3