试填法,LeetCode 3145. 大数组元素的乘积

一、题目

1、题目描述

一个非负整数 x 的 强数组 指的是满足元素为 2 的幂且元素总和为 x 的最短有序数组。下表说明了如何确定 强数组 的示例。可以证明,x 对应的强数组是独一无二的。

数字二进制表示强数组
100001[1]
801000[8]
1001010[2, 8]
1301101[1, 4, 8]
2310111[1, 2, 4, 16]

我们将每一个升序的正整数 i (即1,2,3等等)的 强数组 连接得到数组 big_nums ,big_nums 开始部分为 [1, 2, 1, 2, 4, 1, 4, 2, 4, 1, 2, 4, 8, ...] 。

给你一个二维整数数组 queries ,其中 queries[i] = [fromi, toi, modi] ,你需要计算 (big_nums[fromi] * big_nums[fromi + 1] * ... * big_nums[toi]) % modi 。

请你返回一个整数数组 answer ,其中 answer[i] 是第 i 个查询的答案。

2、接口描述

python3
 ​
class Solution:
    def findProductsOfElements(self, queries: List[List[int]]) -> List[int]:



cpp
 ​
class Solution {
public:
    vector<int> findProductsOfElements(vector<vector<long long>>& queries) {
        
    }
};
C#
 ​
public class Solution {
    public int[] FindProductsOfElements(long[][] queries) {

    }
}

3、原题链接

3145. 大数组元素的乘积


二、解题报告

1、思路分析

对于询问 <l, r, p> ,答案为 2 ^ { Σ cnti } % p,cnti 代表 bignum[i] 对应的二进制位

令 f(n) 代表 [0, n - 1] 所有数字 popcount 之和

那么 f(2^i) = i * 2 ^ {i- 1},考虑每一位都是出现2 ^ {i - 1]次

sum(n) 代表 [0, n - 1] 所有数字二进制位为1 的位下标之和

sum(2^i) = (i - 1) * i / 2 * 2 ^ {i - 1}

我们试填法,从高到低填,能填1就填1,边填边计数

对于第0位,由于 0 - 1 = -1,我们单独处理即可

2、复杂度

时间复杂度: O(qlogU)空间复杂度:O(1)

3、代码详解

python3
 ​
class Solution:
    def findProductsOfElements(self, queries: List[List[int]]) -> List[int]:
        def calc(k: int) -> int:
            res = n = cnt1 = sum_i = 0
            for i in range((k + 1).bit_length() - 1, 0, -1):
                c = cnt1 * ((n | (1 << i)) - n) + (1 << (i - 1)) * i
                if c <= k:
                    k -= c
                    res += sum_i * (1 << i) + i * (i - 1) * (1 << (i - 1)) // 2
                    sum_i += i  # 之前填的 1 的幂次之和
                    cnt1 += 1  # 之前填的 1 的个数
                    n |= 1 << i  # 填 1
            # (0 - 1) < 0,所以第0位单独计算
            if cnt1 <= k:
                k -= cnt1
                res += sum_i
                n += 1  # 填 1
            # 剩余的 k 个幂次,由 n 的低 k 个 1 补充
            while k:
                lb = n & -n
                res += lb.bit_length() - 1
                n ^= lb
                k -= 1
            return res
        return [pow(2, calc(r + 1) - calc(l), mod) for l, r, mod in queries]
cpp
 ​
using i64 = long long;

int power(int a, i64 b, int p) {
    int res = 1 % p;
    for (; b; b /= 2, a = 1LL * a * a % p) 
        if (b & 1)
            res = 1LL * res * a % p;
    return res;
}

class Solution {
public:
    vector<int> findProductsOfElements(vector<vector<long long>>& queries) {
        auto calc = [](i64 k) -> i64{
            i64 res = 0, cnt1 = 0, sum_i = 0, n = 0;
            for (i64 i = __lg(k + 1); i; i--) {
                i64 c = (cnt1 << i) + (i << (i - 1)); 
                if (c <= k) {
                    k -= c;
                    res += (sum_i << i) + ((i * (i - 1) / 2) << (i - 1));
                    sum_i += i; 
                    cnt1++; 
                    n |= 1LL << i; 
                }
            }

            
            if (cnt1 <= k) {
                k -= cnt1;
                res += sum_i;
                n |= 1;
            }

            while (k --) {
                res += __builtin_ctzll(n);
                n &= n - 1;
            }
            
            return res;
        };

        std::vector<int> res;
        
        for (auto &q : queries) {
            i64 l = q[0], r = q[1];
            int p = q[2];
            res.push_back(power(2, calc(r + 1) - calc(l), p));
        }

        return res;
    }
};
C#
 ​
public class Solution {
    public int[] FindProductsOfElements(long[][] queries) {
        int[] res = new int[queries.Length];
        for (int i = 0; i < queries.Length; ++ i) 
            res[i] = power(2, calc(queries[i][1] + 1) - calc(queries[i][0]), (int)queries[i][2]);
        return res;
    }

    public int power(int a, long b, int p) {
        int res = 1 % p;
        for (; b > 0; b /= 2) {
            if (b % 2 == 1)
                res = (int)(1L * res * a % p);
            a = (int)(1L * a * a % p);
        }
        return res;
    }
    
    public long calc(long k) {
        long res = 0, sum_i = 0, cnt1 = 0, n = 0;
        for (int i = BitOperations.Log2((ulong)(k + 1)); i > 0; -- i) {
            long c = (cnt1 << i) + ((long)i << (i - 1));
            if (c <= k) {
                k -= c;
                res += (sum_i << i) + (((long)i * (i - 1) / 2) << (i - 1));
                sum_i += i;
                ++ cnt1;
                n |= 1L << i;
            }
        }

        if (cnt1 <= k) {
            k -= cnt1;
            res += sum_i;
            n |= 1;
        }

        while (k -- > 0) {
            res += BitOperations.TrailingZeroCount((ulong)n);
            n &= n - 1;
        }
        
        Console.WriteLine(res);

        return res;
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值