内容中心

2026年评价高的提花软件高评分公司推荐-杭州经纬计算机系统工程有限公司

To solve this problem, we need to find the number of subsets of a given set of elements where the sum of the elements in the subset is divisible by 3. The solution involves analyzing the elements modulo 3 and using dynamic programming to count valid subsets.

Approach

  1. Modulo Grouping: First, we group the elements into three categories based on their remainder when divided by 3:

    • Group 0: Elements with remainder 0.
    • Group 1: Elements with remainder 1.
    • Group 2: Elements with remainder 2.
  2. Dynamic Programming (DP) for Valid Subsets:

    • DP for Group 1: Compute the number of subsets of Group 1 that sum to each remainder (0, 1, 2) modulo 3.
    • DP for Group 2: Compute the number of subsets of Group 2 that sum to each remainder (0, 1, 2) modulo 3.
  3. Combine Results:

    • The valid subsets from Group 1 and Group 2 are those where their combined sum is divisible by 3. This is calculated by multiplying the count of subsets from Group 1 with remainder r and Group 2 with remainder (3 - r) % 3 (since their sum will be divisible by 3).
    • Group 0 elements can be included in any subset (since they don't affect the sum modulo 3), so we multiply the result by 2^a (where a is the size of Group 0).

Solution Code

MOD = 10**9 + 7

n = int(input())
elements = list(map(int, input().split()))

a = 0  # count of elements mod3=0
b = 0  # count mod3=1
c = 0  # count mod3=2

for num in elements:
    rem = num % 3
    if rem == 0:
        a += 1
    elif rem == 1:
        b += 1
    else:
        c += 1

# Compute dp1 for group1 (mod3=1)
dp1 = [1, 0, 0]
for _ in range(b):
    new_dp = [0] * 3
    new_dp[0] = (dp1[0] + dp1[2]) % MOD
    new_dp[1] = (dp1[1] + dp1[0]) % MOD
    new_dp[2] = (dp1[2] + dp1[1]) % MOD
    dp1 = new_dp

# Compute dp2 for group2 (mod3=2)
dp2 = [1, 0, 0]
for _ in range(c):
    new_dp = [0] * 3
    new_dp[0] = (dp2[0] + dp2[1]) % MOD
    new_dp[1] = (dp2[1] + dp2[2]) % MOD
    new_dp[2] = (dp2[2] + dp2[0]) % MOD
    dp2 = new_dp

# Calculate valid combinations from group1 and group2
total_g1g2 = (dp1[0] * dp2[0] + dp1[1] * dp2[2] + dp1[2] * dp2[1]) % MOD

# Calculate 2^a mod MOD
pow2a = pow(2, a, MOD)

# Final answer
ans = (total_g1g2 * pow2a) % MOD
print(ans)

Explanation

  1. Modulo Grouping: We count the number of elements in each group (0,1,2) using their remainder modulo 3.
  2. DP Calculation:
    • For Group 1, each element contributes 1 to the sum modulo 3. The DP array dp1 tracks the number of subsets for each remainder.
    • For Group 2, each element contributes 2 to the sum modulo 3. The DP array dp2 tracks the number of subsets for each remainder.
  3. Combining Results: The valid subsets from Group 1 and Group 2 are those where their combined sum is divisible by 3. We multiply this by the number of subsets from Group 0 (since all subsets of Group 0 are valid) to get the final result.

This approach efficiently computes the result with a time complexity of O(n), making it suitable for large input sizes. The use of modulo operations ensures that the result remains within the bounds of standard integer limits.

杭州经纬计算机系统工程有限公司

杭州经纬计算机系统工程有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?