力扣2438.二的幂数组中查询范围内的乘积
-
lowbit求所有2的幂
- accumulate函数(begin,end,start,way)
- 求和/积的方式
- 求积并取模
-
const int N = 1e9 + 7;class Solution {public:int lowbit(int x){return x & -x;}vector<int> productQueries(int n, vector<vector<int>>& queries) {vector<int> powers;while(n){int t = lowbit(n);n -= t;powers.emplace_back(t);}vector<int> res;for(auto v:queries){int l = v[0];int r = v[1];//accumulate的用法int product = accumulate(powers.begin() + l,powers.begin() + r + 1, 1 ,[](int x,int y) {return (long long)x*y %N ;});res.emplace_back(product);}return res;}};