📜  总元素总和可被 3 整除的大小为 n 的子集的计数

📅  最后修改于: 2021-09-22 10:34:07             🧑  作者: Mango

给定一个整数n和一个范围[l, r] ,任务是使用给定范围内的整数找到大小为n 的子集的总数,使得其元素的总和可以被3整除。
例子:

方法:由于我们需要子集元素的总和能被 3 整除。因此,我们不关心数字,而是计算数字,使它们在除以3 时分别得到余数012公式如下:

现在,通过动态规划dp[i][j]我们可以检查有多少元素会给出可被3整除的总和。这里dp[i][j]表示除以3 后得到余数j的前i 个元素的总和。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define MOD 1000000007
#define ll long long int
using namespace std;
 
// Function to return the total number of
// required sub-sets
int totalSubSets(ll n, ll l, ll r)
{
 
    // Variable to store total elements
    // which on dividing by 3  give
    // remainder 0, 1 and 2 respectively
    ll zero = floor((double)r / 3)
              - ceil((double)l / 3) + 1;
    ll one = floor((double)(r - 1) / 3)
             - ceil((double)(l - 1) / 3) + 1;
    ll two = floor((double)(r - 2) / 3)
             - ceil((double)(l - 2) / 3) + 1;
 
    // Create a dp table
    ll dp[n][3];
    memset(dp, 0, sizeof(dp));
    dp[0][0] = zero;
    dp[0][1] = one;
    dp[0][2] = two;
 
    // Process for n states and store
    // the sum (mod 3) for 0, 1 and 2
    for (ll i = 1; i < n; ++i) {
 
        // Use of MOD for large numbers
        dp[i][0] = ((dp[i - 1][0] * zero)
                    + (dp[i - 1][1] * two)
                    + (dp[i - 1][2] * one))
                   % MOD;
        dp[i][1] = ((dp[i - 1][0] * one)
                    + (dp[i - 1][1] * zero)
                    + (dp[i - 1][2] * two))
                   % MOD;
        dp[i][2] = ((dp[i - 1][0] * two)
                    + (dp[i - 1][1] * one)
                    + (dp[i - 1][2] * zero))
                   % MOD;
    }
 
    // Final answer store at dp[n - 1][0]
    return dp[n - 1][0];
}
 
// Driver Program
int main()
{
    ll n = 5;
    ll l = 10;
    ll r = 100;
    cout << totalSubSets(n, l, r);
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
         
    static int MOD = 1000000007;
     
    // Function to return the total number of
    // required sub-sets
    static int totalSubSets(int n, int l, int r)
    {
     
        // Variable to store total elements
        // which on dividing by 3 give
        // remainder 0, 1 and 2 respectively
        int zero = (int)Math.floor((double)r / 3)
                - (int)Math.ceil((double)l / 3) + 1;
        int one = (int)Math.floor((double)(r - 1) / 3)
                - (int)Math.ceil((double)(l - 1) / 3) + 1;
        int two = (int)Math.floor((double)(r - 2) / 3)
                - (int)Math.ceil((double)(l - 2) / 3) + 1;
     
        // Create a dp table
        int [][] dp = new int[n][3];
     
        dp[0][0] = zero;
        dp[0][1] = one;
        dp[0][2] = two;
     
        // Process for n states and store
        // the sum (mod 3) for 0, 1 and 2
        for (int i = 1; i < n; ++i)
        {
     
            // Use of MOD for large numbers
            dp[i][0] = ((dp[i - 1][0] * zero)
                        + (dp[i - 1][1] * two)
                        + (dp[i - 1][2] * one))
                    % MOD;
            dp[i][1] = ((dp[i - 1][0] * one)
                        + (dp[i - 1][1] * zero)
                        + (dp[i - 1][2] * two))
                    % MOD;
            dp[i][2] = ((dp[i - 1][0] * two)
                        + (dp[i - 1][1] * one)
                        + (dp[i - 1][2] * zero))
                    % MOD;
        }
     
        // Final answer store at dp[n - 1][0]
        return dp[n - 1][0];
    }
     
    // Driver Program
    public static void main(String []args)
    {
        int n = 5;
        int l = 10;
        int r = 100;
        System.out.println(totalSubSets(n, l, r));
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
import math
 
# Function to return the total
# number of required sub-sets
def totalSubSets(n, l, r):
     
    MOD = 1000000007 ;
     
    # Variable to store total elements
    # which on dividing by 3 give
    # remainder 0, 1 and 2 respectively
    zero = (math.floor(r / 3) -
            math.ceil(l / 3) + 1);
    one = (math.floor((r - 1) / 3) -
           math.ceil((l - 1) / 3) + 1);
    two = (math.floor((r - 2) / 3) -
           math.ceil((l - 2) / 3) + 1);
 
    # Create a dp table
    dp = [[0 for x in range(3)]
             for y in range(n)]
             
    dp[0][0] = zero;
    dp[0][1] = one;
    dp[0][2] = two;
 
    # Process for n states and store
    # the sum (mod 3) for 0, 1 and 2
    for i in range(1, n):
 
        # Use of MOD for large numbers
        dp[i][0] = ((dp[i - 1][0] * zero) +
                    (dp[i - 1][1] * two) +
                    (dp[i - 1][2] * one)) % MOD;
        dp[i][1] = ((dp[i - 1][0] * one) +
                    (dp[i - 1][1] * zero) +
                    (dp[i - 1][2] * two)) % MOD;
        dp[i][2] = ((dp[i - 1][0] * two)+
                    (dp[i - 1][1] * one) +
                    (dp[i - 1][2] * zero)) % MOD;
 
    # Final answer store at dp[n - 1][0]
    return dp[n - 1][0];
 
# Driver Code
n = 5;
l = 10;
r = 100;
print(totalSubSets(n, l, r));
     
# This code is contributed
# by chandan_jnu


C#
// C# implementation of the approach
using System;
 
class GFG
{
         
    static int MOD = 1000000007;
     
    // Function to return the total number of
    // required sub-sets
    static int totalSubSets(int n, int l, int r)
    {
     
        // Variable to store total elements
        // which on dividing by 3 give
        // remainder 0, 1 and 2 respectively
        int zero = (int)Math.Floor((double)r / 3)
                - (int)Math.Ceiling((double)l / 3) + 1;
        int one = (int)Math.Floor((double)(r - 1) / 3)
                - (int)Math.Ceiling((double)(l - 1) / 3) + 1;
        int two = (int)Math.Floor((double)(r - 2) / 3)
                - (int)Math.Ceiling((double)(l - 2) / 3) + 1;
     
        // Create a dp table
        int [, ] dp = new int[n, 3];
     
        dp[0,0] = zero;
        dp[0,1] = one;
        dp[0,2] = two;
     
        // Process for n states and store
        // the sum (mod 3) for 0, 1 and 2
        for (int i = 1; i < n; ++i)
        {
     
            // Use of MOD for large numbers
            dp[i,0] = ((dp[i - 1, 0] * zero)
                        + (dp[i - 1, 1] * two)
                        + (dp[i - 1, 2] * one))
                    % MOD;
            dp[i,1] = ((dp[i - 1, 0] * one)
                        + (dp[i - 1, 1] * zero)
                        + (dp[i - 1, 2] * two))
                    % MOD;
            dp[i,2] = ((dp[i - 1, 0] * two)
                        + (dp[i - 1, 1] * one)
                        + (dp[i - 1, 2] * zero))
                    % MOD;
        }
     
        // Final answer store at dp[n - 1,0]
        return dp[n - 1, 0];
    }
     
    // Driver Program
    public static void Main()
    {
        int n = 5;
        int l = 10;
        int r = 100;
        Console.WriteLine(totalSubSets(n, l, r));
    }
}
 
// This code is contributed by ihritik


PHP


Javascript


输出:
80107136

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程