description
stringlengths
35
9.39k
solution
stringlengths
7
465k
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
n,q = map(int,raw_input().split()) s = raw_input() x = [int(s[i]) for i in range(n)] p = [1] c = 1 MOD = 10**9+7 for i in range(1,10**5+1): c = (c*2)%MOD p.append(c) cum_sum = [0]*(n+1) c = 0 for i in range(n): c += x[i] cum_sum[i+1] = c for i in range(q): l,r = map(int,raw_input().split()) n1 = cum_sum[r] - cum_sum[l-1] n2 = r+1-l-n1 # print n1,n2 ans = (p[n1]-1)*(p[n2])%MOD print ans
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; inline long long int __gcd(long long int a, long long int b); void pdash(int n = 1); int bitcount(long long int u); long long int power(long long int x, long long int y); long long int power(long long int x, long long int y, long long int z); long long int modInverse(long long int n, long long int p); long long int nCrF(long long int n, long long int r, long long int p); void cordinate_compression(vector<int>& v); void make_unique(vector<int>& vec); const long long int mod = 1e9 + 7; void solve() { int n, q; cin >> n >> q; string str; cin >> str; vector<int> vec(n); for (int i = 0; i < n; i++) { vec[i] = (str[i] - '0'); } for (int i = 1; i < n; i++) { vec[i] += vec[i - 1]; } while (q--) { int zero, one; int a, b; cin >> a >> b; a--; b--; one = vec[b] - (((a - 1) >= 0) ? vec[a - 1] : 0); zero = (b - a) + 1 - one; cout << ((power(2, zero, mod) * ((power(2, one, mod) - 1 + mod) % mod)) % mod) << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } } inline long long int __gcd(long long int a, long long int b) { if (a == 0 || b == 0) { return max(a, b); } long long int tempa, tempb; while (1) { if (a % b == 0) return b; else { tempa = a; tempb = b; a = tempb; b = tempa % tempb; } } } void pdash(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < 30; j++) { cout << "-"; } cout << "\n"; } } long long int power(long long int x, long long int y) { long long int result = 1; while (y > 0) { if (y & 1) { result = (result * x); } y = y >> 1; x = (x * x); } return result; } long long int power(long long int x, long long int y, long long int z) { long long int result = 1; x = x % z; while (y > 0) { if (y & 1) { result = (result * x) % z; } y = y >> 1; x = (x * x) % z; } return result; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p); } long long int nCrF(long long int n, long long int r, long long int p) { if (r == 0) return 1; long long int f[n + 1]; f[0] = 1; for (long long int i = 1; i <= n; i++) f[i] = f[i - 1] * i % p; return (f[n] * modInverse(f[r], p) % p * modInverse(f[n - r], p) % p) % p; } void cordinate_compression(vector<int>& v) { vector<int> p = v; make_unique(p); for (int i = 0; i < (int)((v).size()); i++) v[i] = (int)(lower_bound(p.begin(), p.end(), v[i]) - p.begin()); } void make_unique(vector<int>& vec) { sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); } int bitcount(long long int u) { int cnt = 0; while (u) { u = u & (u - 1); cnt++; } return cnt; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const long long MOD = (long long)1e9 + 7; const long double PI = 3.141592653589793238462643383279502884197; long long fac[1] = {1}, inv[1] = {1}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long mp(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % MOD; a = a * a % MOD; b >>= 1; } return ret; } long long cmb(long long r, long long c) { return fac[r] * inv[c] % MOD * inv[r - c] % MOD; } priority_queue<int, vector<int>, greater<int>> pq; vector<int> v; char s[100002]; int psum[100002]; int main() { int n, m; scanf("%d %d", &n, &m); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { psum[i] = psum[i - 1] + s[i] - '0'; } while (m--) { int a, b; scanf("%d %d", &a, &b); long long aa = psum[b] - psum[a - 1]; long long bb = b - a + 1 - aa; if (aa == 0) { printf("0\n"); continue; } long long ans = 0; ans = mp(2, aa + bb) - 1 + MOD; ans = (ans - mp(2, bb) + 1) + MOD + MOD; printf("%lld\n", ans % MOD); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
from sys import stdin, stdout import cProfile, math from collections import Counter from bisect import bisect_left,bisect,bisect_right import itertools from copy import deepcopy from fractions import Fraction printHeap = str() memory_constrained = False P = 10**9+7 import sys sys.setrecursionlimit(10000000) class Operation: def __init__(self, name, function, function_on_equal, neutral_value=0): self.name = name self.f = function self.f_on_equal = function_on_equal def add_multiple(x, count): return x * count def min_multiple(x, count): return x def max_multiple(x, count): return x sum_operation = Operation("sum", sum, add_multiple, 0) min_operation = Operation("min", min, min_multiple, 1e9) max_operation = Operation("max", max, max_multiple, -1e9) class SegmentTree: def __init__(self, array, operations=[sum_operation, min_operation, max_operation]): self.array = array if type(operations) != list: raise TypeError("operations must be a list") self.operations = {} for op in operations: self.operations[op.name] = op self.root = SegmentTreeNode(0, len(array) - 1, self) def query(self, start, end, operation_name): if self.operations.get(operation_name) == None: raise Exception("This operation is not available") return self.root._query(start, end, self.operations[operation_name]) def summary(self): return self.root.values def update(self, position, value): self.root._update(position, value) def update_range(self, start, end, value): self.root._update_range(start, end, value) def __repr__(self): return self.root.__repr__() class SegmentTreeNode: def __init__(self, start, end, segment_tree): self.range = (start, end) self.parent_tree = segment_tree self.range_value = None self.values = {} self.left = None self.right = None if start == end: self._sync() return self.left = SegmentTreeNode(start, start + (end - start) // 2, segment_tree) self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end, segment_tree) self._sync() def _query(self, start, end, operation): if end < self.range[0] or start > self.range[1]: return None if start <= self.range[0] and self.range[1] <= end: return self.values[operation.name] self._push() left_res = self.left._query(start, end, operation) if self.left else None right_res = self.right._query(start, end, operation) if self.right else None if left_res is None: return right_res if right_res is None: return left_res return operation.f([left_res, right_res]) def _update(self, position, value): if position < self.range[0] or position > self.range[1]: return if position == self.range[0] and self.range[1] == position: self.parent_tree.array[position] = value self._sync() return self._push() self.left._update(position, value) self.right._update(position, value) self._sync() def _update_range(self, start, end, value): if end < self.range[0] or start > self.range[1]: return if start <= self.range[0] and self.range[1] <= end: self.range_value = value self._sync() return self._push() self.left._update_range(start, end, value) self.right._update_range(start, end, value) self._sync() def _sync(self): if self.range[0] == self.range[1]: for op in self.parent_tree.operations.values(): current_value = self.parent_tree.array[self.range[0]] if self.range_value is not None: current_value = self.range_value self.values[op.name] = op.f([current_value]) else: for op in self.parent_tree.operations.values(): result = op.f( [self.left.values[op.name], self.right.values[op.name]]) if self.range_value is not None: bound_length = self.range[1] - self.range[0] + 1 result = op.f_on_equal(self.range_value, bound_length) self.values[op.name] = result def _push(self): if self.range_value is None: return if self.left: self.left.range_value = self.range_value self.right.range_value = self.range_value self.left._sync() self.right._sync() self.range_value = None def __repr__(self): ans = "({}, {}): {}\n".format(self.range[0], self.range[1], self.values) if self.left: ans += self.left.__repr__() if self.right: ans += self.right.__repr__() return ans def display(string_to_print): stdout.write(str(string_to_print) + "\n") def primeFactors(n): #n**0.5 complex factors = dict() for i in range(2,math.ceil(math.sqrt(n))+1): while n % i== 0: if i in factors: factors[i]+=1 else: factors[i]=1 n = n // i if n>2: factors[n]=1 return (factors) def isprime(n): """Returns True if n is prime.""" if n < 4: return True if n % 2 == 0: return False if n % 3 == 0: return False i = 5 w = 2 while i * i <= n: if n % i == 0: return False i += w w = 6 - w return True def test_print(*args): if test: print(args) def display_list(list1, sep=" "): stdout.write(sep.join(map(str, list1)) + "\n") def get_int(): return int(stdin.readline().strip()) def get_tuple(): return map(int, stdin.readline().split()) def get_list(): return list(map(int, stdin.readline().split())) memory = dict() def clear_cache(): global memory memory = dict() def cached_fn(fn, *args): global memory if args in memory: return memory[args] else: result = fn(*args) memory[args] = result return result # ----------------------------------------------------------------------------------- MAIN PROGRAM TestCases = False test = False def main(): n, q = get_tuple() li = list(map(int,list(input()))) ones = [0] k = 0 for i in li: k+=i ones.append(k) for i in range(q): l,r = get_tuple() sm = ones[r]-ones[l-1] print(((pow(2,sm,P)-1)*pow(2,r-l+1-sm,P))%P) # --------------------------------------------------------------------------------------------- END if TestCases: for _ in range(get_int()): cProfile.run('main()') if test else main() else: cProfile.run('main()') if test else main()
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long poww(int n) { long long ans = 1; long long a = 2; while (n) { if (n & 1) ans *= a, ans %= 1000000000 + 7; a *= a, a %= 1000000000 + 7; n >>= 1; } return ans; } int main(void) { int n, m; string s; int p[100005]; cin >> n >> m; cin >> s; int x = 0; for (int i = 0; i < n; i++) { if (s[i] == '1') x++; p[i + 1] = x; } p[0] = 0; for (int i = 1; i <= m; i++) { int l, r; cin >> l >> r; int num1 = p[r] - p[l - 1]; int num0 = r - l + 1 - num1; int x = poww(num1) - 1; long long y = (long long)x * (poww(num0) - 1); y %= 1000000000 + 7; long long sum = x + y; sum %= 1000000000 + 7; cout << sum << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long zeros[100000 + 342], ones[100000 + 342]; long long add(long long a, long long b) { return (a + b) % mod; } long long sub(long long a, long long b) { return ((a % mod) - (b % mod) + mod) % mod; } long long mul(long long a, long long b) { return ((a % mod) * (b % mod)) % mod; } long long poww(long long a, long long b) { if (b == 0LL) { return 1; } long long aa = poww(a, b / 2LL); aa = mul(aa, aa); if (b % 2LL == 1LL) { aa = mul(aa, a); } return aa; } int main() { ios::sync_with_stdio(false); int n, q; string str; cin >> n >> q; cin >> str; for (int i = 0; i < str.length(); i++) { if (str[i] == '0') { zeros[i + 1]++; } else { ones[i + 1]++; } zeros[i + 1] += zeros[i]; ones[i + 1] += ones[i]; } while (q--) { int l, r; cin >> l >> r; long long no_of_ones = ones[r] - ones[l - 1]; long long no_of_zeros = zeros[r] - zeros[l - 1]; long long byone = poww(2LL, no_of_ones), byzero, mlplr, ans; byone = sub(byone, 1); byzero = byone; mlplr = sub(poww(2LL, no_of_zeros), 1); byzero = mul(byzero, mlplr); cout << add(byone, byzero) << "\n"; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = map(int, r[0].strip().split()) s = list(map(int, r[1].strip())) p = [0] for i in range(n): p.append(p[i] + int(s[i])) for k in range(q): a, b = map(int, r[k + 2].strip().split()) l = b - a + 1 one = p[b] - p[a - 1] zero = l - one sys.stdout.write(str(((pow(2, one, M) - 1) * pow(2, zero, M)) % M) + "\n")
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
from sys import stdin, stdout n,q = tuple(map(int,stdin.readline().split())) arr = list(str(stdin.readline()))[:-1] arr[0] = int(arr[0]) for i in range(1,n): arr[i] = int(arr[i]) + arr[i-1] arr.insert(0,0) mod = int(1e9+7) inputs = [ tuple(map(int,line.split())) for line in stdin ] ans = [] for l,r in inputs: o = (arr[r] - arr[l-1]) z = (r - l + 1 - o) e_o = (pow(2,o,mod) - 1)%mod e_z = e_o * (pow(2,z,mod)-1)%mod ans.append(str(int((e_o + e_z)%mod))) stdout.write("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import os import sys def init_input(): it = iter(os.read(sys.stdin.fileno(), 10 ** 7).split()) return lambda: next(it).decode(), lambda: int(next(it)), lambda: float(next(it)) ns, ni, nf = init_input() MOD = 10 ** 9 + 7 n, q = ni(), ni() s = 'x' + ns() c = [0] * (n + 1) for i in range(1, n + 1): c[i] = c[i - 1] + (s[i] == '1') p2 = [1] * (2 * n + 1) for i in range(1, 2 * n + 1): p2[i] = p2[i - 1] * 2 % MOD out = [] for qq in range(q): l, r = ni(), ni() o = c[r] - c[l - 1] z = (r - l + 1) - o ans = (p2[o + z] - 1 - p2[z] + 1) % MOD out.append(str(ans)) sys.stdout.write("\n".join(out))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#!/usr/bin/env python """ This file is part of https://github.com/Cheran-Senthil/PyRival. Copyright 2018 Cheran Senthilkumar all rights reserved, Cheran Senthilkumar <hello@cheran.io> Permission to use, modify, and distribute this software is given under the terms of the MIT License. """ from __future__ import division, print_function import cmath import itertools import math import operator as op # import random import sys from atexit import register from bisect import bisect_left, bisect_right # from collections import Counter, MutableSequence, defaultdict, deque # from copy import deepcopy # from decimal import Decimal # from difflib import SequenceMatcher # from fractions import Fraction # from heapq import heappop, heappush if sys.version_info[0] < 3: # from cPickle import dumps from io import BytesIO as stream # from Queue import PriorityQueue, Queue else: # from functools import reduce from io import StringIO as stream from math import gcd # from pickle import dumps # from queue import PriorityQueue, Queue if sys.version_info[0] < 3: class dict(dict): """dict() -> new empty dictionary""" def items(self): """D.items() -> a set-like object providing a view on D's items""" return dict.iteritems(self) def keys(self): """D.keys() -> a set-like object providing a view on D's keys""" return dict.iterkeys(self) def values(self): """D.values() -> an object providing a view on D's values""" return dict.itervalues(self) def gcd(x, y): """gcd(x, y) -> int greatest common divisor of x and y """ while y: x, y = y, x % y return x input = raw_input range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip def sync_with_stdio(sync=True): """Set whether the standard Python streams are allowed to buffer their I/O. Args: sync (bool, optional): The new synchronization setting. """ global input, flush if sync: flush = sys.stdout.flush else: sys.stdin = stream(sys.stdin.read()) input = lambda: sys.stdin.readline().rstrip('\r\n') sys.stdout = stream() register(lambda: sys.__stdout__.write(sys.stdout.getvalue())) def main(): n, q = map(int, input().split()) d = input() one_cnt = [0] * (n + 1) for i in range(n): one_cnt[i] = one_cnt[i-1] if d[i] == '1': one_cnt[i] = one_cnt[i-1] + 1 for _ in range(q): l, r = map(int, input().split()) ones = one_cnt[r - 1] - one_cnt[l - 2] zeros = (r - (l - 1)) - ones one_sol = pow(2, ones, 1000000007) - 1 zero_sol = one_sol * (pow(2, zeros, 1000000007) - 1) print((one_sol + zero_sol) % 1000000007) if __name__ == '__main__': sync_with_stdio(False) if 'PyPy' in sys.version: from _continuation import continulet def bootstrap(c): callable, arg = c.switch() while True: to = continulet(lambda _, f, x: f(x), callable, arg) callable, arg = c.switch(to=to) c = continulet(bootstrap) c.switch() main() else: import threading sys.setrecursionlimit(2097152) threading.stack_size(134217728) main_thread = threading.Thread(target=main) main_thread.start() main_thread.join()
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Main { private static class Solution { int[] arr; int[] pre; int n; int[] p2; int mod = 1_000_000_000 + 7; public Solution(int[] arr) { this.arr = arr; n = arr.length - 1; pre = new int[n + 1]; for (int i = 1; i <= n; ++i) { pre[i] = pre[i - 1] + arr[i]; } p2 = new int[n + 1]; p2[0] = 1; for (int i = 1; i <= n; ++i) { p2[i] = (p2[i - 1] * 2) % mod; } } public int query(int low, int high) { int cnt1 = pre[high] - pre[low - 1]; if (cnt1 == 0) return 0; int cnt0 = high - low + 1 - cnt1; long ret = (long) ((p2[cnt1] - 1)) * p2[cnt0]; ret %= mod; if (ret < 0) ret += mod; return (int) ret; } } public static void main(String[] args) throws Exception { MyScanner in = new MyScanner(); int n = in.nextInt(); int q = in.nextInt(); String str = in.next(); int[] arr = new int[n + 1]; for (int i = 1; i <= n; ++i) { arr[i] = str.charAt(i - 1) - '0'; } Solution sol = new Solution(arr); StringBuilder sb = new StringBuilder(); while (q-- > 0) { int low = in.nextInt(); int high = in.nextInt(); sb.append(sol.query(low, high) + "\n"); } System.out.print(sb.toString()); } // -----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // -------------------------------------------------------- }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static Scanner in; // static StreamTokenizer in; // static int nextInt() throws IOException { // in.nextToken(); // return (int)in.nval; // } static Random rand = new Random(); static long gcd(long a, long c) { BigInteger q = BigInteger.valueOf(a); BigInteger w = BigInteger.valueOf(c); q = q.gcd(w); return q.longValue(); } static int[] w, d; public static void main(String args[]) throws IOException { // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int q = in.nextInt(); String s = in.next(); int[] a = new int[n]; long[] pow = new long[n]; pow[0] = 1; long two = 1; int mod = (int) (1e9+7); for(int i = 0;i<n;i++) { a[i] = s.charAt(i) -'0'; } for(int i=1;i<n;i++) { a[i] = a[i]+a[i-1]; two*=2; two%=mod; pow[i] = (pow[i-1] + two)%mod; } for(int h=0;h<q;h++) { int l = in.nextInt()-1; int r = in.nextInt()-1; int k =0; if(l==0) k = a[r]; else k = a[r] - a[l-1]; long ans = 0; if(r - k - l == -1) ans = pow[r-l]; else ans = (pow[r-l] - pow[r-k-l] +mod) % mod; out.println(ans); } out.close(); } } class Pair implements Comparable{ int a; int b; public Pair() { this.a = 0; this.b = 0; } public Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Object arg0) { if(Integer.compare(this.a, ((Pair) arg0).a) == 0) return Integer.compare(this.b, ((Pair) arg0).b); return Integer.compare(this.a, ((Pair) arg0).a); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long cnt[100002]; string s; long long pre[100002]; long long fi(long long a, long long n) { long long b = n - a; long long i, cn = 0, ans = 0; if (a == 0) return 0; ans = pre[b]; ans *= (pre[a] - 1); ans %= 1000000007; if (ans < 0) ans += 1000000007; return ans; } int main() { long long n, q, i, j; cin >> n >> q; pre[0] = 1; for (i = 1; i < 100001; i++) { pre[i] = (pre[i - 1]) * 2; pre[i] %= 1000000007; if (pre[i] < 1000000007) pre[i] += 1000000007; } cin >> s; for (i = 0; i < n; i++) { if (s[i] == '0') cnt[i + 1] = cnt[i]; else cnt[i + 1] = cnt[i] + 1; } while (q--) { long long l, r; cin >> l >> r; cout << fi(-cnt[l - 1] + cnt[r], r - l + 1) << endl; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
/* Rajkin Hossain */ import java.io.*; import java.util.*; public class C { static FastInput k = new FastInput(System.in); static FastOutput z = new FastOutput(); static int n, q; static char [] y; static Pair [] data; static long mod = (long) 1e9 + 7l; static long pow(long a, long b){ if(b == 0) return 1l % mod; long x = pow(a, b / 2); x = (x * x) % mod; if(b % 2 == 1) x = (x * a) % mod; return x; } static void startAlgorithm(){ while(q-->0){ int left = k.nextInt(); int right = k.nextInt(); Pair p2 = data[right]; Pair p1 = data[left-1]; long one = p2.one - p1.one; long zero = p2.zero - p1.zero; long oneSum = 0l, zeroSum = 0l; if(one % 2l == 0){ oneSum = pow(4l, one/2) - 1l; } else{ oneSum = (1l % mod) + ((2l % mod) * (pow(4l, (one-1)/2) - 1l)) % mod; oneSum %= mod; } if(zero % 2 == 0){ zeroSum = pow(4l, zero/2) - 1l; } else{ zeroSum = (1l % mod) + ((2l % mod) * (pow(4l, (zero-1)/2) - 1l)) % mod; zeroSum %= mod; } zeroSum = ((oneSum % mod) * (zeroSum % mod)) % mod; long ans = ((oneSum % mod) + (zeroSum % mod)) % mod; z.println(ans); } } public static void main(String[] args) throws Exception { while(k.hasNext()){ n = k.nextInt(); q = k.nextInt(); y = k.next().toCharArray(); data = new Pair[n+1]; data[0] = new Pair(0,0); for(int i = 1; i<=n; i++){ if(y[i-1] == '0'){ data[i] = new Pair(data[i-1].zero + 1, data[i-1].one); } else{ data[i] = new Pair(data[i-1].zero, data[i-1].one + 1); } } startAlgorithm(); } z.flush(); System.exit(0); } static class Pair { int zero, one; public Pair(int zero, int one) { this.zero = zero; this.one = one; } } public static class FastInput { BufferedReader reader; StringTokenizer tokenizer; public FastInput(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); } public FastInput(String path){ try { reader = new BufferedReader(new FileReader(path)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } tokenizer = null; } public String next() { return nextToken(); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public boolean hasNext(){ try { return reader.ready(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = null; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } public Integer nextInt() { return Integer.parseInt(nextToken()); } public Long nextLong() { return Long.valueOf(nextToken()); } } public static class FastOutput extends PrintWriter { public FastOutput() { super(new BufferedOutputStream(System.out)); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; public class ProblemC { public static final long M = 1_000_000_000 + 7; /* Iterative Function to calculate (x^y)%p in O(log y) */ static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1l) > 0) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); /* INPUT */ int n = scan.nextInt(), q = scan.nextInt(); String s = scan.next(); int[] accu = new int[n]; int curr = 0; for (int i = 0; i < n; i++) { int ai = s.charAt(i) - '0'; curr += ai; accu[i] = curr; } // System.out.println(Arrays.toString(accu)); long[] pow2 = new long[n + 1]; pow2[0] = 1; long p2 = 1; for (int i = 1; i <= n; i++) { p2 *= 2; p2 %= M; pow2[i] = p2; } // System.out.println(Arrays.toString(pow2)); StringBuilder res = new StringBuilder(); while (q-- > 0) { int l = scan.nextInt() - 1, r = scan.nextInt() - 1; int len = r - l + 1; int ones = accu[r]; if (l > 0) ones -= accu[l - 1]; int zero = len - ones; // System.out.println(ones + " 1:0 " + zero); long onesMax = power(2l, ones - 1, M); // System.out.println(onesMax); // a * (1 - r^n)/ ( 1 - r) // r = 2 long onesSum = pow2[ones]; //power(2l, ones, M); onesSum--; if (onesSum <= 0) onesSum += M; // onesSum * (1 - r ^2) / -1 long zeroSum = pow2[zero]; //power(2l, zero, M); zeroSum--; if (zeroSum <= 0) onesSum += M; zeroSum *= onesSum; zeroSum %= M; res.append(((onesSum + zeroSum) % M)).append("\n"); } System.out.println(res); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; const long long mo = 1e9 + 7; int n, m, k; long long a[maxn], sum[maxn]; long long c[maxn]; long long ans, ct, cnt, tmp, flag; char s[maxn]; long long power(long long a, long long n) { long long ans = 1; a = a % mo; while (n) { if (n & 1) ans = (ans * a) % mo; n >>= 1; a = (a * a) % mo; } return ans; } int main() { int T, cas = 1; while (scanf("%d%d", &n, &m) != EOF) { ans = 0; flag = 1; memset(c, 0, sizeof(c)); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { if (s[i] == '0') c[i] = c[i - 1]; else c[i] = c[i - 1] + 1; } while (m--) { long long x, y; scanf("%lld%lld", &x, &y); if (c[y] - c[x - 1] == 0) { puts("0"); continue; } long long k = c[y] - c[x - 1]; long long tp = (power(2, k) - 1 + mo) % mo; long long kk = (y - x + 1 - k); long long tmp = (tp * (power(2, kk) - 1 + mo) % mo) % mo; ans = (tmp + tp) % mo; printf("%lld\n", ans); } } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split() n = int(n) q = int(q) s = r[1] p = [0] k = 0 for v in range(n): d = int(s[v]) k += d p.append(k) ans = [] for k in range(q): a, b = r[k + 2].strip().split() a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] ans.append((pow(2, l, M) - pow(2, l - one, M) + M) % M) sys.stdout.write("\n".join(map(str, ans)))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; int s[100001]; long long p[100001]; long long mod = 1e9 + 7; int cnt(int left, int right) { return s[right] - s[left - 1]; } int main() { int n, q; cin >> n >> q; string a; cin >> a; s[1] = (int)(a[0] - '0'); for (int i = 1; i < a.length(); i++) { s[i + 1] = s[i] + (a[i] == '1'); } p[0] = 1; for (int i = 1; i <= 100000; i++) { p[i] = p[i - 1] * 2 % mod; } while (q--) { int left, right; cin >> left >> right; int one = cnt(left, right); int zero = right - left + 1 - one; cout << ((p[one] - 1) * p[zero]) % mod << "\n"; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
from sys import stdin, stdout from itertools import repeat def main(): n, q = map(int, stdin.readline().split()) s = stdin.readline().strip() dat = map(int, stdin.read().split(), repeat(10, 2 * q)) a = [0] * (n + 1) for i, x in enumerate(s): a[i+1] = a[i] + (x == '1') ans = [0] * q mod = 1000000007 for i in xrange(q): l, r = dat[i*2] - 1, dat[i*2+1] o = a[r] - a[l] z = r - l - o ans[i] = ((pow(2, o, mod) - 1) * pow(2, z, mod)) % mod stdout.write('\n'.join(map(str, ans))) main()
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; using namespace std; string s; long long tree_bit[200100]; int lim; void update_bit(int idx, int val) { while (idx <= lim) { tree_bit[idx] += val; if (tree_bit[idx] >= 1000000007) tree_bit[idx] %= 1000000007; idx += (idx & -idx); } } long long query_bit(int idx) { if (!idx) return 0; long long sum = 0; while (idx != 0) { sum += tree_bit[idx]; idx -= (idx & -idx); if (sum >= 1000000007) sum %= 1000000007; } return sum; } long long pw2[100100]; long long get_progression(long long a, long long pod) { if (a >= 1000000007) a %= 1000000007; long long ret = a * (pw2[pod] - 1); if (ret >= 1000000007) ret %= 1000000007; return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; pw2[0] = 1; for (int i = 1; i <= 100010; i++) { pw2[i] = (pw2[i - 1] << 1); if (pw2[i] >= 1000000007) pw2[i] %= 1000000007; } cin >> n >> q; cin >> s; lim = n; for (int i = 0; i < n; i++) { if (s[i] == '1') { update_bit(i + 1, 1); } } while (q--) { int l, r; cin >> l >> r; l--; int range = r - l; int ones = query_bit(r) - query_bit(l); long long ans = 0; ans = get_progression(1, ones); ans += get_progression(ans, range - ones); if (ans >= 1000000007) ans %= 1000000007; cout << ans << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; int delta[4][2] = {{-1, -0}, {0, 1}, {1, 0}, {0, -1}}; long long calc_pow(long long a, long long p, long long m = 1000000007) { long long res = 1; while (p > 0) { if (p & 1) res = (res * a) % m; a = (a * a) % m; p >>= 1; } return res; } long long calc_pow_without_mod(long long a, long long p) { long long res = 1; while (p > 0) { if (p & 1) res = res * a; a = a * a; p >>= 1; } return res; } void func() { long long n, q; cin >> n >> q; string s; cin >> s; char arr[n + 1]; for (int i = 0; i < n; i++) arr[i + 1] = s[i]; long long pre[n + 1]; for (int i = 0; i <= n; i++) pre[i] = 0; for (int i = 1; i <= (n); i++) { pre[i] = pre[i - 1]; if (arr[i] == '1') pre[i]++; } for (int i = 1; i <= (q); i++) { int l, r; cin >> l >> r; long long ones = pre[r] - pre[l - 1]; long long zeroes = r - l + 1 - ones; long long val = (calc_pow(2, ones) - 1 + 1000000007) % 1000000007; long long new_val = (calc_pow(2, zeroes) - 1 + 1000000007) % 1000000007; new_val = (new_val * val) % 1000000007; cout << (val + new_val) % 1000000007 << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); func(); return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; int main() { long long n, q; cin >> n >> q; vector<long long> powe(n + 1, 0); powe[0] = 1; for (long long i = 1; i <= n; i++) powe[i] = (powe[i - 1] * 2) % 1000000007; vector<long long> A(n, 0), pre(n, 0); string s; cin >> s; for (long long i = 0; i < n; i++) if (s[i] == '1') A[i] = 1; for (long long i = 0; i < n; i++) pre[i] = A[i] + (i - 1 >= 0 ? pre[i - 1] : 0); for (long long i = 0; i < q; i++) { long long l, r; cin >> l >> r; l--; r--; long long num = r - l + 1, one = pre[r] - (l - 1 >= 0 ? pre[l - 1] : 0); long long zer = num - one; long long val = powe[one] - 1; long long val2 = powe[zer] - 1; val = ((val + ((val * val2) % 1000000007) % 1000000007) % 1000000007 + 2 * 1000000007) % 1000000007; cout << val << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int MAX = 100005; const int INF = 0x3f3f3f3f; const int mod = 1000000007; string str; long long n, q; long long l, r; long long cum[100005]; long long p[100005]; void make() { for (int i = 1; i <= n; i++) { if (str[i - 1] == '1') cum[i] = cum[i - 1] + 1; else cum[i] = cum[i - 1]; } p[0] = 0; p[1] = 1; long long temp = 2; for (long long i = 2; i < 100005; i++) { temp *= 2; temp %= mod; p[i] = temp - 1; } } int main() { cin >> n >> q; cin >> str; make(); long long n1, n0; long long temp1, temp2, ans; while (q--) { cin >> l >> r; n1 = cum[r] - cum[l - 1]; n0 = r - l + 1 - n1; temp1 = p[n1]; temp2 = p[n0]; temp2 *= temp1; temp2 %= mod; ans = temp1 + temp2; ans %= mod; cout << ans << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction from collections import defaultdict BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n,m=map(int,input().split()) t=list(map(int,list(input()))) p=[0]+t[:] mod=10**9+7 o=[] for i in range(n): p[i+1]+=p[i] for _ in range(m): l,r=map(int,input().split()) a=p[r]-p[l-1] b=(r-l+1)-a o.append(str((pow(2,a+b,mod)-pow(2,b,mod))%mod)) print('\n'.join(map(str,o)))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
n,q=map(int,input().split()) a=list(map(int,list(input()))) p=[0]+a[:] M=10**9+7 for i in range(1,n+1):p[i]+=p[i-1] o=[] for _ in range(q): l,r=map(int,input().split()) w=p[r]-p[l-1] z=(r-l+1)-w o.append(str((pow(2,w+z,M)-pow(2,z,M))%M)) print('\n'.join(map(str,o)))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18; const int N = 1e6 + 10, M = 2e6 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f; long long a[N], sum[N]; long long q_pow(long long a, long long k) { long long s = 1; while (k) { if (k & 1) s = s * a % mod; a = a * a % mod; k >>= 1; } return s; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int _; _ = 1; while (_--) { long long n, m, inv2 = q_pow(2, mod - 2); string s; cin >> n >> m >> s; s = " " + s; for (int i = 1; (i) <= (n); i++) sum[i] = sum[i - 1] + s[i] - '0'; while (m--) { long long l, r; cin >> l >> r; long long sum1 = sum[r] - sum[l - 1]; if (sum1 == 0) { cout << 0 << '\n'; continue; } long long sum0 = r - l + 1 - sum1; long long ans = q_pow(2, sum1) - 1; long long now = ans; ans = (ans + now * (q_pow(2, sum0) - 1) % mod) % mod; cout << ans << '\n'; } } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; namespace fast_IO { inline int read_int() { register int ret = 0, f = 1; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = (ret << 1) + (ret << 3) + int(c - 48); c = getchar(); } return ret * f; } } // namespace fast_IO using namespace fast_IO; int N, Q; namespace BIT { int c[200005]; inline int lowbit(int x) { return x & (-x); } inline void add(int pos, int w) { while (pos <= N) c[pos] += w, pos += lowbit(pos); } inline int query(int pos) { int res = 0; while (pos) res += c[pos], pos -= lowbit(pos); return res; } } // namespace BIT long long w[200005]; char s[200005]; inline void init() { N = read_int(), Q = read_int(); scanf("%s", s + 1); for (register int i = 1; i <= N; i++) BIT::add(i, s[i] - '0'); w[1] = 1; for (register int i = 2; i <= N + 1; i++) w[i] = w[i - 1] << 1, w[i] %= 1000000007; for (register int i = 1; i <= N + 1; i++) w[i] += w[i - 1], w[i] %= 1000000007; for (register int i = 1; i <= N + 1; i++) w[i] += w[i - 1], w[i] %= 1000000007; } inline void calc() { register int l, r; register int zero, one; while (Q--) { l = read_int(), r = read_int(); one = BIT::query(r) - BIT::query(l - 1); zero = r - l + 1 - one; int len = r - l + 1; long long ans = w[len - 1] - w[len - one - 1] + one; ans = (ans % 1000000007 + 1000000007) % 1000000007; printf("%lld\n", ans); } } int main() { init(); calc(); return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.*; import java.util.*; public class Main { public static long MOD = 1000_000_007; public static void solve(FastIO io) { int n = io.nextInt(); int q = io.nextInt(); String parts = io.nextLine(); int[] ones = new int[n+1]; for(int i = 1; i <= n; ++i){ if(parts.charAt(i-1) == '1') ones[i] = 1; } for(int i = 1; i <= n; ++i) ones[i] += ones[i-1]; for(int i = 0, l, r; i < q; ++i){ l = io.nextInt(); r = io.nextInt(); int oCnt = ones[r] - ones[l-1], zCnt = r-l+1 - oCnt; if(oCnt > 0) { long r1 = (binPow(2, oCnt, MOD) + MOD - 1)%MOD; long r2 = r1 * ((binPow(2, zCnt, MOD) + MOD - 1)%MOD) %MOD; io.println((r1 + (zCnt > 0 ? r2 : 0))%MOD); continue; } io.println(0); } } public static long binPow(long a, long b, long m){ a %= m; long res = 1; while (b > 0) { if ((b & 1) != 0) res = res * a % m; a = a * a % m; b >>= 1; } return res; } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) throws FileNotFoundException { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
from sys import stdin out = [] n,q = map(int, raw_input().split()) s = raw_input() pref = [0] for i in s: if i == '0': pref.append(pref[-1]) else: pref.append(pref[-1]+1) MOD = pow(10,9)+7 for line in stdin.readlines(): l,r = map(int, line.split()) num1 = pref[r] - pref[l-1] num0 = r - l + 1 - num1 k = num0 n = num1 out.append(str((pow(2,n,MOD)-1 + (pow(2,n, MOD)-1)*(pow(2,k,MOD)-1))%MOD)) print('\n'.join(out))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import math if __name__ == '__main__': n,q = [int(x) for x in input().split()] qq = str(input()) s = [ int(x) for x in qq] prefix = [0]*n prefix[0]= s[0] temp = [0]*(n+1) temp[0]=1 mod = (pow(10,9)//1)+7 for i in range(1,n): prefix[i] += prefix[i-1] + s[i] temp[i] =( 2*(temp[i-1]%mod) )%mod temp[n] = (2*(temp[n-1]%mod))%mod ansarray=[] while q> 0: q-=1 l,r = [int(x)-1 for x in input().split()] a = prefix[r]-prefix[l]+s[l] d = r-l+1 val1 = temp[d] val2 = temp[d-a] # val2 = val2%mod ansarray.append((val1-val2)%mod) print(*ansarray)
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions,statistics from collections import deque,defaultdict,OrderedDict,Counter from fractions import Fraction from decimal import Decimal from sys import stdout from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max,nsmallest,nlargest def main(): mod=1000000007 f=[1] t=[1] for i in range(1,100005): t.append((t[-1]*2)%mod) f.append((f[-1]+t[i])%mod) # InverseofNumber(mod) # InverseofFactorial(mod) # factorial(mod) starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") tc=1 for _ in range(tc): n,q=ria() s=rs() cs1=[0] cs0=[0] ns1=0 ns0=0 for i in range(n): if s[i]=='1': ns1+=1 else: ns0+=1 cs1.append(ns1) cs0.append(ns0) for i in range(q): l,r=ria() n1q=cs1[r]-cs1[l-1] n0q=cs0[r]-cs0[l-1] ans=0 if n1q>0: ans+=f[n1q-1] if n0q>0: ans+=(t[n1q]-1)*f[n0q-1] wi(ans%mod) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main()
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 5; const int MOD = 1e9 + 7; string a; long long sum[MAX_N]; long long fpow(long long a, long long b) { long long aa = 1; while (b > 0) { if (b & 1) aa = aa * a % MOD; a = a * a % MOD; b /= 2; } return aa; } int main() { long long n, m; cin >> n >> m; cin >> a; sum[0] = 0; for (int i = 1; i <= a.length(); i++) { sum[i] = sum[i - 1]; if (a[i - 1] == '1') sum[i]++; } long long p, q; while (m--) { cin >> p >> q; long long cn1 = sum[q] - sum[p - 1]; long long cn0 = q - p + 1 - cn1; long long ans = 0; ans = fpow(2, cn1) - 1; if (ans < 0) ans += MOD; ans = ans * fpow(2, cn0) % MOD; cout << ans << endl; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; import java.io.*; import java.math.*; // Solution to PCS Problem 89: Banh Mi // https://pcs.cs.cloud.vt.edu/contests/89/problems/C // Solution by: jLink23 public class BanhMi { private static PrintWriter out; public static final int MOD = 1000000007; public static void main(String[] args) { out = new PrintWriter(System.out); FastScanner in = new FastScanner(); int n = in.nextInt(); int q = in.nextInt(); String banhmi = in.next(); // Create prefix sum array int[] prefix = new int[n + 1]; for (int i = 1; i <= n; i++) { if (banhmi.charAt(i-1) == '0') { prefix[i] = prefix[i-1]; } else { prefix[i] = prefix[i-1] + 1; } } // Precompute powers of 2 for quicker access Long[] pow2 = new Long[100001]; pow2[0] = 1L; for (int i = 1; i <= 100000; i++) { pow2[i] = pow2[i-1] * 2L % MOD; } for (int i = 0; i < q; i++) { int l = in.nextInt(); int r = in.nextInt(); int k = r-l+1; // Length of segment int x = prefix[r] - prefix[l-1]; // Number of 1's int y = k - x; // Number of 0's out.println(((pow2[x] - 1) * pow2[y]) % MOD); } out.flush(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] a = new int[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } return a; } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e5 + 15; const int MOD = int(1e9) + 7; int n, q; vector<long long> ans; int one_cnt[MAXN]; long long bin_pow[MAXN]; long long bin_pref[MAXN]; void in() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> q; ans.resize(q + 5); string s; cin >> s; one_cnt[1] = (s[0] == '1'); for (int i = 1; i < n; ++i) { one_cnt[i + 1] = one_cnt[i] + (s[i] == '1'); } bin_pow[0] = 1; bin_pref[0] = 1; for (int i = 1; i <= n; ++i) { bin_pow[i] = (bin_pow[i - 1] * 2) % MOD; bin_pref[i] = (bin_pref[i - 1] + bin_pow[i]) % MOD; } for (int i = 0; i < q; ++i) { int L, R; cin >> L >> R; int one = one_cnt[R] - one_cnt[L - 1]; int nul_cnt = (R - L + 1) - one; long long answer = bin_pref[one - 1]; long long tmp = (bin_pref[one - 1] * bin_pref[nul_cnt - 1]) % MOD; answer = (answer + tmp) % MOD; ans[i] = answer; } } void out() { for (int i = 0; i < q; ++i) { cout << ans[i] << "\n"; } } int main() { in(); out(); return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
###### ### ####### ####### ## # ##### ### ##### # # # # # # # # # # # # # ### # # # # # # # # # # # # # ### ###### ######### # # # # # # ######### # ###### ######### # # # # # # ######### # # # # # # # # # # # #### # # # # # # # # # # ## # # # # # ###### # # ####### ####### # # ##### # # # # from __future__ import print_function # for PyPy2 # from itertools import permutations # from functools import cmp_to_key # for adding custom comparator # from fractions import Fraction from collections import * from sys import stdin from bisect import * from heapq import * from math import log2 g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] rr = lambda x : reversed(range(x)) mod = int(1e9)+7 inf = float("inf") n, q = gil() a = gbs() sp = [[0 for _ in range(n)] for _ in range(int(log2(n))+1)] sp[0] = a m = len(sp) for i in range(1, m): d = 1<<(i-1) for j in range(n): sp[i][j] = sp[i-1][j] + (sp[i-1][j+d] if j+d < n else 0) def getSum(l, r): ans = 0 while l <= r: bt = int(log2(r-l+1)) ans += sp[bt][l] delta = (1<<bt) l += delta return ans for _ in range(q): l, r = gil() one = getSum(l-1, r-1) zero = r-l+1 - one ans = ((pow(2, one, mod) - 1 + mod)%mod)*pow(2, zero, mod) if ans >= mod: ans %= mod print(ans)
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; import java.io.*; public class Main { static int n, q; static boolean[] arr; static query[] qs; static long[] out, pows; static long mod = (long)(1e9 + 7), modi = 500000004L, ans; public static void main (String[] args) throws IOException { //pre comp pows of two mod pows = new long[100_000]; pows[0] = 1; for(int i = 1; i < 100_000; ++i){ pows[i] = (pows[i - 1] * 2) % mod; } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); String[] nq = br.readLine().split(" "); n = p(nq[0]); q = p(nq[1]); arr = new boolean[n]; qs = new query[q]; out = new long[q]; char[] in = br.readLine().toCharArray(); for(int i = 0; i < n; ++i) arr[i] = in[i] == '1'; for(int i = 0; i < q; ++i){ String[] curr = br.readLine().split(" "); int ll = p(curr[0]) - 1, rr = p(curr[1]) - 1; qs[i] = new query(ll, rr, i); } Arrays.sort(qs); int cl = 0, cr = 0; ans = (arr[0] ? 1 : 0); for(int i = 0; i < q; ++i){ int l = qs[i].l, r = qs[i].r; while(cl > l){ --cl; if(!arr[cl]) mul2(); else add(cr - cl + 1); } while(cr < r){ ++cr; if(!arr[cr]) mul2(); else add(cr - cl + 1); } while(cl < l){ if(!arr[cl]) div2(); else sub(cr - cl + 1); ++cl; } while(cr > r){ if(!arr[cr]) div2(); else sub(cr - cl + 1); --cr; } out[qs[i].id] = ans; } for(int i = 0; i < q; ++i) pw.println(out[i]); pw.close(); } static void add(int size){ ans = (ans + pows[size - 1]) % mod; } static void sub(int size){ ans -= pows[size - 1]; if(ans < 0) ans += mod; } static void mul2(){ ans = (ans * 2) % mod; } static void div2(){ ans = (ans * modi) % mod; } static int p(String in){ return Integer.parseInt(in); } static class query implements Comparable<query> { int l, r, id; query(int ll, int rr, int ii){ l = ll; r = rr; id = ii; } public int compareTo(query in){ int lbuck1 = (int)(l / Math.sqrt(n)); int lbuck2 = (int)(in.l / Math.sqrt(n)); if(lbuck1 != lbuck2) return lbuck1 - lbuck2; if(lbuck1 % 2 == 0) return r - in.r; return in.r - r; } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.InputMismatchException; public class C1062 { static class Solver { int N, Q, cs[]; long del[], sum[], MOD = 1_000_000_007; long add(long a, long b) { return (a + b) % MOD; } long mul(long a, long b) { return (a * b) % MOD; } long pow(long b, long e) { if (e == 0) return 1; if (e == 1) return b; long sq = pow(b, e >> 1); sq = mul(sq, sq); return (e & 1) == 0 ? sq : mul(sq, b); } void solve(int testNumber, FastScanner s, PrintWriter out) { N = s.nextInt(); Q = s.nextInt(); cs = new int[N + 1]; char[] ch = s.next().toCharArray(); for (int i = 1; i <= N; i++) cs[i] += cs[i - 1] + ch[i - 1] - '0'; while (Q-- > 0) { int l = s.nextInt(), r = s.nextInt(), len = r - l + 1; long z = cs[r] - cs[l - 1], o = len - z; long oz = add(MOD - 1, pow(2, z)); // ones long tot = add(oz, mul(oz, add(MOD - 1, pow(2, o)))); out.println(tot); } } } final static boolean cases = false; public static void main(String[] args) { FastScanner s = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(); for (int t = 1, T = cases ? s.nextInt() : 1; t <= T; t++) solver.solve(t, s, out); out.close(); } static int min(int a, int b) { return a < b ? a : b; } static int max(int a, int b) { return a > b ? a : b; } static long min(long a, long b) { return a < b ? a : b; } static long max(long a, long b) { return a > b ? a : b; } static int swap(int a, int b) { return a; } static Object swap(Object a, Object b) { return a; } static String ts(Object... o) { return Arrays.deepToString(o); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } public FastScanner(File f) throws FileNotFoundException { this(new FileInputStream(f)); } public FastScanner(String s) { this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } // Jacob Garbage public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } public long[] nextLongArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = this.nextLong(); return ret; } public long[][] next2DLongArray(int N, int M) { long[][] ret = new long[N][]; for (int i = 0; i < N; i++) ret[i] = nextLongArray(M); return ret; } public double[] nextDoubleArray(int N) { double[] ret = new double[N]; for (int i = 0; i < N; i++) ret[i] = this.nextDouble(); return ret; } public double[][] next2DDoubleArray(int N, int M) { double[][] ret = new double[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextDoubleArray(M); return ret; } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; import java.io.*; public class Main { public static int gcd(int a , int b) { int divs=0; int divd=0; if(a>=b) { divd=a; divs=b; } else { divd=b; divs=a; } int rem=divd%divs; while(rem!=0) { int temp=divs; divs=rem; divd=temp; rem=divd%divs; } return divs; } static class spComp implements Comparator<long[]> { public int compare(long[] a, long[] b) { if(a[0]>b[0]) return 1; else if(a[0]<b[0]) return -1; else if(a[1]>b[1]) return 1; else if(a[1]>b[1]) return -1; else return 1; } } class unionFind { int parent[]; int height[]; int size=0; unionFind(int n) { size=n; parent = new int[n]; height = new int[n]; // Arrays.fill(parent, -1); for(int i=0;i<n;i++) parent[i]=i; } int find(int a) { if(parent[a]==a) return a; parent[a]=find(a); return parent[a]; } void add(int a, int b) { a = find(a); b = find(b); if(height[a] > height[b]) { parent[b] = a; } else if(height[ b ] > height[ a ]) parent[ a ] = b; else if(height[ a ] == height[ b ]) { height[ a ] = height[ a ]+1; parent[ b ] = a; } } } static ArrayList<Integer> largestpow(int val, ArrayList<Integer> primes) { int n=primes.size(); ArrayList<Integer> powers= new ArrayList<Integer>(); for(int i=0;i<n;i++) { int p=primes.get(i); while(val%p==0) { p=p*p; } powers.add(p); } return powers; } public static int twoPow(int a) { int val=1; int ans=0; if(a==1) return 1; while(val<a) { val*=2; ans++; } if(val==a) return ans; else return ans; // return ans; } static long pow2(int a) { long modd=1000000007; long ans=1; for(int i=0;i<a;i++) { ans*=2; ans%=modd; } return ans; } public static void main(String args[]) { Scanner sc= new Scanner(System.in); int n= sc.nextInt(); int q= sc.nextInt(); int arr[]= new int[n]; String str= sc.next(); for(int i=0;i<n;i++) { arr[i]= str.charAt(i)-'0'; } int zeros[]= new int[n]; int ones[]= new int[n]; long modd= 1000000007; long powers[]= new long[n+1]; for(int i=0;i<=n;i++) { if(i==0) powers[i]=1; else powers[i]= (powers[i-1]*2)%modd; } for(int i=0;i<n;i++) { if(i==0) { if(arr[i]==0) zeros[i]=1; if(arr[i]==1) ones[i]=1; } else { if(arr[i]==0) { zeros[i]=zeros[i-1]+1; ones[i]=ones[i-1]; } if(arr[i]==1) { ones[i]= ones[i-1]+1; zeros[i]=zeros[i-1]; } } } for(int i=0;i<q;i++) { int l= sc.nextInt()-1; int r= sc.nextInt()-1; if(l!=0) { int zer=zeros[r]-zeros[l-1]; int one= ones[r]-ones[l-1]; long ans= (powers[one+zer]-powers[zer] + modd)%modd; System.out.println(ans); } else { int zer=zeros[r]; int one= ones[r]; long ans= (powers[one+zer]-powers[zer] + modd)%modd; System.out.println(ans); } } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; long double PI = 4 * atan(1); long long n, q, dp[100001], po[100001], x, cur, ans, l, r; string second; void init() { po[0] = 1; for (int i = 1; i < 100001; i++) { po[i] = (po[i - 1] * 2LL) % MOD; } } int main() { ios::sync_with_stdio(0), cin.tie(0); init(); cin >> n >> q >> second; for (int i = 1; i < n + 1; i++) { if (second[i - 1] == '1') { dp[i] = 1; } dp[i] += dp[i - 1]; } while (q--) { cin >> l >> r; x = dp[r] - dp[l - 1]; ans = po[x] - 1; if (ans < 0) ans += MOD; cur = po[r - l + 1 - x] - 1; if (cur < 0) cur += MOD; cur = (ans * cur) % MOD; ans = (ans + cur) % MOD; cout << ans << "\n"; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class codeforces { static class Student{ int x,y; Student(int x,int y){ this.x=x; this.y=y; //this.z=z; } } static int prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. int pos=0; prime= new int[n+1]; for(int p = 2; p*p <=n; p++) { // If prime[p] is not changed, then it is a prime if(prime[p] == 0) { // Update all multiples of p prime[p]=p; for(int i = p*p; i <= n; i += p) prime[i] = p; } } } static class Sortbyroll implements Comparator<Student> { // Used for sorting in ascending order of // roll number public int compare(Student c, Student b) { return c.x-b.x; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Edge{ int a,b; Edge(int a,int b){ this.a=a; this.b=b; } } static class Trie{ HashMap<Character,Trie>map; int c; Trie(){ map=new HashMap<>(); //z=null; //o=null; c=0; } } //static long ans; static int parent[]; static int rank[]; static int b[][]; static int bo[]; static int ho[]; static int seg[]; //static int pos; // static long mod=1000000007; //static int dp[][]; static HashMap<Integer,Integer>map; static PriorityQueue<Student>q=new PriorityQueue<>(); //static Stack<Integer>st; // static ArrayList<Character>ans; static ArrayList<ArrayList<Integer>>adj; //static long ans; static Trie root; static long fac[]; static int gw,gb; static long mod=(long)(1000000007); //static int ans; static void solve()throws IOException{ FastReader sc=new FastReader(); int n,q,l,r,o,z,i; long ans,t; n=sc.nextInt(); q=sc.nextInt(); String s=sc.nextLine(); int a[]=new int[n+1]; for(i=1;i<=n;i++){ a[i]=s.charAt(i-1)-'0'; a[i]+=a[i-1]; } StringBuilder sb=new StringBuilder(); for(i=0;i<q;i++){ l=sc.nextInt(); r=sc.nextInt(); o=a[r]-a[l-1]; z=r-l+1-o; t=0; ans=0; t=(power(2,o,mod)-(long)1+mod)%mod; ans+=t; t=(t*(power(2,z,mod)-(long)1+mod)%mod)%mod; ans=(ans+t)%mod; sb.append(ans+"\n"); } System.out.println(sb.toString()); } static long nCr(long n, long r, long p) { return (fac[(int)n]* modInverse(fac[(int)r], p) % p * modInverse(fac[(int)(n-r)], p) % p) % p; } //static int prime[]; //static int dp[]; public static void main(String[] args){ //long sum=0; try { codeforces.solve(); } catch (Exception e) { e.printStackTrace(); } } static long modInverse(long n, long p) { return power(n, p-(long)2,p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y %(long)2!=0) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res%p; } /*public static long power(long x,long a) { if(a==0) return 1; if(a%2==1)return (x*1L*power(x,a-1))%mod; return (power((int)((x*1L*x)%mod),a/2))%mod; }*/ static int find(int x) { // Finds the representative of the set // that x is an element of while(parent[x]!=x) { // if x is not the parent of itself // Then x is not the representative of // his set, x=parent[x]; // so we recursively call Find on its parent // and move i's node directly under the // representative of this set } return x; } static void union(int x, int y) { // Find representatives of two sets int xRoot = find(x), yRoot = find(y); // Elements are in the same set, no need // to unite anything. if (xRoot == yRoot) return; // If x's rank is less than y's rank if (rank[xRoot] < rank[yRoot]) // Then move x under y so that depth // of tree remains less parent[xRoot] = yRoot; // Else if y's rank is less than x's rank else if (rank[yRoot] < rank[xRoot]) // Then move y under x so that depth of // tree remains less parent[yRoot] = xRoot; else // if ranks are the same { // Then move y under x (doesn't matter // which one goes where) parent[yRoot] = xRoot; // And increment the the result tree's // rank by 1 rank[xRoot] = rank[xRoot] + 1; } } static long gcd(long a, long b) { if (a == 0){ //ans+=b; return b; } return gcd(b % a, a); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import org.omg.CORBA.INTERNAL; import javax.swing.plaf.basic.BasicTreeUI; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { private static Scanner sc; private static Printer pr; static boolean []visited; static int []color; static int []pow=new int[(int)3e5+1]; static int []count; static boolean check=true; static boolean checkBip=true; static TreeSet<Integer>[]list; private static long aLong=(long)(Math.pow(10,9)+7); static final long div=998244353; static int answer=0; static int[]countSubTree; static int[]colorNode; static boolean con=true; static ArrayList<Integer>dfsPath=new ArrayList<>(); static ArrayList<Integer>ans=new ArrayList<>(); static boolean[]rec; static int min=Integer.MAX_VALUE; static ArrayList<Task>tasks=new ArrayList<>(); static boolean []checkpath; private static void solve() throws IOException { int n=sc.nextInt(),q=sc.nextInt(); int []sum=new int[n+1]; StringBuilder builder=new StringBuilder(sc.next()); if (builder.charAt(0)=='1')sum[1]=1; for (int i=1;i<builder.length();++i){ if (builder.charAt(i)=='1') sum[i+1]=sum[i]+1; else sum[i+1]=sum[i]; } //System.out.println("sum:"+Arrays.toString(sum)); while (q-->0){ int l=sc.nextInt(),r=sc.nextInt(); int one=sum[r]-sum[l-1]; int zero=(r-l+1)-one; pr.println(((power(2,one,aLong)-1)*power(2,zero,aLong))%aLong); } } public static class dsu{ int []rank; int[]parent; long []sum; int n; public dsu(int n) { this.n = n; parent=new int[n]; rank=new int[n]; sum=new long[n]; for (int i=0;i<n;++i){ rank[i]=1; parent[i]=i; sum[i]=0; } } public int dad(int child){ if (parent[child]==child)return child; else return dad(parent[child]); } public void merge(int u,int v){ int dadU=dad(u); int dadV=dad(v); if (dadU==dadV)return ; if (u!=v){ if (rank[u]<rank[v]){ parent[u]=v; sum[v]+=sum[u]; } else if (rank[u]==rank[v]){ parent[u]=v; sum[v]+=sum[u]; } else{ parent[v]=u; sum[u]+=sum[v]; } } } } public static long power(long x,long y,long mods){ if (y==0) return 1%mods; long u=power(x,y/2,mods); u=((u)*(u))%mods; if (y%2==1) u=(u*(x%mods))%mods; return u; } public static class Task implements Comparable<Task>{ long l,r; public Task(long w,long v) { this.l=w; this.r=v; } @Override public int compareTo(Task o) { if (o.l<this.l)return 1; else if (o.l>this.l)return -1; else { if (o.r>this.r)return 1; else if (o.r<this.r)return -1; } return 0; } } public static void printSolve(StringBuilder str, int[] colors, int n,int color){ for(int i = 1;i<=n;++i) if(colors[i] == color) str.append(i + " "); str.append('\n'); } public static class pairTask{ int val; int size; public pairTask(int x, int y) { this.val = x; this.size = y; } } public static void subTree(int src,int parent,int x){ countSubTree[src]=1; if (src==x) { checkpath[src]=true; //System.out.println("src:"+src); } else checkpath[src]=false; for (int v:list[src]){ if (v==parent) continue; subTree(v,src,x); countSubTree[src]+=countSubTree[v]; checkpath[src]|=checkpath[v]; } } public static boolean prime(long src){ for (int i=2;i*i<=src;i++){ if (src%i==0) return false; } return true; } public static void bfsColor(int src){ Queue<Integer>queue = new ArrayDeque<>(); queue.add(src); while (!queue.isEmpty()){ boolean b=false; int vertex=-1,p=-1; int poll=queue.remove(); for (int v:list[poll]){ if (color [v]==0){ vertex=v; break; } } for (int v:list[poll]){ if (color [v]!=0&&v!=vertex){ p=v; break; } } for (int v:list[p]){ if (color [v]!=0){ color[vertex]=color[v]; b=true; break; } } if (!b){ color[vertex]=color[poll]+1; } } } static int add(int a,int b ){ if (a+b>=div) return (int)(a+b-div); return (int)a+b; } public static int isBipartite(ArrayList<Integer>[]list,int src){ color[src]=0; Queue<Integer>queue=new LinkedList<>(); int []ans={0,0}; queue.add(src); while (!queue.isEmpty()){ ans[color[src=queue.poll()]]++; for (int v:list[src]){ if (color[v]==-1){ queue.add(v); color[v]=color[src]^1; }else if (color[v]==color[src]) check=false; } } return add(pow[ans[0]],pow[ans[1]]); } public static int powerMod(long b, long e){ long ans=1; while (e-->0){ ans=ans*b%div; } return (int)ans; } public static int dfs(int s){ int ans=1; visited[s]=true; for (int k:list[s]){ if (!visited[k]){ ans+=dfs(k); } } return ans; } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } public static long []primeFactor(int n){ long []prime=new long[n+1]; prime[1]=1; for (int i=2;i<=n;i++) prime[i]=((i&1)==0)?2:i; for (int i=3;i*i<=n;i++){ if (prime[i]==i){ for (int j=i*i;j<=n;j+=i){ if (prime[j]==j) prime[j]=i; } } } return prime; } public static StringBuilder binaryradix(long number){ StringBuilder builder=new StringBuilder(); long remainder; while (number!=0) { remainder = number % 2; number >>= 1; builder.append(remainder); } builder.reverse(); return builder; } public static int binarySearch(long[] a, int index,long target) { int l = index; int h = a.length - 1; while (l<=h) { int med = l + (h-l)/2; if(a[med] - target <= target) { l = med + 1; } else h = med - 1; } return h; } public static int val(char c){ return c-'0'; } public static long gcd(long a,long b) { if (a == 0) return b; return gcd(b % a, a); } private static class Pair implements Comparable<Pair> { long x; long y; Pair() { this.x = 0; this.y = 0; } Pair(long x, long y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) return false; Pair other = (Pair) obj; if (this.x == other.x && this.y == other.y) { return true; } return false; } @Override public int compareTo(Pair other) { if (this.x != other.x) return Long.compare(this.x, other.x); return Long.compare(this.y*other.x, this.x*other.y); } } public static void main(String[] args) throws IOException { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); // sc.close(); } private static class Scanner { BufferedReader br; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } private boolean isPrintable(int ch) { return ch >= '!' && ch <= '~'; } private boolean isCRLF(int ch) { return ch == '\n' || ch == '\r' || ch == -1; } private int nextPrintable() { try { int ch; while (!isPrintable(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } return ch; } catch (IOException e) { throw new NoSuchElementException(); } } String next() { try { int ch = nextPrintable(); StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (isPrintable(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } int nextInt() { try { // parseInt from Integer.parseInt() boolean negative = false; int res = 0; int limit = -Integer.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } int multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } long nextLong() { try { // parseLong from Long.parseLong() boolean negative = false; long res = 0; long limit = -Long.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Long.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } long multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { int ch; while (isCRLF(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (!isCRLF(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } void close() { try { br.close(); } catch (IOException e) { // throw new NoSuchElementException(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class List { String Word; int length; List(String Word, int length) { this.Word = Word; this.length = length; } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys i = int r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split() n = i(n) q = i(q) s = r[1] p = [0] k = 0 for v in range(n): d = i(s[v]) k += (1 - d) p.append(k) ans = [] for k in range(q): a, b = r[k + 2].strip().split() a = i(a) b = i(b) l = b - a + 1 zero = p[b] - p[a - 1] ans.append(str((pow(2, l, M) - pow(2, zero, M) + M) % M)) sys.stdout.write("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.net.Inet4Address; import java.util.*; import java.io.*; public class Did_I_Deserve_It{ public static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st1 = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st1.nextToken()); int q = Integer.parseInt(st1.nextToken()); String ss = br.readLine(); point arr[] = new point[n]; long zeros = 0l, ones = 0l; for(int i = 0 ; i < ss.length() ; i++) { if(ss.charAt(i) == '1') ones+=1l; else zeros+=1l; arr[i] = new point(zeros , ones); } while(q-- > 0) { st1 = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st1.nextToken())-1; int r = Integer.parseInt(st1.nextToken())-1; ones = arr[r].ones - arr[l].ones; zeros = arr[r].zeros - arr[l].zeros; if(ss.charAt(l) == '0') zeros+=1l; else ones+=1l; long sum = (long)(power(2,ones,1000000007)-1l)*(power(2,zeros,1000000007)); out.println(sum % (1000000007)); } out.flush(); out.close(); } static class point{ long zeros , ones; public point(long z , long o) { zeros = z; ones = o; } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(0); if ("" == "input") { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } else if ("" != "") { freopen( "" ".in", "r", stdin); freopen( "" ".out", "w", stdout); } int t = 1; if (0) { cin >> t; } while (t--) solve(); return 0; } const int mod = (int)1e9 + 7; void solve() { int n, q; cin >> n >> q; string s; cin >> s; vector<long long> p(n + 1); for (int i = 0; i < n; ++i) { p[i + 1] = p[i] + (s[i] - '0'); } vector<long long> pw(n + 1); pw[0] = 1; for (int i = 1; i <= n; ++i) { pw[i] = 2ll * pw[i - 1] % mod; } vector<long long> G(n + 1); G[1] = 1; for (int i = 2; i <= n; ++i) { G[i] = (pw[i] - 1 + mod) % mod; } for (int i = 0; i <= n; ++i) { 42; } while (q--) { int l, r; cin >> l >> r; --l, --r; int N = r - l + 1; int cnt = p[r + 1] - p[l]; 42; cout << (G[N] - 1ll * G[N - cnt] % mod + mod) % mod << "\n"; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long power(long long n) { if (n == 0) return 1; long long a = power(n / 2); a *= a; a %= 1000000007; if (n % 2) a *= 2; a %= 1000000007; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; string s; cin >> s; int pre[n + 1][2]; pre[0][0] = 0; pre[0][1] = 0; for (int i = int(1); i <= int(n); i++) { if (s[i - 1] == '1') { pre[i][0] = pre[i - 1][0]; pre[i][1] = pre[i - 1][1] + 1; } else { pre[i][0] = pre[i - 1][0] + 1; pre[i][1] = pre[i - 1][1]; } } while (q--) { int l, r; cin >> l >> r; long long ct1 = pre[r][1] - pre[l - 1][1]; long long ct2 = pre[r][0] - pre[l - 1][0]; long long val1 = power(ct1) - 1; val1 += 1000000007; val1 %= 1000000007; long long val2 = power(ct2) - 1; val2 += 1000000007; val2 %= 1000000007; long long ans = 0; ans += val1; val2 *= val1; val2 %= 1000000007; ans += val2; ans %= 1000000007; cout << ans << "\n"; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { if (v.size() == 0) { os << "empty vector\n"; return os; } for (auto element : v) os << element << " "; return os; } template <typename T, typename second> ostream& operator<<(ostream& os, pair<T, second>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream& operator<<(ostream& os, set<T>& v) { if (v.size() == 0) { os << "empty set\n"; return os; } auto endit = v.end(); endit--; os << "["; for (auto it = v.begin(); it != v.end(); it++) { os << *it; if (it != endit) os << ", "; } os << "]"; return os; } template <typename T> ostream& operator<<(ostream& os, multiset<T>& v) { if (v.size() == 0) { os << "empty multiset\n"; return os; } auto endit = v.end(); endit--; os << "["; for (auto it = v.begin(); it != v.end(); it++) { os << *it; if (it != endit) os << ", "; } os << "]"; return os; } template <typename T, typename second> ostream& operator<<(ostream& os, map<T, second>& v) { if (v.size() == 0) { os << "empty map\n"; return os; } auto endit = v.end(); endit--; os << "{"; for (auto it = v.begin(); it != v.end(); it++) { os << "(" << (*it).first << " : " << (*it).second << ")"; if (it != endit) os << ", "; } os << "}"; return os; } template <typename T> ostream& operator<<(ostream& os, vector<vector<T>>& v) { for (auto& subv : v) { for (auto& e : subv) os << e << " "; os << "\n"; } return os; } bool do_debug = false; vector<long long> fact(200001), ifact(200001); long long power(long long x, long long n) { if (!n) return 1; if (n % 2) return (x * (power((x * x) % 1000000007, n / 2) % 1000000007)) % 1000000007; return power((x * x) % 1000000007, n / 2) % 1000000007; } long long inverse(long long n) { return power(n, 1000000007 - 2) % 1000000007; } void pre_factorials() { fact[0] = 1; for (long long i = 1; i < 200001; i++) fact[i] = (fact[i - 1] * i) % 1000000007; for (long long i = 0; i < 200001; i++) ifact[i] = inverse(i); } long long ncr(long long n, long long r) { if (n < r) return 0; else if (n == r) return 1; else if (r == 0) return 0; long long ans = fact[n]; ans = (ans * ifact[r]) % 1000000007; ans = (ans * ifact[n - r]) % 1000000007; return ans; } void Runtime_Terror() { long long n, q; cin >> n >> q; string s; cin >> s; vector<long long> v(n); for (long long i = 0; i < n; i++) v[i] = s[i] - '0'; vector<pair<long long, long long>> cnt(n); if (v[0] == 0) cnt[0] = {1, 0}; else cnt[0] = {0, 1}; for (long long i = 1; i < n; i++) { if (v[i] == 0) cnt[i] = {cnt[i - 1].first + 1, cnt[i - 1].second}; else cnt[i] = {cnt[i - 1].first, cnt[i - 1].second + 1}; } for (long long i = 0; i < q; i++) { long long l, r; cin >> l >> r; if (l == 1) { long long one = cnt[r - 1].second; long long zero = cnt[r - 1].first; long long ans = 0; long long tem = ((power(2, one) - 1) % 1000000007 + 1000000007) % 1000000007; ans += tem; long long dtem = tem; tem *= ((power(2, zero) - 1) % 1000000007 + 1000000007) % 1000000007; tem %= 1000000007; ans += tem; ans %= 1000000007; cout << ans << endl; } else { long long one = cnt[r - 1].second - cnt[l - 2].second; long long zero = cnt[r - 1].first - cnt[l - 2].first; long long ans = 0; long long tem = ((power(2, one) - 1) % 1000000007 + 1000000007) % 1000000007; ans += tem; long long dtem = tem; tem *= ((power(2, zero) - 1) % 1000000007 + 1000000007) % 1000000007; tem %= 1000000007; ans += tem; ans %= 1000000007; cout << ans << endl; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; for (long long i = 0; i < t; i++) Runtime_Terror(); return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long n, i, j, ans = 0; long long M = (long long)1E9 + 7; long long a[1000000], zero[1000000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long sum = 0; a[1] = 1, a[2] = 2; sum = 3; for (i = 3; i <= 100000 + 5; i++) { a[i] = (1 + sum + M) % M; sum += a[i]; sum = (sum + M) % M; } for (i = 1; i <= 100000 + 5; i++) a[i] = (a[i] + a[i - 1] + M) % M; long long q, l, r; cin >> n >> q; string s; cin >> s; s = "k" + s; for (i = 1; i <= n; i++) zero[i] = zero[i - 1] + (s[i] == '0'); while (q--) { cin >> l >> r; long long x = zero[r] - zero[l - 1]; long long ans = a[r - l + 1] % M - (a[x]) % M; cout << (ans + M) % M << endl; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 5; int pre[N], pw[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); long long n, q, l, r, a, b; char ch; cin >> n >> q; int sum = 0, temp, ans; pw[0] = 1, pre[0] = 0; for (int i = 1; i <= n; i++) { cin >> ch; pre[i] += (pre[i - 1] + (ch == '0')); pw[i] = ((pw[i - 1] << 1) % 1000000007); } for (int i = 0; i < q; i++) { cin >> l >> r; b = pre[r] - pre[l - 1]; a = r - l + 1 - b; ans = (1ll * pw[a] - 1ll) * 1ll * pw[b] % 1000000007; cout << ans << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; import java.io.*; import java.math.*; public class Main { static void precompute_pow(long arr[],int n){ arr[0]=1l; for(int i=1;i<=n;i++) arr[i]=(arr[i-1]*2l)%mod; } public static void process()throws IOException { int n=ni(),q=ni(); long pow[]=new long[n+1]; char arr[]=(" "+nln()).toCharArray(); precompute_pow(pow,n); int pref_1[]=new int[n+1],pref_0[]=new int[n+1]; for(int i=1;i<=n;i++){ pref_1[i]=pref_1[i-1]+(arr[i]-'0'); pref_0[i]=pref_0[i-1]+(1-(arr[i]-'0')); } while(q-- > 0){ int l=ni(),r=ni(); long res=pow[pref_1[r]-pref_1[l-1]]-1l; res%=mod; res=res+res*(pow[pref_0[r]-pref_0[l-1]]-1l)%mod; res%=mod; pn(res); } } static FastReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new FastReader(); long s = System.currentTimeMillis(); int t=1; //t=ni(); while(t-->0) process(); out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static int ni()throws IOException{return Integer.parseInt(sc.next());} static long nl()throws IOException{return Long.parseLong(sc.next());} static double nd()throws IOException{return Double.parseDouble(sc.next());} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static long mod=(long)1e9+7l; static<T> void r_sort(T arr[],int n){ Random r = new Random(); for (int i = n-1; i > 0; i--){ int j = r.nextInt(i+1); T temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } Arrays.sort(arr); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 1; long long l, r, n, q, p[MAXN], a[MAXN], k, m, mod = 1e9 + 7; string s; int main() { cin >> n >> q >> s; for (int i = 0; i < n; i++) { a[i + 1] += a[i]; if (s[i] == '1') a[i + 1]++; } p[0] = 1; for (int i = 1; i <= 1e5; i++) p[i] = (p[i - 1] * 2) % mod; for (int i = 0; i < q; i++) { cin >> l >> r; cout << (p[r - l + 1] - p[r - l + 1 - a[r] + a[l - 1]] + mod) % mod << "\n"; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int maxN = 1e6 + 5, MOD = 1e9 + 7, eps = 1e-7, INF = 1e9; const double PI = acos(-1); int n, q, a, b; string str; int acum[maxN]; long long mulmod(long long a, long long b) { long long ret = 0; while (b) { if (b & 1) ret = (ret + a) % MOD; b >>= 1, a = (a << 1) % MOD; } return ret; } long long fastPow(long long x, long long n) { long long ret = 1; while (n) { if (n & 1) ret = ret * x % MOD; n >>= 1, x = x * x % MOD; } return ret; } int main() { cin >> n >> q >> str; acum[0] = 0; for (int i = int(0); i < int(n); i++) { char c = str[i]; if (c == '0') { acum[i + 1] = acum[i]; } else { acum[i + 1] = acum[i] + 1; } } for (int i = int(0); i < int(q); i++) { cin >> a >> b; long long tot = b - a + 1, ceros = tot - (acum[b] - acum[a - 1]); long long ans = (fastPow(2, tot) + MOD - 1) % MOD; ans -= (fastPow(2, ceros) + MOD - 1) % MOD; ans += MOD; ans %= MOD; cout << ans << '\n'; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.Scanner; public class Main { private static long MOD = 1000000007; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(), q = s.nextInt(), curSum = 0; s.nextLine(); String str = s.nextLine(); long[] pows = new long[100001]; pows[0] = 1; for (int i = 1; i <= 100000; i++) { pows[i] = (pows[i - 1] << 1) % MOD; } int[] prefixSums = new int[n]; for (int i = 0; i < n; i++) { if (str.charAt(i) == '1') { curSum++; } prefixSums[i] = curSum; } for (int i = 0; i < q; i++) { int l = s.nextInt() - 1, r = s.nextInt() - 1; int numOnes = prefixSums[r]; if (l > 0) { numOnes -= prefixSums[l - 1]; } int numZeros = (r - l + 1) - numOnes; long num = (pows[numOnes] - 1) * pows[numZeros]; num %= MOD; System.out.println(num); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int mod= 1000000007; int n = ni(), Q = ni(); char[] s = ns(n); int[] ones = new int[n+1]; int[] zeros = new int[n+1]; for(int i = 0; i < n; i++) { ones[i+1] = ones[i] + (s[i] == '0' ? 0 : 1); zeros[i+1] = zeros[i] + (s[i] == '0' ? 1 : 0); } for(int q = 0; q < Q; q++) { int l = ni(), r = ni(); int o = ones[r] - ones[l-1]; int z = zeros[r] - zeros[l-1]; long ans = pow(2, o, mod) - 1; ans = ans * pow(2, z, mod); out.println(ans % mod); } } public static long pow(long a, long n, long mod) { // a %= mod; if(n < 0) return 0; long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.InputMismatchException; public class Q3 { public static int mod = (int) 1e9 + 7; public static long[] pow = new long[100003]; public static void main(String[] args) { InputReader s = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = 1; // t = s.nextInt(); pow[0] = 1; for (int i = 1; i < 100003; i++) { pow[i] = pow[i - 1] * 2; if (pow[i] >= mod) { pow[i] -= mod; } } nexttest: while (t-- > 0) { int n = s.nextInt(); int m = s.nextInt(); char[] xx = s.nextLine().toCharArray(); int[] a = new int[n]; for (int i = 0; i < n; i++) { if (xx[i] == '1') { a[i] = 1; } } Query[] q = new Query[m]; for (int i = 0; i < m; i++) { q[i] = new Query(s.nextInt() - 1, s.nextInt() - 1); } long[] res = processQueries(a, q); printArray(res, out); } out.close(); } public static class Query { int index; int a; int b; public Query(int a, int b) { this.a = a; this.b = b; } } static int count = 0; static long invmod = pow(2, mod - 2, mod); static long add(int[] a, int i, long cur) { count++; if (a[i] == 1) { return (cur + pow[count - 1]) >= mod ? cur + pow[count - 1] - mod : cur + pow[count - 1]; } return (cur * 2) >= mod ? cur * 2 - mod : cur * 2; } static long remove(int[] a, int i, long cur) { count--; if (a[i] == 1) { return (cur - pow[count]) < 0 ? (cur - pow[count] + mod) : (cur - pow[count]); } return cur * invmod % mod; } public static long[] processQueries(int[] a, Query[] queries) { for (int i = 0; i < queries.length; i++) { queries[i].index = i; } int sqrtn = (int) Math.sqrt(a.length); Arrays.sort(queries, Comparator.comparingInt((Query q) -> q.a / sqrtn).thenComparingInt(q -> q.b)); long[] res = new long[queries.length]; int L = 0; int R = 0; long cur = a[0]; count = 1; for (Query query : queries) { while (L > query.a) { cur = add(a, --L, cur); // System.out.println(2 +" " + cur); } while (R < query.b) { cur = add(a, ++R, cur); // System.out.println(3 + " " + cur); } while (L < query.a) { cur = remove(a, L++, cur); // System.out.println(1 + " " + cur); } while (R > query.b) { cur = remove(a, R--, cur); // System.out.println(4 + " " + cur); } res[query.index] = cur; } return res; } public static void printArray(long[] a, PrintWriter out) { for (int i = 0; i < a.length; i++) { out.println(a[i]); } } public static int pow(long x, long n, int mod) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) { if ((n & 1) != 0) { res = (res * p % mod); } } return (int) res; } static long gcd(long n1, long n2) { long r; while (n2 != 0) { r = n1 % n2; n1 = n2; n2 = r; } return n1; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) { throw new InputMismatchException(); } if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int MAXN = 1e5, MOD = 1e9 + 7; int n, q; string s; vector<int> pow2(MAXN + 1); int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); pow2[0] = 1; for (int i = 1; i <= MAXN; ++i) { pow2[i] = (pow2[i - 1] * 2) % MOD; } cin >> n >> q >> s; vector<pii> pref(n + 1); for (int i = 1; i <= n; ++i) { pref[i] = pref[i - 1]; if (s[i - 1] == '0') pref[i].second++; else pref[i].first++; } int l, r; for (int i = 0; i < q; ++i) { cin >> l >> r; ll res = ((ll)(pow2[pref[r].first - pref[l - 1].first] - 1) * pow2[pref[r].second - pref[l - 1].second]) % MOD; cout << res << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; const int mod = 1e9 + 7; inline pair<long long, long long> mp(long long a, long long b) { pair<long long, long long> temp; temp.first = a; temp.second = b; return temp; } long long read_int() { char r; bool start = false, neg = false; long long ret = 0; while (true) { r = getchar(); if ((r - '0' < 0 || r - '0' > 9) && r != '-' && !start) { continue; } if ((r - '0' < 0 || r - '0' > 9) && r != '-' && start) { break; } if (start) ret *= 10; start = true; if (r == '-') neg = true; else ret += r - '0'; } if (!neg) return ret; else return -ret; } long long p[N][2], comp[N]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, q; cin >> n >> q; string st; cin >> st; p[0][0] = p[0][1] = 0; for (long long i = 0; i < st.size(); i++) { if (i != 0) { p[i][0] = p[i - 1][0]; p[i][1] = p[i - 1][1]; } long long cur = st[i] - '0'; p[i][cur]++; } comp[0] = 1; for (long long i = 1; i < N; i++) { comp[i] = comp[i - 1] * 2; comp[i] %= mod; } while (q--) { long long l, r; cin >> l >> r; l--; r--; long long c0; if (l == 0) c0 = p[r][0]; else c0 = p[r][0] - p[l - 1][0]; long long ans = comp[r - l + 1] - comp[c0]; ans += mod; ans %= mod; cout << ans << endl; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys input_file = sys.stdin C = (10**9+7) [n, q] = list(int(i) for i in input_file.readline().split()) temp = input_file.readline() lst = [] for char in temp[:-1]: lst.append(int(char)) new_lst = [(0, 0)] for i in lst: if i == 0: new_lst.append((new_lst[-1][0]+1, new_lst[-1][1])) else: new_lst.append((new_lst[-1][0], new_lst[-1][1]+1)) ls = [1] for i in range(n): ls.append(ls[-1]*2 % C) for line in input_file: [l, r] = list(int(i) for i in line[:-1].split()) q = (new_lst[r][0] - new_lst[l-1][0], new_lst[r][1] - new_lst[l-1][1]) print((ls[sum(q)] - ls[q[0]]) % C)
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.Arrays; import java.util.ArrayList; import java.math.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class TestClass { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int testCount = 1; // testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskA { private final long MOD = 1000000007; public void solve(int testNumber, InputReader in, PrintWriter out) { long[] pows = new long[100001]; pows[0] = 1; for (int i = 1; i <= 100000; i++) { pows[i] = (pows[i - 1] << 1) % MOD; } int n = in.nextInt(); int q = in.nextInt(); String s = in.next(); int[] pre = new int[n+1]; pre[0] = 0; int cnt = 0; for(int i=0;i<n;i++) { if(s.charAt(i)=='1') cnt++; pre[i] = cnt; } for(int i=0;i<q;i++) { int l = in.nextInt()-1; int r = in.nextInt()-1; int ones = pre[r]; if (l > 0) { ones -= pre[l - 1]; } int zeroes = (r - l + 1) - ones; long ans = (pows[ones]-1)*(pows[zeroes]) ; ans%=MOD; out.println(ans); } } /* private long pow(long x, int n) { if (n == 0) { return 1; } long x2 = x * x % MOD; return pow(x2, n/2) * (n % 2 == 0 ? 1 : x) % MOD; } */ } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- mod=1000000007 n,m=map(int,input().split()) s=list(input()) l=[0]*(n+1) for i in range(1,n+1): l[i]=l[i-1]+int(s[i-1]) #print(l) for i in range(m): l1,r=map(int,input().split()) s=r-l1 no=l[r]-l[l1-1] ans=pow(2,s+1,mod)-pow(2,s-no+1,mod) print(ans%mod)
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> char s[100005]; int cnt[100005]; const long long mod = 1000000007; long long powm(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } int main() { int n, q, l, r, m, len; scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int i = 1; i <= n; i++) if (s[i] == '1') cnt[i] = cnt[i - 1] + 1; else cnt[i] = cnt[i - 1]; for (int i = 0; i < q; i++) { scanf("%d%d", &l, &r); m = cnt[r] - cnt[l - 1]; len = r - l + 1; printf("%I64d\n", (powm(2, m) + mod - 1) % mod * powm(2, len - m) % mod); } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; const int inf = 1e8; const int mod = 1e9 + 7; int n, t; int pre[maxn]; char s[maxn]; long long ksm(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res % mod; } int main() { cin >> n >> t; cin >> (s + 1); int len = strlen(s + 1); for (int i = 1; i <= len; ++i) { pre[i] = pre[i - 1] + (s[i] == '1'); } while (t--) { int l, r; cin >> l >> r; long long first = pre[r] - pre[l - 1], second = r - l + 1 - first; long long res = ksm(2, first) - 1; if (second >= 0) res += (ksm(2, second) - 1) % mod * res % mod; cout << res % mod << '\n'; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; int64_t const base = 1e9 + 7; int n, q, l, r, cnt, res; string s; int64_t f[100005], i_th[100005]; int main() { ios::sync_with_stdio(0); cin >> n >> q >> s; i_th[1] = 1; for (int i = 2; i <= 100003; ++i) i_th[i] = (i_th[i - 1] << 1) % base; for (int i = 0; i < s.size(); ++i) f[i + 1] = f[i] + s[i] - '0'; while (q--) { cin >> l >> r; cnt = f[r] - f[l - 1]; res = (base + i_th[cnt + 1] - 1) % base; res = ((i_th[r - l + 1 - cnt + 1]) * res) % base; cout << res << '\n'; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int N = 300000 + 10; const int mod = 1e9 + 7; int n, q; char s[N]; long long sum[N]; long long pw[N], fibo[N], pw2[N], pw3[N]; int main() { scanf("%d%d", &n, &q); scanf("%s", s + 1); for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + (s[i] == '1'); } for (int i = 1; i <= 2e5; ++i) { if (i == 1) fibo[i] = 1; else fibo[i] = fibo[i - 1] * 2; fibo[i] %= mod; pw[i] = pw[i - 1] + fibo[i]; pw[i] %= mod; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long num1 = sum[r] - sum[l - 1]; long long num0 = (r - l + 1) - num1; long long ans = pw[num1]; ans %= mod; if (num0) ans += pw[num1] * pw[num0] % mod; ans %= mod; printf("%lld\n", ans); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.*; import java.util.*; public class Main { private static final int MOD = 1000000007; private static int add(long a, long b) { return (int) ((a%MOD+b%MOD)%MOD); } private static int subtract(long a, long b) { return add(a, -b); } private static int multiply(long a, long b) { return (int) ((a%MOD*b%MOD)%MOD); } private static int power(long a, long b) { long c = 1; while(b > 0) { if((b&1) > 0) { c = multiply(c, a); } a = multiply(a, a); b >>= 1; } return (int) c; } public static void main(String[] args) throws IOException{ //Scanner f = new Scanner(new File("uva.in")); //Scanner f = new Scanner(System.in); //BufferedReader f = new BufferedReader(new FileReader("uva.in")); BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); int[] prefixSum = new int[n+1]; char[] s = f.readLine().toCharArray(); for(int i = 1; i <= n; i++) { prefixSum[i] = prefixSum[i-1]+(s[i-1] == '1' ? 1 : 0); } for(int i = 0; i < q; i++) { st = new StringTokenizer(f.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); out.println(multiply(subtract(power(2, prefixSum[r]-prefixSum[l-1]), 1), power(2, r-l-prefixSum[r]+prefixSum[l-1]+1))); } f.close(); out.close(); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; template <typename T> using V = vector<T>; template <typename T, typename U> using P = pair<T, U>; template <typename T> void cout_join(vector<T> &v, string d = " ") { for (int i = (0); i < (v.size()); ++i) { if (i > 0) cout << d; cout << v[i]; } cout << endl; } template <typename T> long long int power(T n, T p, T m) { if (p == 0) return 1LL; if (p == 1) return n; long long int k = power(n, p / 2, m); return ((k * k) % m * (p % 2 ? n : 1)) % m; } const long long int mod = 1e9 + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); long long int n, q; cin >> n >> q; string s; cin >> s; vector<long long int> sum(n + 1, 0); for (int i = (1); i <= (n); ++i) { sum[i] = sum[i - 1] + (s[i - 1] - '0'); } for (int i = (0); i < (q); ++i) { long long int l, r; cin >> l >> r; long long int cnt1 = sum[r] - sum[l - 1], cnt0 = r - (l - 1) - cnt1; long long int enj = (power(2LL, cnt1, mod) - 1 + mod) % mod; enj += (enj * ((power(2LL, cnt0, mod) - 1 + mod) % mod)) % mod; enj %= mod; cout << enj << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
MOD = 10 ** 9 + 7 MAXN = 10 ** 5 + 1 N, Q = map(int, raw_input().strip().split()) arr = map(int, [ i for i in raw_input().strip()]) # calculate prefix sum pre = [0 for _ in range(N+1)] for ind, val in enumerate(arr): pre[ind + 1] = pre[ind] + val # calculate powers of two pows = [1 for _ in range(MAXN)] for i in range(1, MAXN): pows[i] = (2 * pows[i - 1]) % MOD for _ in range(Q): L, R = map(int, raw_input().strip().split()) x = pre[R] - pre[L - 1] y = R - L + 1 - x result = (pows[y] * (pows[x] - 1)) % MOD print result
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].split(' ') n = int(n) q = int(q) s = r[1] p = [0] for v in range(n): p.append(p[v] + int(s[v])) ans = [] for k in range(q): a, b = r[k + 2].split(' ') a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] ans.append(str((pow(2, l, M) - pow(2, l - one, M) + M) % M)) sys.stdout.write("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.*; import java.util.*; public class Main { static class Reader { BufferedReader in; Reader() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); // initial size buffer C = 32768 ??? /// default C = 8192 } Reader(String name) throws IOException { in = new BufferedReader(new FileReader(name)); } StringTokenizer tokin = new StringTokenizer(""); String next() throws IOException { if (!tokin.hasMoreTokens()) { tokin = new StringTokenizer(in.readLine()); } return tokin.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } } static class Writer { PrintWriter cout; Writer() throws IOException { cout = new PrintWriter(System.out); } Writer(String name) throws IOException { cout = new PrintWriter(new FileWriter(name)); } StringBuilder out = new StringBuilder(); void print(Object a) { out.append(a); } void close() { cout.print(out.toString()); cout.close(); } } static long mod = Long.parseLong("1000000007"); static long pow(long a, long b) { if (b == 0) return 1; if (b % 2 == 1) { return (a * pow(a, b - 1)) % mod; } else { long c = pow(a, b / 2); return (c * c) % mod; } } public static void main(String args[]) throws IOException { Reader in = new Reader(); Writer out = new Writer(); int n = in.nextInt(); int q = in.nextInt(); int col1[] = new int[n]; String s = in.next(); if (s.charAt(0) == '1') col1[0] = 1; for (int i = 1; i < n; i++) { col1[i] = col1[i - 1]; if (s.charAt(i) == '1') col1[i]++; } for (int i = 0; i < q; i++) { int l = in.nextInt() - 1; int r = in.nextInt() - 1; long col = col1[r]; long size = r - l + 1; if (l > 0) col -= col1[l - 1]; long ans1 = pow(2, col) - 1; if (ans1 < 0) ans1 += mod; long p1 = pow(2, size - col) - 1; if (p1 < 0) p1 += mod; long ans = (ans1 + p1 * ans1) % mod; out.print(ans + "\n"); } out.close(); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys from math import * def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def add(a,b): return (a+b)%1000000007 def mul(a,b): return (a*b)%1000000007 def sub(a,b): return (a-b+1000000007)%1000000007 def qpow(a, b): r = 1 k = a for i in range(17): if b & (1<<i): r = mul(r, k) k = mul(k, k) return r n, q = mints() a = list(minp()) c = [0]*(n+1) for i in range(n): c[i+1] = c[i] + int(a[i]) for i in range(q): l, r = mints() k = (r-l+1) o = c[r]-c[l-1] z = sub(qpow(2,o),1) print(mul(z,qpow(2,k-o)))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; import java.io.*; public class Banhmi { // https://codeforces.com/contest/1062/problem/C public static void main(String[] args) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader in = new BufferedReader(new FileReader("Banhmi")); StringTokenizer st = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); char[] arr = new char[n]; arr = in.readLine().toCharArray(); int[] one_count = new int[n+1]; for (int i=0; i<n; i++) { one_count[i+1] = one_count[i]; if (arr[i] == '1') one_count[i+1]++; } long mod = (long)1e9 + 7; long[] powers_of_two = new long[n+10]; powers_of_two[0] = 1; for (int i=1; i<powers_of_two.length; i++) { powers_of_two[i] = powers_of_two[i-1] * 2; powers_of_two[i] %= mod; } for (int i=0; i<q; i++) { st = new StringTokenizer(in.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int num_ones = one_count[r] - one_count[l-1]; int num_zeroes = r-l+1 - num_ones; long curans=0; curans += powers_of_two[num_ones]-1; curans %= mod; long others = curans; others *= powers_of_two[num_zeroes]-1; others %= mod; curans += others; curans = (curans%mod+mod)%mod; System.out.println(curans); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split() n = int(n) q = int(q) s = r[1] p = [0] k = 0 for i in range(n): d = int(s[i]) k += d p.append(k) ans = [] for k in range(q): a, b = r[k + 2].strip().split() a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] zero = l - one ans.append(str((pow(2, l, M) - pow(2, zero, M) + M) % M)) sys.stdout.write("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; b /= 2; a = ((a % m) * (a % m)) % m; } return ans % m; } const long double eps = 1e-10L; long long one[300005], zero[300005]; vector<long long> v; void pw() { long long val = 1; long long temp = 1; v.push_back(val); for (long long i = 1; i <= 300005; i++) { temp *= 2; temp %= (long long)(1000 * 1000 * 1000 + 7); val += temp; val %= (long long)(1000 * 1000 * 1000 + 7); v.push_back(val); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, q; cin >> n >> q; pw(); string s; cin >> s; for (long long i = 1; i <= n; i++) { if (s[i - 1] == '1') one[i]++; else zero[i]++; one[i] += one[i - 1]; zero[i] += zero[i - 1]; } while (q--) { long long a, b; cin >> a >> b; long long o = one[b] - one[a - 1]; long long z = zero[b] - zero[a - 1]; long long ans = v[o + z - 1]; if (z > 0) ans -= v[z - 1]; ans += (long long)(1000 * 1000 * 1000 + 7); ans %= (long long)(1000 * 1000 * 1000 + 7); cout << ans << "\n"; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.*; import java.util.*; import java.math.*; public class A implements Runnable { public void run() { long startTime = System.nanoTime(); int M = (int) (1e9 + 7); int n = nextInt(); int m = nextInt(); char[] c = nextToken().toCharArray(); int[] s = new int[n + 1]; for (int i = 0; i < n; i++) { s[i + 1] = s[i] + (c[i] - '0'); } for (int j = 0; j < m; j++) { int a = nextInt() - 1; int b = nextInt(); int x = s[b] - s[a]; int y = b - a - x; println((get(2, x, M) + M - 1) % M * get(2, y, M) % M); } if (fileIOMode) { System.out.println((System.nanoTime() - startTime) / 1e9); } out.close(); } private long get(long x, int pow, int m) { if (pow == 0) { return 1; } if ((pow & 1) == 0) { return get(x * x % m, pow >> 1, m); } else { return x * get(x, pow - 1, m) % m; } } //----------------------------------------------------------------------------------- private static boolean fileIOMode; private static BufferedReader in; private static PrintWriter out; private static StringTokenizer tokenizer; public static void main(String[] args) throws Exception { fileIOMode = args.length > 0 && args[0].equals("!"); if (fileIOMode) { in = new BufferedReader(new FileReader("a.in")); out = new PrintWriter("a.out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } tokenizer = new StringTokenizer(""); new Thread(new A()).start(); } private static String nextLine() { try { return in.readLine(); } catch (IOException e) { return null; } } private static String nextToken() { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } private static int nextInt() { return Integer.parseInt(nextToken()); } private static long nextLong() { return Long.parseLong(nextToken()); } private static double nextDouble() { return Double.parseDouble(nextToken()); } private static BigInteger nextBigInteger() { return new BigInteger(nextToken()); } private static void print(Object o) { if (fileIOMode) { System.out.print(o); } out.print(o); } private static void println(Object o) { if (fileIOMode) { System.out.println(o); } out.println(o); } private static void printf(String s, Object... o) { if (fileIOMode) { System.out.printf(s, o); } out.printf(s, o); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
def solve(): mod=10**9+7 def _mp(): return map(int,raw_input().split()) n,q=_mp() #n=10**5 count=[0 for _ in range(n+2)] p2=[1 for _ in range(n+2)] r=1 for i in range(1,n+1): # r*=2 p2[i]=(p2[i-1]*2)%mod s=raw_input() for i in range(1,n+1): count[i]=count[i-1]+int(s[i-1]) for i in range(q): l,r=_mp() n1=r-l+1 n2=n1-(count[r]-count[l-1]) #if p2[n1]<p2[n2]: # p2[n1]=p2[n1]+mod print (p2[n1]-p2[n2])%mod #print (2**n1-2**n2)%mod if __name__=="__main__": solve();
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; char str[1008611]; long long s1[1008611], s2[1008611]; long long pow_(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % 1000000007; b >>= 1; a = (a * a) % 1000000007; } return ans; } long long inv(long long x) { return pow_(x, 1000000007 - 2); } int main() { int n, q; cin >> n >> q; cin >> str; if (str[0] == '0') { s1[0] = 1; s2[0] = 0; } else { s1[0] = 0; s2[0] = 1; } for (int i = 1; i < n; i++) { s1[i] = s1[i - 1]; s2[i] = s2[i - 1]; if (str[i] == '0') { s1[i] = (s1[i] + 1) % 1000000007; } else { s2[i] = (s2[i] + 1) % 1000000007; } } long long n1, n2; while (q--) { int l, r; cin >> l >> r; n1 = (s1[r - 1] - s1[l - 1]) % 1000000007; n2 = (s2[r - 1] - s2[l - 1]) % 1000000007; if (str[l - 1] == '0') { n1 = (n1 + 1) % 1000000007; } else { n2 = (n2 + 1) % 1000000007; } long long a, b; a = pow_(2, n2) - 1; a = a % 1000000007; b = ((pow_(2, n1) - 1) * a); b = (b + 1000000007) % 1000000007; long long sum = (a + b) % 1000000007; cout << sum << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
# Causes TLE # C++17 implemention -> 1062c.cpp MOD = 1000000007 def main(): buf = input() buflist = buf.split() n = int(buflist[0]) q = int(buflist[1]) buf = input() x = buf sum_list = [0] # sentinel / one indexing for i, deliciousness in enumerate(x): sum_list.append(int(deliciousness) + sum_list[i]) enjoyment_list = [0] for i in range(n): enjoyment_list.append((enjoyment_list[i] * 2 + 1) % MOD) query_list = [] for i in range(q): buf = input() buflist = buf.split() l = int(buflist[0]) # one indexing r = int(buflist[1]) # one indexing query_list.append((l, r)) for i, query in enumerate(query_list): l = query[0] r = query[1] banhmi_count = r - l + 1 delicious_count = sum_list[r] - sum_list[l - 1] non_delicious_count = banhmi_count - delicious_count enjoyment = 0 # main part if delicious_count == 0: enjoyment = 0 else: enjoyment += enjoyment_list[delicious_count] enjoyment += (enjoyment_list[banhmi_count] - enjoyment_list[delicious_count] - enjoyment_list[non_delicious_count]) enjoyment = enjoyment % MOD print(enjoyment) if __name__ == '__main__': main()
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100002; const int MOD = 1000000007; int n, nQueries, ps[MAX_N]; int64_t pw2[MAX_N]; void solve() { cin >> n >> nQueries; pw2[0] = 1; for (int i = 1; i <= n; ++i) { char x; cin >> x; ps[i] = ps[i - 1] + x - '0'; pw2[i] = pw2[i - 1] * 2 % MOD; } while (nQueries--) { int l, r; cin >> l >> r; int64_t tmp1 = pw2[ps[r] - ps[l - 1]] - 1; int64_t tmp2 = pw2[r - l + 1 - ps[r] + ps[l - 1]] - 1; cout << (tmp1 + tmp1 * tmp2 % MOD) % MOD << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int LIMIT = 1e5 + 7; const int MOD = 1e9 + 7; const int MAX = 1 << 30; int n, q, l, r, dp[LIMIT], f[LIMIT]; char c; int main() { scanf("%d %d\n", &n, &q); f[0] = 1; for (int i = 0; i < n; i++) { scanf("%c", &c); dp[i + 1] = dp[i] + (c - '0'); f[i + 1] = f[i] * 2ll % MOD; } while (q--) { scanf("%d %d", &l, &r); unsigned long long ones = dp[r] - dp[l - 1]; unsigned long long zeros = r - l + 1 - ones; unsigned long long ans = (f[ones] - 1ll) % MOD; ans = (ans * (unsigned long long)f[zeros]) % MOD; printf("%lld\n", ans); } return EXIT_SUCCESS; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! # TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!! from sys import stdin, stdout import collections #N = int(input()) #s = input() N,Q = [int(x) for x in stdin.readline().split()] s = input() ones = [0]*N t = 0 for i in range(N): if s[i]=='1': t += 1 ones[i] = t find = [1]*(N+1) for i in range(1,N+1): find[i] = (find[i-1]*2)%1000000007 ans = [0]*Q for i in range(Q): L,R = [int(x) for x in stdin.readline().split()] L -= 1 R -= 1 if L==0: one = ones[R] else: one = ones[R] - ones[L-1] if one==0: ans[i] = 0 else: size = R-L+1 a1 = find[size-1] res = find[size] - find[size-one] ans[i] = res%1000000007 print(*ans,sep='\n')
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys r = sys.stdin.readlines() n, q = r[0].split(' ') n = int(n) q = int(q) M = 10 ** 9 + 7 twopow = [1] * (2 * n + 1) for j in range(1, (2 * n + 1)): twopow[j] = twopow[j - 1] * 2 % M s = r[1] p = [0] * (n + 1) for v in range(n): p[v + 1] = p[v] + (s[v] == '1') ans = [0] * q for k in range(q): a, b = r[k + 2].split(' ') a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] v = (twopow[l] - twopow[l - one] + M) % M ans[k] = str(v) sys.stdout.write("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.DataInputStream; import java.io.IOException; import java.util.InputMismatchException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author xwchen */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int q = in.nextInt(); char[] s = in.next().toCharArray(); int[] sum = new int[s.length + 1]; long[] e2 = new long[s.length + 1]; long mod = 1000000007L; e2[0] = 1; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + ((s[i - 1] == '1') ? 1 : 0); e2[i] = e2[i - 1] * 2; e2[i] %= mod; } while (q-- > 0) { int l = in.nextInt(); int r = in.nextInt(); int len = r - l + 1; int one = sum[r] - sum[l - 1]; int zero = len - one; long ans = e2[one] - 1; long base = ans; ans = ans + (e2[zero] - 1) * base % mod; ans %= mod; out.println(ans); } } } static class InputReader { final private int BUFFER_SIZE = 1 << 10; final private int LINE_SIZE = 1 << 20; private DataInputStream in; private byte[] buffer; private int bufferPointer; private int bytesRead; public InputReader(InputStream inputStream) { this.in = new DataInputStream(inputStream); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String next() { byte[] buf = new byte[LINE_SIZE]; // line length int cnt = 0, c; c = read(); while (c == ' ' || c == '\n' || c == '\r') c = read(); do { if (c == ' ' || c == '\n' || c == '\r') break; buf[cnt++] = (byte) c; } while ((c = read()) != -1); return new String(buf, 0, cnt); } public int nextInt() { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } private void fillBuffer() { try { bytesRead = in.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } catch (IOException e) { throw new InputMismatchException(); } } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class Main { static class Task { int NN = 1000006; int MOD = 1000000007; int [] zeros = new int[NN]; long [] exp2 = new long[NN]; public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); String s = in.next(); for(int i=0;i<n;++i) { zeros[i] = 0; if(i > 0) { zeros[i] = zeros[i - 1]; } if(s.charAt(i) == '0') { ++zeros[i]; } } exp2[0] = 1L; for(int i=1;i<NN;++i) { exp2[i] = (exp2[i - 1] * 2L)%MOD; } while(q-- > 0) { int l = in.nextInt(), r = in.nextInt(); --l;--r; int zero = zeros[r]; if(l > 0) { zero -= zeros[l - 1]; } int len = r - l + 1; long ans = exp2[len]; ans = (ans - exp2[zero] + MOD)%MOD; out.println(ans); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].split(' ') n = int(n) q = int(q) s = r[1] p = [0] for v in range(n): p.append(p[v] + int(s[v])) ans = [] for k in range(q): a, b = r[k + 2].split(' ') a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] v = (pow(2, l, M) - pow(2, l - one, M) + M) % M ans.append(str(v)) sys.stdout.write("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
def init_input(): import os from sys import stdin it = iter(os.read(stdin.fileno(), 10 ** 7).split()) return lambda: next(it).decode(), lambda: int(next(it)), lambda: float(next(it)) ns, ni, nf = init_input() MOD = 10 ** 9 + 7 n, q = ni(), ni() s = 'x' + ns() c = [0] * (n + 1) for i in range(1, n + 1): c[i] = c[i - 1] + (s[i] == '1') p2 = [1] * (2 * n + 1) for i in range(1, 2 * n + 1): p2[i] = p2[i - 1] * 2 % MOD out = [] for qq in range(q): l, r = ni(), ni() o = c[r] - c[l - 1] z = (r - l + 1) - o ans = (p2[o + z] - 1 - p2[z] + 1) % MOD out.append(ans) print(*out, sep='\n')
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
def beki(a,b): waru=10**9+7 ans=1 while(b>0): if(1 & b): ans= ans * a %waru b >>= 1 a=a * a % waru return ans n,m=map(int,input().split()) s=input() ans=[] waru=10**9+7 ruiseki=[0]*(n+1) bekij=[1]*(n+1) for i in range(n): ruiseki[i+1]=ruiseki[i]+int(s[i]) bekij[i+1]=(bekij[i]*2)%waru for i in range(m): l,r=map(int,input().split()) ko=ruiseki[r]-ruiseki[l-1] ans.append(str((((bekij[ko] -1)) * ((bekij[r+1-l - ko]))) %waru)) print("\n".join(ans))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
from sys import stdin out = [] n,q = map(int, raw_input().split()) s = raw_input() pref = [0] for i in s: if i == '0': pref.append(pref[-1]) else: pref.append(pref[-1]+1) MOD = pow(10,9)+7 pow2 = [1]*(1+n) for i in range(n): pow2[i+1] = (pow2[i] *2)%MOD for line in stdin.readlines(): l,r = map(int, line.split()) n = pref[r] - pref[l-1] k = r - l + 1 - n out.append(str((pow2[n]-1 + (pow2[n]-1)*(pow2[k]-1))%MOD)) print('\n'.join(out))
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int mul(int a, int b) { return (long long)a * b % mod; } int power(int a, long long b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } void solve() { int n, q; cin >> n >> q; string s; cin >> s; vector<int> ps(n + 1); for (int i = 0; i < n; i++) { ps[i + 1] = ps[i] + (s[i] == '1'); } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; int one = ps[r] - ps[l - 1]; int zeo = r - l + 1 - one; cout << (power(2, one) - 1) * 1ll * power(2, zeo) % mod << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int mod = 1e9 + 7; int n, q, f[N]; char c; int binPow(int x, int y) { int ans = 1; while (y > 0) { if (y & 1) ans = (1LL * ans * x) % mod; x = (1LL * x * x) % mod; y >>= 1; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) cin >> c, f[i] = f[i - 1] + (c == '1'); while (q--) { int l, r; cin >> l >> r; int x = f[r] - f[l - 1], y = r - l + 1 - x; int ans = binPow(2, x) - 1; ans = (1LL * ans * binPow(2, y)) % mod; if (ans < 0) ans += mod; cout << ans << "\n"; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; int n, q, x, y, z, h; string s; long long a[100005], b[100005]; void init() { b[0] = 1; for (int i = 1; i <= n; i++) { b[i] = b[i - 1] * 2 % mod; } } int main() { cin >> n >> q; cin >> s; init(); for (int i = 0; i < n; i++) a[i] = a[i - 1] + s[i] - 48; while (q--) { cin >> x >> y; if (x == 1) z = a[y - 1]; else z = a[y - 1] - a[x - 2]; h = y - x + 1; long long ans = (b[h] - b[h - z] + mod) % mod; cout << ans << endl; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> #pragma GCC optimize("03") using namespace std; long long int const mod = 1e9 + 7; long long int pre[100010][2]; void mul(long long int &x, long long int val) { x = (x * val) % mod; if (x < 0) x += mod; } void add(long long int &x, long long int val) { x = (x + val) % mod; if (x < 0) x += mod; } long long int power(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1ll) mul(res, x); mul(x, x); y >>= 1; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, q, l, r, one, zero, x, y, val, d; string st; cin >> n >> q; cin >> st; for (long long int i = 1; i <= n; i++) { d = st[i - 1] - '0'; pre[i][d] += 1; } for (long long int i = 1; i <= n; i++) { pre[i][0] += pre[i - 1][0]; pre[i][1] += pre[i - 1][1]; } while (q--) { cin >> l >> r; one = pre[r][1] - pre[l - 1][1]; zero = pre[r][0] - pre[l - 1][0]; if (one) { x = (power(2, one) - 1); val = x; y = (power(2, zero) - 1); mul(y, val); add(x, y); cout << x << "\n"; } else { y = 0; cout << y << "\n"; } } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long a[1000000]; long long bp(long long a, int n) { long long res = 1; while (n) { if (n % 2) res *= a; a *= a; a %= mod; n /= 2; res %= mod; } return res; } int main() { int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { char c; cin >> c; a[i] = a[i - 1] + (c == '0'); } while (q--) { int l, r; cin >> l >> r; cout << (bp(2, r - l + 1) + 10 * mod - 1 - bp(2, a[r] - a[l - 1]) + 1) % mod << endl; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { FastReader reader = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int n = reader.nextInt(); int q = reader.nextInt(); String input = reader.nextLine(); int[] zeros = new int[n+1]; int[] ones = new int[n+1]; for (int i=0; i<n; i++) if (input.charAt(i) == '0') zeros[i+1]++; else ones[i+1]++; for (int i=0; i<n; i++) { zeros[i+1] += zeros[i]; ones[i+1] += ones[i]; } long mod = 1000000007; long[] pow = new long[n+1]; pow[0] = 1; for (int i=1; i<=n; i++) pow[i] = (pow[i-1]*2) % mod; while (q > 0) { int l = reader.nextInt()-1; int r = reader.nextInt(); int z = zeros[r] - zeros[l]; int o = ones[r] - ones[l]; writer.println((pow[z] * (pow[o]+mod-1)) % mod); q--; } writer.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
import java.io.*; import java.util.stream.Stream; /** * @author madi.sagimbekov */ public class C1062C { private static BufferedReader in; private static BufferedWriter out; public static void main(String[] args) throws IOException { open(); int[] nq = readInts(); int n = nq[0]; int q = nq[1]; char[] a = readString().toCharArray(); int[] z = new int[n]; int[] o = new int[n]; for (int i = 0; i < n; i++) { if (i == 0) { if (a[i] == '0') { z[i]++; } else { o[i]++; } } else { if (a[i] == '0') { z[i] = z[i - 1] + 1; o[i] = o[i - 1]; } else { o[i] = o[i - 1] + 1; z[i] = z[i - 1]; } } } long mod = 1000000007; for (int i = 0; i < q; i++) { int[] lr = readInts(); int l = lr[0]; int r = lr[1]; int zeros = z[r - 1] - (l == 1 ? 0 : z[l - 2]); int ones = o[r - 1] - (l == 1 ? 0 : o[l - 2]); long ans1 = pow(ones, mod) - 1; long ans2 = pow(zeros, mod) - 1; long ans = (ans1 % mod + (ans1 * ans2) % mod) % mod; out.write(ans + "\n"); } close(); } private static long pow(int k, long mod) { if (k == 0) { return 1; } if (k == 1) { return 2; } long res = pow(k / 2, mod); if (k % 2 == 0) { return (res * res) % mod; } else { return (res * res * 2) % mod; } } private static int[] readInts() throws IOException { return Stream.of(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } private static int readInt() throws IOException { return Integer.parseInt(in.readLine()); } private static long[] readLongs() throws IOException { return Stream.of(in.readLine().split(" ")).mapToLong(Long::parseLong).toArray(); } private static long readLong() throws IOException { return Long.parseLong(in.readLine()); } private static double[] readDoubles() throws IOException { return Stream.of(in.readLine().split(" ")).mapToDouble(Double::parseDouble).toArray(); } private static double readDouble() throws IOException { return Double.parseDouble(in.readLine()); } private static String readString() throws IOException { return in.readLine(); } private static void open() { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter((System.out))); } private static void close() throws IOException { out.flush(); out.close(); in.close(); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int INFMEM = 63; const int INF = 1061109567; const long long LINF = 4557430888798830399LL; const double DINF = numeric_limits<double>::infinity(); const long long MOD = 1000000007; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; const double PI = 3.141592653589793; inline void fastll(long long &input_number) { input_number = 0; int ch = getchar_unlocked(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar_unlocked(); } while (ch >= '0' && ch <= '9') { input_number = (input_number << 3) + (input_number << 1) + ch - '0'; ch = getchar_unlocked(); } input_number *= sign; } inline void open(string a) { freopen((a + ".in").c_str(), "r", stdin); freopen((a + ".out").c_str(), "w", stdout); } inline void fasterios() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long n, q; long long power[100005]; long long isi[100005]; long long satu[100005]; long long nol[100005]; int main() { fasterios(); cin >> n >> q; power[0] = 1; for (int i = 1; i <= 100001; i++) power[i] = (power[i - 1] * 2) % MOD; for (int i = 1; i <= n; i++) { char tmp; cin >> tmp; isi[i] = (tmp == '1'); if (isi[i]) satu[i]++; else nol[i]++; } for (int i = 1; i <= n; i++) { nol[i] += nol[i - 1]; satu[i] += satu[i - 1]; } while (q--) { long long l, r; cin >> l >> r; long long curnol = nol[r] - nol[l - 1]; long long cursatu = satu[r] - satu[l - 1]; long long ans = power[cursatu] - 1; ans *= power[curnol]; cout << ans % MOD << '\n'; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const long long sz = 2e5 + 5; const long long MD = 1e9 + 7; pair<long long, long long> st[2 * sz]; long long arr[sz]; long long n, q; string s; pair<long long, long long> ADD(pair<long long, long long> &A, pair<long long, long long> &B) { return {A.first + B.first, A.second + B.second}; } void build() { for (long long i = 0; i < n; ++i) { arr[i] = s[i] == '1'; } for (long long i = 0; i < n; ++i) { st[i + n] = {arr[i] == 0, arr[i] == 1}; } for (long long i = n - 1; i >= 0; --i) { st[i] = ADD(st[i << 1], st[i << 1 | 1]); } } pair<long long, long long> query(long long L, long long R) { --L; pair<long long, long long> tot = {0LL, 0LL}; for (L += n, R += n; L < R; L >>= 1, R >>= 1) { if (L & 1) tot = ADD(tot, st[L++]); if (R & 1) tot = ADD(tot, st[--R]); } return tot; } long long powMod(long long a, long long p) { long long res = 1; for (; p; p >>= 1, a = ((a) * (a)) % MD) { if (p & 1) res *= a, res %= MD; } return res; } long long MUL(long long a, long long b) { a %= MD; b %= MD; return (a * b) % MD; } int main() { scanf("%lld", &n), scanf("%lld", &q); cin >> s; build(); while (q--) { long long L, R; scanf("%lld", &L), scanf("%lld", &R); pair<long long, long long> res = query(L, R); long long le = (powMod(2LL, res.second) + MD - 1) % MD; long long ri = (powMod(2LL, res.first) + MD - 1) % MD; printf("%lld\n", MUL(le, 1LL + ri)); } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long int bs(long long int a, long long int b) { long long int res = 1; while (b) { if (b & 1) res *= a; res %= ((long long int)1e9 + 7); b >>= 1; a *= a; a %= ((long long int)1e9 + 7); } return res; } signed main() { long long int n, m; cin >> n >> m; string a; cin >> a; long long int p[n]; memset(p, 0, sizeof p); for (long long int i = 0; i < n; i++) { if (i) p[i] += p[i - 1]; if (a[i] == '1') p[i]++; } while (m--) { long long int l, r; cin >> l >> r; --l, --r; long long int nos = p[r] - (l ? p[l - 1] : 0); long long int noo = (r - l + 1) - nos; long long int ptf = bs(2, nos) - 1; long long int ptt = (bs(2, noo) - 1) * ptf; cout << (ptt % ((long long int)1e9 + 7) + ptf % ((long long int)1e9 + 7)) % ((long long int)1e9 + 7) << '\n'; } return 0; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
from __future__ import division from sys import stdin, stdout # from fractions import gcd # from math import * # from operator import mul # from functools import reduce # from copy import copy from collections import deque, defaultdict, Counter rstr = lambda: stdin.readline().strip() rstrs = lambda: [str(x) for x in stdin.readline().split()] rint = lambda: int(stdin.readline()) rints = lambda: [int(x) for x in stdin.readline().split()] rstr_2d = lambda n: [rstr() for _ in range(n)] rint_2d = lambda n: [rint() for _ in range(n)] rints_2d = lambda n: [rints() for _ in range(n)] pr = lambda args, sep: stdout.write(sep.join(map(str, args)) + '\n') out = [] add = lambda a, b: (a + b) % mod def arr_sum(): tem0, tem1 = [0], [0] for i in range(n): tem0.append(tem0[i] + int(s[i] == '0')) tem1.append(tem1[i] + int(s[i] == '1')) return tem0, tem1 n, q = rints() s, qur, mod = rstr(), rints_2d(q), 10 ** 9 + 7 zero, one = arr_sum() for l, r in qur: all0, all1 = zero[r] - zero[l - 1], one[r] - one[l - 1] if not all1: out.append(0) else: v1 = add(-1, add(-pow(2, all0, mod), 1)) out.append(add(pow(2, all1 + all0, mod), v1)) pr(out, '\n')
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long gcd(long long n1, long long n2) { if (!n1) return n2; if (!n2) return n1; if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { base %= 1000000007; long long ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % 1000000007; base = (base * base) % 1000000007; exponent /= 2; } ans %= 1000000007; return ans; } int arr[1000100 + 1]; long long dp[1000100 + 1]; int main() { clock_t begin = clock(); ; int i, j, k, n, q, l, r; scanf("%d", &n); scanf("%d", &q); string s; cin >> s; for (i = 1; i <= n; i++) { arr[i] = arr[i - 1]; if (s[i - 1] == '1') arr[i]++; } while (q--) { scanf("%d", &l); scanf("%d", &r); int ones = arr[r] - arr[l - 1]; int zeroes = ((r - l + 1) - ones); long long answer = powmod(2, ones) - 1; answer = (answer * powmod(2, zeroes)) % 1000000007; printf("%lld\n", answer); } clock_t end = clock(); double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; fprintf(stderr, "\nTime elapsed : %.3f seconds\n", elapsed_secs); return 0; ; }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; const int nn = 1e5 + 5; const long long MOD = 1e9 + 7; long long pangkat[nn], arr[nn], pre0[nn], pre1[nn]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, q; pangkat[0] = 1; for (int i = 1; i <= 1e5; i++) { pangkat[i] = (pangkat[i - 1] * 2) % MOD; } cin >> n >> q; char a; for (int i = 1; i <= n; i++) { cin >> a; arr[i] = a - '0'; } pre0[1] = (arr[1] == 0); pre1[1] = (arr[1] == 1); for (int i = 2; i <= n; i++) { pre0[i] = pre0[i - 1] + (arr[i] == 0); pre1[i] = pre1[i - 1] + (arr[i] == 1); } while (q--) { int l, r; cin >> l >> r; int nol = pre0[r] - pre0[l - 1]; int satu = pre1[r] - pre1[l - 1]; long long bil1 = pangkat[nol]; long long bil2 = pangkat[satu] - 1; while (bil2 < 0) bil2 += MOD; cout << (bil1 * bil2) % MOD << "\n"; } }
JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way. First, he splits the Banh-mi into n parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as x_i ∈ \{0, 1\}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by x_i and the deliciousness of all the remaining parts will also increase by x_i. The initial enjoyment of JATC is equal to 0. For example, suppose the deliciousness of 3 parts are [0, 1, 0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1, \\_, 1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [\\_, \\_, 2]. After eating the last part, JATC's enjoyment will become 4. However, JATC doesn't want to eat all the parts but to save some for later. He gives you q queries, each of them consisting of two integers l_i and r_i. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [l_i, r_i] in some order. All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 10^9+7. Input The first line contains two integers n and q (1 ≀ n, q ≀ 100 000). The second line contains a string of n characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i-th part. Each of the following q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of the corresponding query. Output Print q lines, where i-th of them contains a single integer β€” the answer to the i-th query modulo 10^9 + 7. Examples Input 4 2 1011 1 4 3 4 Output 14 3 Input 3 2 111 1 2 3 3 Output 3 1 Note In the first example: * For query 1: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2. * For query 2: Both 3, 4 and 4, 3 ordering give the same answer. In the second example, any order of eating parts leads to the same answer.
#include <bits/stdc++.h> using namespace std; long long n, q, l, r, sum[100010], tmp1, tmp2, ans; string s; long long Pow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1 == 1) ans = (ans * a) % 1000000007LL; a = (a * a) % 1000000007LL, b >>= 1; } return ans % 1000000007LL; } int main() { cin >> n >> q; cin >> s; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + s[i - 1] - '0'; while (q--) { cin >> l >> r; tmp1 = sum[r] - sum[l - 1], tmp2 = r - l + 1 - tmp1; ans = Pow(2, tmp1) - 1; if (ans == -1) ans += 1000000007LL; ans = (ans * Pow(2, tmp2)) % 1000000007LL; cout << ans % 1000000007LL << endl; } }