output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 6; string s; long long pp[N]; int pref[N]; const int mod = 1e9 + 7; long long int fpower(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } int main() { pp[0] = 1; for (int i = 1; i < N; i++) { pp[i] = (pp[i - 1] + pp[i - 1]) % mod; } int n, q; cin >> n >> q; string s; cin >> s; for (int i = 0; i < n; i++) { pref[i + 1] = pref[i] + (s[i] == '1'); } while (q--) { int l, r; scanf("%d%d", &l, &r); int ones = pref[r] - pref[l - 1]; long long int ans = (pp[ones] - 1 + mod) % mod; long long int zer = r - l + 1 - ones; if (zer > 0) ans = (ans + (ans * (pp[zer] - 1)) % mod) % mod; printf("%lld\n", ans); } }
### Prompt Create a solution in Cpp for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 6; string s; long long pp[N]; int pref[N]; const int mod = 1e9 + 7; long long int fpower(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } int main() { pp[0] = 1; for (int i = 1; i < N; i++) { pp[i] = (pp[i - 1] + pp[i - 1]) % mod; } int n, q; cin >> n >> q; string s; cin >> s; for (int i = 0; i < n; i++) { pref[i + 1] = pref[i] + (s[i] == '1'); } while (q--) { int l, r; scanf("%d%d", &l, &r); int ones = pref[r] - pref[l - 1]; long long int ans = (pp[ones] - 1 + mod) % mod; long long int zer = r - l + 1 - ones; if (zer > 0) ans = (ans + (ans * (pp[zer] - 1)) % mod) % mod; printf("%lld\n", ans); } } ```
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; long long int powers(long long int a, long long int b) { long long int ans = 1; a %= mod; while (b > 0) { if (b % 2 == 1) ans = ((ans % mod) * (a % mod)) % mod; b /= 2; a = ((a % mod) * (a % mod)) % mod; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, q, l, r; cin >> n >> q; long long int arr[n], one[n + 1], zero[n]; string second; cin >> second; for (int i = 0; i < n; i++) { if (second[i] == '1') arr[i] = 1; else arr[i] = 0; } one[0] = zero[0] = 0; for (int i = 1; i <= n; i++) { one[i] = one[i - 1] + arr[i - 1]; zero[i] = zero[i - 1]; if (!arr[i - 1]) zero[i]++; } while (q--) { cin >> l >> r; long long int ans = (powers(2, one[r] - one[l - 1]) - 1) % mod; long long int zans = (powers(2, zero[r] - zero[l - 1]) - 1) % mod; cout << (((zans + 1) % mod) * ans) % mod << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; long long int powers(long long int a, long long int b) { long long int ans = 1; a %= mod; while (b > 0) { if (b % 2 == 1) ans = ((ans % mod) * (a % mod)) % mod; b /= 2; a = ((a % mod) * (a % mod)) % mod; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, q, l, r; cin >> n >> q; long long int arr[n], one[n + 1], zero[n]; string second; cin >> second; for (int i = 0; i < n; i++) { if (second[i] == '1') arr[i] = 1; else arr[i] = 0; } one[0] = zero[0] = 0; for (int i = 1; i <= n; i++) { one[i] = one[i - 1] + arr[i - 1]; zero[i] = zero[i - 1]; if (!arr[i - 1]) zero[i]++; } while (q--) { cin >> l >> r; long long int ans = (powers(2, one[r] - one[l - 1]) - 1) % mod; long long int zans = (powers(2, zero[r] - zero[l - 1]) - 1) % mod; cout << (((zans + 1) % mod) * ans) % mod << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long power(long long x, unsigned long long y) { int res = 1; x = x % 1000000007; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; y = y >> 1; x = (x * x) % 1000000007; } return res; } int main() { int n, q; vector<int> one(100005, 0), zero(100005, 0); string s; cin >> n >> q; cin >> s; for (int i = 1; i <= n; i++) { zero[i] = zero[i - 1]; one[i] = one[i - 1]; if (s[i - 1] == '0') zero[i]++; else one[i]++; } int l, r; while (q--) { cin >> l >> r; int z = zero[r] - zero[l - 1]; int o = one[r] - one[l - 1]; long long ans = power(2, o) - 1; long long aa = power(2, z) - 1; long long b = (aa % 1000000007 * ans % 1000000007) % 1000000007; cout << (ans % 1000000007 + b % 1000000007) % 1000000007 << "\n"; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long power(long long x, unsigned long long y) { int res = 1; x = x % 1000000007; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; y = y >> 1; x = (x * x) % 1000000007; } return res; } int main() { int n, q; vector<int> one(100005, 0), zero(100005, 0); string s; cin >> n >> q; cin >> s; for (int i = 1; i <= n; i++) { zero[i] = zero[i - 1]; one[i] = one[i - 1]; if (s[i - 1] == '0') zero[i]++; else one[i]++; } int l, r; while (q--) { cin >> l >> r; int z = zero[r] - zero[l - 1]; int o = one[r] - one[l - 1]; long long ans = power(2, o) - 1; long long aa = power(2, z) - 1; long long b = (aa % 1000000007 * ans % 1000000007) % 1000000007; cout << (ans % 1000000007 + b % 1000000007) % 1000000007 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int max2 = 1000000007; long long int power(long long int base, long long int exp) { long long int ans = 1; while (exp) { if (exp % 2 == 1) { ans *= base; ans %= max2; } exp /= 2; base *= base; base %= max2; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); int tab; auto geti = [&]() { cin >> tab; return tab; }; ; long long int n, q; cin >> n >> q; string s; cin >> s; long long int onescount[n]; if (s[0] == '0') { onescount[0] = 0; } else { onescount[0] = 1; } for (long long int i = 1; i < n; i++) { if (s[i] == '0') { onescount[i] = onescount[i - 1]; } else { onescount[i] = onescount[i - 1] + 1; } } while (q > 0) { long long int l, r; cin >> l >> r; l--; r--; long long int one = onescount[r] - onescount[l]; long long int zero = 0; if (s[l] == '1' && s[l] == '1') { one++; } else if (s[l] == '1' && s[r] == '0') { one++; } zero = r - l + 1 - one; long long int count = 0; long long int a = power(2, one) - 1; long long int b = power(2, zero); count = (a * b) % max2; cout << count << endl; q--; } return 0; }
### Prompt Please create a solution in CPP to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int max2 = 1000000007; long long int power(long long int base, long long int exp) { long long int ans = 1; while (exp) { if (exp % 2 == 1) { ans *= base; ans %= max2; } exp /= 2; base *= base; base %= max2; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); int tab; auto geti = [&]() { cin >> tab; return tab; }; ; long long int n, q; cin >> n >> q; string s; cin >> s; long long int onescount[n]; if (s[0] == '0') { onescount[0] = 0; } else { onescount[0] = 1; } for (long long int i = 1; i < n; i++) { if (s[i] == '0') { onescount[i] = onescount[i - 1]; } else { onescount[i] = onescount[i - 1] + 1; } } while (q > 0) { long long int l, r; cin >> l >> r; l--; r--; long long int one = onescount[r] - onescount[l]; long long int zero = 0; if (s[l] == '1' && s[l] == '1') { one++; } else if (s[l] == '1' && s[r] == '0') { one++; } zero = r - l + 1 - one; long long int count = 0; long long int a = power(2, one) - 1; long long int b = power(2, zero); count = (a * b) % max2; cout << count << endl; q--; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; vector<long long> p; int main() { int n, q; cin >> n >> q; string s; cin >> s; p.resize(1e5 + 1); p[0] = 1; for (int i = 1; i <= 1e5; i++) { p[i] = (p[i - 1] * 2) % MOD; } vector<long long> kum(n + 1); kum[0] = 0; for (int i = 0; i < n; i++) { if (s[i] == '1') { kum[i + 1] = kum[i] + 1; } else { kum[i + 1] = kum[i]; } } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; long long kol_1 = kum[r] - kum[l - 1]; long long kol_0 = r - l + 1 - kol_1; long long ans_1 = p[kol_1] - 1; long long ans_0 = ans_1 * (p[kol_0] - 1); cout << (ans_1 + ans_0) % MOD << '\n'; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; vector<long long> p; int main() { int n, q; cin >> n >> q; string s; cin >> s; p.resize(1e5 + 1); p[0] = 1; for (int i = 1; i <= 1e5; i++) { p[i] = (p[i - 1] * 2) % MOD; } vector<long long> kum(n + 1); kum[0] = 0; for (int i = 0; i < n; i++) { if (s[i] == '1') { kum[i + 1] = kum[i] + 1; } else { kum[i + 1] = kum[i]; } } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; long long kol_1 = kum[r] - kum[l - 1]; long long kol_0 = r - l + 1 - kol_1; long long ans_1 = p[kol_1] - 1; long long ans_0 = ans_1 * (p[kol_0] - 1); cout << (ans_1 + ans_0) % MOD << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long calc(long long n) { if (!n) return 1; if (n == 1) return 2; long long u = calc(n / 2); u = ((u % ((long long)(1e9 + 7))) * (u % ((long long)(1e9 + 7)))) % ((long long)(1e9 + 7)); if (n % 2 == 1) return (2 * u) % ((long long)(1e9 + 7)); return u % ((long long)(1e9 + 7)); } int main() { long long n, q; scanf("%lld %lld", &n, &q); vector<long long> v(n + 1, 0); string s; cin >> s; for (long long i = 0; i < n; i++) { v[i + 1] = (long long)(s[i] - '0'); } for (long long i = 1; i < n + 1; i++) v[i] += v[i - 1]; while ((q--)) { long long l, r; scanf("%lld %lld", &l, &r); cout << (((calc(v[r] - v[l - 1]) - 1) % ((long long)(1e9 + 7))) * (calc(r - l + 1 - v[r] + v[l - 1]) % ((long long)(1e9 + 7)))) % ((long long)(1e9 + 7)) << '\n'; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long calc(long long n) { if (!n) return 1; if (n == 1) return 2; long long u = calc(n / 2); u = ((u % ((long long)(1e9 + 7))) * (u % ((long long)(1e9 + 7)))) % ((long long)(1e9 + 7)); if (n % 2 == 1) return (2 * u) % ((long long)(1e9 + 7)); return u % ((long long)(1e9 + 7)); } int main() { long long n, q; scanf("%lld %lld", &n, &q); vector<long long> v(n + 1, 0); string s; cin >> s; for (long long i = 0; i < n; i++) { v[i + 1] = (long long)(s[i] - '0'); } for (long long i = 1; i < n + 1; i++) v[i] += v[i - 1]; while ((q--)) { long long l, r; scanf("%lld %lld", &l, &r); cout << (((calc(v[r] - v[l - 1]) - 1) % ((long long)(1e9 + 7))) * (calc(r - l + 1 - v[r] + v[l - 1]) % ((long long)(1e9 + 7)))) % ((long long)(1e9 + 7)) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, q, sl1, l, r; string s; long long f[100009], lt[100009], a[100009], so, res, sum[100009]; int main() { int mod = 1000000007; cin >> n >> q; getline(cin, s); getline(cin, s); for (int i = 0; i < n; i++) { if (s[i] == '1') a[i + 1] = 1; } for (int i = 1; i < n + 1; i++) { a[i] = a[i - 1] + a[i]; } lt[0] = 1; sum[0] = 1; for (int i = 1; i < n + 1; i++) { lt[i] = (lt[i - 1] * 2) % mod; sum[i] = (sum[i - 1] + lt[i]) % mod; } for (int i = 1; i < q + 1; i++) { cin >> l >> r; sl1 = a[r] - a[l - 1]; if (sl1 == 0) { cout << 0 << endl; continue; } res = sum[sl1 - 1]; so = sum[sl1 - 1]; if (r - l + 1 - sl1 > 0) res = (res + (so * (sum[r - l - sl1])) % mod) % mod; cout << res << endl; } }
### Prompt Your challenge is to write a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, q, sl1, l, r; string s; long long f[100009], lt[100009], a[100009], so, res, sum[100009]; int main() { int mod = 1000000007; cin >> n >> q; getline(cin, s); getline(cin, s); for (int i = 0; i < n; i++) { if (s[i] == '1') a[i + 1] = 1; } for (int i = 1; i < n + 1; i++) { a[i] = a[i - 1] + a[i]; } lt[0] = 1; sum[0] = 1; for (int i = 1; i < n + 1; i++) { lt[i] = (lt[i - 1] * 2) % mod; sum[i] = (sum[i - 1] + lt[i]) % mod; } for (int i = 1; i < q + 1; i++) { cin >> l >> r; sl1 = a[r] - a[l - 1]; if (sl1 == 0) { cout << 0 << endl; continue; } res = sum[sl1 - 1]; so = sum[sl1 - 1]; if (r - l + 1 - sl1 > 0) res = (res + (so * (sum[r - l - sl1])) % mod) % mod; cout << res << endl; } } ```
#include <bits/stdc++.h> using namespace std; char c; int a[101001], n, p, l, r; const long long mod = 1e9 + 7; long long num[101010]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> p; num[0] = 1; for (int i = 1; i < 100001; i++) num[i] = num[i - 1] * 2 % mod; for (int i = 1; i <= n; i++) { cin >> c; a[i] = a[i - 1] + c - '0'; } while (p--) { cin >> l >> r; cout << (long long)(num[a[r] - a[l - 1]] - 1) * (long long)num[r - l + 1 - a[r] + a[l - 1]] % mod << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char c; int a[101001], n, p, l, r; const long long mod = 1e9 + 7; long long num[101010]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> p; num[0] = 1; for (int i = 1; i < 100001; i++) num[i] = num[i - 1] * 2 % mod; for (int i = 1; i <= n; i++) { cin >> c; a[i] = a[i - 1] + c - '0'; } while (p--) { cin >> l >> r; cout << (long long)(num[a[r] - a[l - 1]] - 1) * (long long)num[r - l + 1 - a[r] + a[l - 1]] % mod << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long m0 = 0, m1 = 0; vector<long long> v0, v1, v2; int main() { long long q, n, i, j, cnt = 0, l, r, one = 0, zero = 0, even = 0, odd = 0, total = 0; string s; cin >> n >> q; cin >> s; v0.push_back(0); v1.push_back(0); long long p = 1; for (i = 0; i <= 100005; i++) { v2.push_back(p); p = (p * 2) % 1000000007; } for (i = 0; i < n; i++) { if (s[i] == '0') m0++; else m1++; v0.push_back(m0); v1.push_back(m1); } while (q--) { cin >> l >> r; one = v1[r] - v1[l - 1]; zero = v0[r] - v0[l - 1]; even = (v2[one] - 1) % 1000000007; long long qq = (v2[zero] - 1) % 1000000007; odd = (even * qq) % 1000000007; total = (even + odd) % 1000000007; cout << total << endl; } }
### Prompt Create a solution in cpp for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long m0 = 0, m1 = 0; vector<long long> v0, v1, v2; int main() { long long q, n, i, j, cnt = 0, l, r, one = 0, zero = 0, even = 0, odd = 0, total = 0; string s; cin >> n >> q; cin >> s; v0.push_back(0); v1.push_back(0); long long p = 1; for (i = 0; i <= 100005; i++) { v2.push_back(p); p = (p * 2) % 1000000007; } for (i = 0; i < n; i++) { if (s[i] == '0') m0++; else m1++; v0.push_back(m0); v1.push_back(m1); } while (q--) { cin >> l >> r; one = v1[r] - v1[l - 1]; zero = v0[r] - v0[l - 1]; even = (v2[one] - 1) % 1000000007; long long qq = (v2[zero] - 1) % 1000000007; odd = (even * qq) % 1000000007; total = (even + odd) % 1000000007; cout << total << endl; } } ```
#include <bits/stdc++.h> long du = 1e9 + 7; long long muhai[100005]; int main() { unsigned int n, q; std::cin >> n >> q; std::string x; std::cin >> x; muhai[0] = 1; for (int i = 1; i < 100005; i++) { muhai[i] = muhai[i - 1] * 2; muhai[i] %= du; } int dem[n]; if (x[0] == '0') dem[0] = 1; else dem[0] = 0; for (int i = 1; i < n; i++) { if (x[i] == '0') dem[i] = dem[i - 1] + 1; else dem[i] = dem[i - 1]; } unsigned int l, r, ko; unsigned long long ans; for (int i = 0; i < q; i++) { std::cin >> l >> r; if (x[l - 1] == '1') ko = dem[r - 1] - dem[l - 1]; else ko = dem[r - 1] - dem[l - 1] + 1; ans = muhai[r - l + 1] % du - muhai[ko] % du; ans += du; ans %= du; std::cout << ans << std::endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> long du = 1e9 + 7; long long muhai[100005]; int main() { unsigned int n, q; std::cin >> n >> q; std::string x; std::cin >> x; muhai[0] = 1; for (int i = 1; i < 100005; i++) { muhai[i] = muhai[i - 1] * 2; muhai[i] %= du; } int dem[n]; if (x[0] == '0') dem[0] = 1; else dem[0] = 0; for (int i = 1; i < n; i++) { if (x[i] == '0') dem[i] = dem[i - 1] + 1; else dem[i] = dem[i - 1]; } unsigned int l, r, ko; unsigned long long ans; for (int i = 0; i < q; i++) { std::cin >> l >> r; if (x[l - 1] == '1') ko = dem[r - 1] - dem[l - 1]; else ko = dem[r - 1] - dem[l - 1] + 1; ans = muhai[r - l + 1] % du - muhai[ko] % du; ans += du; ans %= du; std::cout << ans << std::endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long maxN = 2e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 1; int n, q, m, h, l, A[maxN]; int acum[maxN]; 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() { ios_base::sync_with_stdio(false), cin.tie(NULL); cin >> n >> q; for (int i = int(1); i < int(n + 1); i++) { char c; cin >> c; acum[i] = acum[i - 1]; if (c == '0') { acum[i]++; } } int a, b; while (q--) { cin >> a >> b; int tot = b - a + 1; int ceros = 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; }
### Prompt Please formulate a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long maxN = 2e5 + 5, MOD = 1e9 + 7, INF = 1e9 + 1; int n, q, m, h, l, A[maxN]; int acum[maxN]; 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() { ios_base::sync_with_stdio(false), cin.tie(NULL); cin >> n >> q; for (int i = int(1); i < int(n + 1); i++) { char c; cin >> c; acum[i] = acum[i - 1]; if (c == '0') { acum[i]++; } } int a, b; while (q--) { cin >> a >> b; int tot = b - a + 1; int ceros = 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; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const double PI = acos(-1); const double ERR = 1e-10; long long pow_(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % Mod; b >>= 1; a = (a * a) % Mod; } return ans; } long long inv(long long x) { return pow_(x, Mod - 2); } long long sum[maxn][2]; char str[maxn]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", str + 1); for (int i = 1; i <= n; i++) { sum[i][0] = sum[i - 1][0]; sum[i][1] = sum[i - 1][1]; if (str[i] == '0') sum[i][0] = (sum[i][0] + 1) % Mod; else sum[i][1] = (sum[i][1] + 1) % Mod; ; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long sum0 = (sum[r][0] - sum[l - 1][0] + Mod) % Mod; long long sum1 = (sum[r][1] - sum[l - 1][1] + Mod) % Mod; long long ans = 0; ans = (pow_(2, sum1) - 1 + Mod) % Mod; long long st = (pow_(2, sum1) - 1 + Mod) % Mod; long long tmp = (pow_(2, sum0) - 1 + Mod) % Mod; tmp = (tmp * st) % Mod; ans = (ans + tmp) % Mod; printf("%lld\n", ans); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int Mod = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double e = exp(1); const double PI = acos(-1); const double ERR = 1e-10; long long pow_(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % Mod; b >>= 1; a = (a * a) % Mod; } return ans; } long long inv(long long x) { return pow_(x, Mod - 2); } long long sum[maxn][2]; char str[maxn]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", str + 1); for (int i = 1; i <= n; i++) { sum[i][0] = sum[i - 1][0]; sum[i][1] = sum[i - 1][1]; if (str[i] == '0') sum[i][0] = (sum[i][0] + 1) % Mod; else sum[i][1] = (sum[i][1] + 1) % Mod; ; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long sum0 = (sum[r][0] - sum[l - 1][0] + Mod) % Mod; long long sum1 = (sum[r][1] - sum[l - 1][1] + Mod) % Mod; long long ans = 0; ans = (pow_(2, sum1) - 1 + Mod) % Mod; long long st = (pow_(2, sum1) - 1 + Mod) % Mod; long long tmp = (pow_(2, sum0) - 1 + Mod) % Mod; tmp = (tmp * st) % Mod; ans = (ans + tmp) % Mod; printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long OO = (long long)1e9; const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(4); } vector<int> tree; int arr[1000000]; long long vec[100005]; void build(int s, int e, int pos) { if (s == e) { tree[pos] = arr[e]; return; } int mid = (s + e) / 2; build(s, mid, pos * 2); build(mid + 1, e, pos * 2 + 1); tree[pos] = tree[pos * 2] + tree[pos * 2 + 1]; } int getQ(int l, int r, int s, int e, int pos) { if (s >= l && e <= r) return tree[pos]; if (s > r || e < l) return 0; int mid = (s + e) / 2; int x = getQ(l, r, s, mid, pos * 2); int y = getQ(l, r, mid + 1, e, pos * 2 + 1); return (x + y); } int main() { fast(); long long power = 1; for (int i = 1; i <= 100000; i++) { vec[i] = vec[i - 1] + power; vec[i] %= mod; power *= 2; power %= mod; } int n, q; cin >> n >> q; char ch; for (int i = 0; i < n; i++) { cin >> ch; arr[i] = ch - '0'; } int sz = (int)(ceil(log(n) / log(2))); sz = 2 * (int)(pow(2, sz)); tree.resize(sz + 1); build(0, n - 1, 1); int l, r, u, v; long long ans = 0; for (int i = 0; i < q; i++) { cin >> l >> r; l--, r--; u = getQ(l, r, 0, n - 1, 1); v = (r - l + 1) - u; ans = vec[u] + ((vec[u] * vec[v]) % mod); ans %= mod; cout << ans << '\n'; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long OO = (long long)1e9; const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(4); } vector<int> tree; int arr[1000000]; long long vec[100005]; void build(int s, int e, int pos) { if (s == e) { tree[pos] = arr[e]; return; } int mid = (s + e) / 2; build(s, mid, pos * 2); build(mid + 1, e, pos * 2 + 1); tree[pos] = tree[pos * 2] + tree[pos * 2 + 1]; } int getQ(int l, int r, int s, int e, int pos) { if (s >= l && e <= r) return tree[pos]; if (s > r || e < l) return 0; int mid = (s + e) / 2; int x = getQ(l, r, s, mid, pos * 2); int y = getQ(l, r, mid + 1, e, pos * 2 + 1); return (x + y); } int main() { fast(); long long power = 1; for (int i = 1; i <= 100000; i++) { vec[i] = vec[i - 1] + power; vec[i] %= mod; power *= 2; power %= mod; } int n, q; cin >> n >> q; char ch; for (int i = 0; i < n; i++) { cin >> ch; arr[i] = ch - '0'; } int sz = (int)(ceil(log(n) / log(2))); sz = 2 * (int)(pow(2, sz)); tree.resize(sz + 1); build(0, n - 1, 1); int l, r, u, v; long long ans = 0; for (int i = 0; i < q; i++) { cin >> l >> r; l--, r--; u = getQ(l, r, 0, n - 1, 1); v = (r - l + 1) - u; ans = vec[u] + ((vec[u] * vec[v]) % mod); ans %= mod; cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> const int maxn = 100100; const int MOD = 1e9 + 7; char s[maxn]; int c[maxn], f[maxn]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", s + 1); f[0] = 1; for (int i = 1; i <= n; i++) { c[i] = c[i - 1] + (s[i] == '1'); f[i] = f[i - 1] * 2ll % MOD; } for (int k = 1; k <= q; k++) { int l, r; scanf("%d%d", &l, &r); int o = c[r] - c[l - 1]; int ans = (f[o] + MOD - 1ll) % MOD * f[r - l + 1 - o] % MOD; printf("%d\n", ans); } return 0; }
### Prompt Create a solution in cpp for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> const int maxn = 100100; const int MOD = 1e9 + 7; char s[maxn]; int c[maxn], f[maxn]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", s + 1); f[0] = 1; for (int i = 1; i <= n; i++) { c[i] = c[i - 1] + (s[i] == '1'); f[i] = f[i - 1] * 2ll % MOD; } for (int k = 1; k <= q; k++) { int l, r; scanf("%d%d", &l, &r); int o = c[r] - c[l - 1]; int ans = (f[o] + MOD - 1ll) % MOD * f[r - l + 1 - o] % MOD; printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int N, Q; string s; int s0[100005]; long long qpow(long long x, int n) { long long res = 1; while (n) { if (n & 1) res = res * x % int(1e9 + 7); x = x * x % int(1e9 + 7); n /= 2; } return res; } int main() { cin >> N >> Q; cin >> s; for (int i = 0; i < N; i++) { s0[i + 1] = s0[i] + (s[i] == '0'); } int l, r; while (Q--) { cin >> l >> r; long long ans = (qpow(2, r - l + 1) - qpow(2, s0[r] - s0[l - 1]) + int(1e9 + 7)) % int(1e9 + 7); cout << ans << endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, Q; string s; int s0[100005]; long long qpow(long long x, int n) { long long res = 1; while (n) { if (n & 1) res = res * x % int(1e9 + 7); x = x * x % int(1e9 + 7); n /= 2; } return res; } int main() { cin >> N >> Q; cin >> s; for (int i = 0; i < N; i++) { s0[i + 1] = s0[i] + (s[i] == '0'); } int l, r; while (Q--) { cin >> l >> r; long long ans = (qpow(2, r - l + 1) - qpow(2, s0[r] - s0[l - 1]) + int(1e9 + 7)) % int(1e9 + 7); cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int M1 = 1000000007; const int M2 = 998244353; const int N = 100005; int ones[N]; template <typename T> T modpow(T base, T exp, T modulus) { base %= modulus; T result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } long long int GPmodM(long long int a, long long int r, long long int n) { return (a * (modpow(r, n, M1) - 1) * modpow(r - 1, M1 - 2, M1)) % M1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m, q, a, b, temp; cin >> n >> q; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '1') { ones[i + 1] = ones[i] + 1; } else { ones[i + 1] = ones[i]; } } for (int i = 0; i < q; i++) { cin >> a >> b; int length = b - a + 1; int one = ones[b] - ones[a - 1]; int zero = length - one; if (zero == length) { cout << 0 << "\n"; } else { long long int ans = modpow(2 * 1LL, 1LL * one, M1) - 1; ans += GPmodM(ans, 1LL * 2, zero); cout << ans % M1 << "\n"; } } }
### Prompt Please provide a CPP coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int M1 = 1000000007; const int M2 = 998244353; const int N = 100005; int ones[N]; template <typename T> T modpow(T base, T exp, T modulus) { base %= modulus; T result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } long long int GPmodM(long long int a, long long int r, long long int n) { return (a * (modpow(r, n, M1) - 1) * modpow(r - 1, M1 - 2, M1)) % M1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m, q, a, b, temp; cin >> n >> q; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '1') { ones[i + 1] = ones[i] + 1; } else { ones[i + 1] = ones[i]; } } for (int i = 0; i < q; i++) { cin >> a >> b; int length = b - a + 1; int one = ones[b] - ones[a - 1]; int zero = length - one; if (zero == length) { cout << 0 << "\n"; } else { long long int ans = modpow(2 * 1LL, 1LL * one, M1) - 1; ans += GPmodM(ans, 1LL * 2, zero); cout << ans % M1 << "\n"; } } } ```
#include <bits/stdc++.h> using namespace std; long long a1[200009]; long long a0[200009]; long long mod = 1e9 + 7; long long bigmod(long long base, long long pow) { long long r1, r2; if (pow <= 0) return 1; if (pow % 2 != 0) { r1 = bigmod(2, pow - 1); r2 = (r1 * 2) % mod; return r2; } else if (pow % 2 == 0) { r1 = bigmod(2, pow / 2); r2 = (r1 * r1) % mod; return r2; } } int main() { long long n, q, i, j, ans1, one, zero, p, q2, q1, l, r; string s, s1; cin >> n >> q; cin >> s1; s += '#'; s += s1; for (i = 1; i <= n; i++) { a1[i] = a1[i - 1]; a0[i] = a0[i - 1]; if (s[i] == '1') a1[i]++; else a0[i]++; } for (i = 1; i <= q; i++) { cin >> l >> r; one = a1[r] - a1[l - 1]; zero = a0[r] - a0[l - 1]; if (one >= 1) { p = bigmod(2, one); p--; q1 = bigmod(2, zero); q1--; ans1 = (q1 * p) % mod; cout << ((ans1 + p) % mod) << endl; } else cout << 0 << endl; } }
### Prompt Create a solution in cpp for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a1[200009]; long long a0[200009]; long long mod = 1e9 + 7; long long bigmod(long long base, long long pow) { long long r1, r2; if (pow <= 0) return 1; if (pow % 2 != 0) { r1 = bigmod(2, pow - 1); r2 = (r1 * 2) % mod; return r2; } else if (pow % 2 == 0) { r1 = bigmod(2, pow / 2); r2 = (r1 * r1) % mod; return r2; } } int main() { long long n, q, i, j, ans1, one, zero, p, q2, q1, l, r; string s, s1; cin >> n >> q; cin >> s1; s += '#'; s += s1; for (i = 1; i <= n; i++) { a1[i] = a1[i - 1]; a0[i] = a0[i - 1]; if (s[i] == '1') a1[i]++; else a0[i]++; } for (i = 1; i <= q; i++) { cin >> l >> r; one = a1[r] - a1[l - 1]; zero = a0[r] - a0[l - 1]; if (one >= 1) { p = bigmod(2, one); p--; q1 = bigmod(2, zero); q1--; ans1 = (q1 * p) % mod; cout << ((ans1 + p) % mod) << endl; } else cout << 0 << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = (long long)1e9 + 7; long long mod_pow(long long x, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } signed main() { long long n, q; cin >> n >> q; string s; cin >> s; vector<long long> csum1(n + 1, 0), csum0(n + 1, 0); for (long long i = 0; i < n; i++) { csum0[i + 1] = csum0[i]; csum1[i + 1] = csum1[i]; if (s[i] == '0') csum0[i + 1]++; if (s[i] == '1') csum1[i + 1]++; } for (long long i = 0; i < q; i++) { long long l, r; cin >> l >> r; long long ans = 0; long long cnt1 = csum1[r] - csum1[l - 1]; long long cnt0 = csum0[r] - csum0[l - 1]; ans += mod_pow(2, cnt1) - 1; ans %= MOD; ans += (mod_pow(2, cnt0) - 1) * (mod_pow(2, cnt1) - 1); ans %= MOD; cout << ans << endl; } }
### Prompt Please formulate a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = (long long)1e9 + 7; long long mod_pow(long long x, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } signed main() { long long n, q; cin >> n >> q; string s; cin >> s; vector<long long> csum1(n + 1, 0), csum0(n + 1, 0); for (long long i = 0; i < n; i++) { csum0[i + 1] = csum0[i]; csum1[i + 1] = csum1[i]; if (s[i] == '0') csum0[i + 1]++; if (s[i] == '1') csum1[i + 1]++; } for (long long i = 0; i < q; i++) { long long l, r; cin >> l >> r; long long ans = 0; long long cnt1 = csum1[r] - csum1[l - 1]; long long cnt0 = csum0[r] - csum0[l - 1]; ans += mod_pow(2, cnt1) - 1; ans %= MOD; ans += (mod_pow(2, cnt0) - 1) * (mod_pow(2, cnt1) - 1); ans %= MOD; cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long solve(long long good, long long len, vector<long long> &pow) { long long rest = len - good; long long ans = pow[good] - 1; long long help = (ans * (pow[rest] - 1)) % mod; ans += help; ans %= mod; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<long long> pow(100005); pow[0] = 1; for (int i = 1; i < 1e5 + 5; i++) { pow[i] = (pow[i - 1] * 2) % mod; } int n, q; cin >> n >> q; string s; cin >> s; int x; vector<int> pref(n + 1); pref[0] = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') x = 0; else x = 1; pref[i + 1] = pref[i] + x; } int L, R; for (int i = 0; i < q; i++) { cin >> L >> R; int len = (R - L + 1); int good = pref[R] - pref[L - 1]; cout << solve(good, len, pow) << endl; } }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long solve(long long good, long long len, vector<long long> &pow) { long long rest = len - good; long long ans = pow[good] - 1; long long help = (ans * (pow[rest] - 1)) % mod; ans += help; ans %= mod; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<long long> pow(100005); pow[0] = 1; for (int i = 1; i < 1e5 + 5; i++) { pow[i] = (pow[i - 1] * 2) % mod; } int n, q; cin >> n >> q; string s; cin >> s; int x; vector<int> pref(n + 1); pref[0] = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') x = 0; else x = 1; pref[i + 1] = pref[i] + x; } int L, R; for (int i = 0; i < q; i++) { cin >> L >> R; int len = (R - L + 1); int good = pref[R] - pref[L - 1]; cout << solve(good, len, pow) << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long fenwick[100005]; void update(long long value, long long where, long long n) { while (where <= n) { fenwick[where] += value; where += (where & -where); } } long long query(long long where) { long long res = 0; while (where) { res += fenwick[where]; where -= (where & -where); } return res; } void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long p[100007]; long long exp(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; long long half = exp(a, b / 2) % 1000000007; if (b % 2 == 1) return ((half * half) % 1000000007 * a % 1000000007) % 1000000007; return (half * half) % 1000000007; } int main() { fastio(); long long n, q; cin >> n >> q; vector<long long> arr; memset(fenwick, 0, sizeof fenwick); memset(p, 0, sizeof p); p[0] = 0; for (int i = 1; i < 100007; i++) { if (i == 1) { p[i] = 2; continue; } long long lo = exp(2, i); p[i] = ((p[i - 1] % 1000000007) + lo % 1000000007) % 1000000007; p[i] %= 1000000007; } string s; cin >> s; for (long long i = 0; i < n; i++) update((long long)s[i] - '0', i + 1, n); while (q--) { long long l, r; cin >> l >> r; long long x = query(r) - query(l - 1); long long ans = 0; if (r - l + 1 == x) cout << ((((p[(r - l)] % 1000000007 - p[(r - l + 1 - x) - 1] % 1000000007) + 1000000007) % 1000000007) + 1) % 1000000007 << "\n"; else cout << (((p[(r - l)] % 1000000007 - p[(r - l + 1 - x) - 1] % 1000000007) + 1000000007) % 1000000007) << "\n"; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long fenwick[100005]; void update(long long value, long long where, long long n) { while (where <= n) { fenwick[where] += value; where += (where & -where); } } long long query(long long where) { long long res = 0; while (where) { res += fenwick[where]; where -= (where & -where); } return res; } void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long p[100007]; long long exp(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; long long half = exp(a, b / 2) % 1000000007; if (b % 2 == 1) return ((half * half) % 1000000007 * a % 1000000007) % 1000000007; return (half * half) % 1000000007; } int main() { fastio(); long long n, q; cin >> n >> q; vector<long long> arr; memset(fenwick, 0, sizeof fenwick); memset(p, 0, sizeof p); p[0] = 0; for (int i = 1; i < 100007; i++) { if (i == 1) { p[i] = 2; continue; } long long lo = exp(2, i); p[i] = ((p[i - 1] % 1000000007) + lo % 1000000007) % 1000000007; p[i] %= 1000000007; } string s; cin >> s; for (long long i = 0; i < n; i++) update((long long)s[i] - '0', i + 1, n); while (q--) { long long l, r; cin >> l >> r; long long x = query(r) - query(l - 1); long long ans = 0; if (r - l + 1 == x) cout << ((((p[(r - l)] % 1000000007 - p[(r - l + 1 - x) - 1] % 1000000007) + 1000000007) % 1000000007) + 1) % 1000000007 << "\n"; else cout << (((p[(r - l)] % 1000000007 - p[(r - l + 1 - x) - 1] % 1000000007) + 1000000007) % 1000000007) << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = 100005; int q, n, l, r, ans; char c; long long sum[MAXN], sumPow[MAXN]; void init() { sumPow[0] = 1; long long temp = 1; for (int i = 1; i <= MAXN; i++) { temp = temp * 2 % MOD; sumPow[i] = (sumPow[i - 1] % MOD + temp % MOD) % MOD; } } int main() { ios::sync_with_stdio(false); init(); cin >> n >> q; sum[0] = 0; for (int i = 1; i <= n; i++) { cin >> c; sum[i] = sum[i - 1] + (c == '0' ? 1 : 0); } for (int i = 0; i < q; i++) { cin >> l >> r; int x = sum[r] - sum[l - 1]; int size = r - l + 1; ans = (sumPow[size - 1] - (x == 0 ? 0 : sumPow[x - 1]) + MOD) % MOD; printf("%d\n", ans); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = 100005; int q, n, l, r, ans; char c; long long sum[MAXN], sumPow[MAXN]; void init() { sumPow[0] = 1; long long temp = 1; for (int i = 1; i <= MAXN; i++) { temp = temp * 2 % MOD; sumPow[i] = (sumPow[i - 1] % MOD + temp % MOD) % MOD; } } int main() { ios::sync_with_stdio(false); init(); cin >> n >> q; sum[0] = 0; for (int i = 1; i <= n; i++) { cin >> c; sum[i] = sum[i - 1] + (c == '0' ? 1 : 0); } for (int i = 0; i < q; i++) { cin >> l >> r; int x = sum[r] - sum[l - 1]; int size = r - l + 1; ans = (sumPow[size - 1] - (x == 0 ? 0 : sumPow[x - 1]) + MOD) % MOD; printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int m = (int)1e9 + 7; int mult(int a, int b) { return (long long)a * b % m; } void add(int& a, int b) { a += b; if (a >= m) { a -= m; } } int mpow(int a, int b) { int ret = 1; while (b) { if (b & 1) { ret = mult(ret, a); } a = mult(a, a); b >>= 1; } return ret; } int main() { int n, q; cin >> n >> q; string s; cin >> s; vector<int> arr; vector<int> ones = {0}; for (int i = 0; i < n; ++i) { arr.push_back(s[i] - '0'); ones.push_back(ones.back() + arr.back()); } for (int i = 0; i < q; ++i) { int l, r; cin >> l >> r; int length = r - l + 1; int sums = ones[r] - ones[l - 1]; int a = mpow(2, sums); int ans = 0; ans = (ans + a - 1) % m; int mul = a - 1; int b = mpow(2, length - sums); int c = 0; if (b <= 1) c = 0; else { c = (mult((b - 1), mul)) % m; } add(ans, c); ans = ans % m; cout << ans << endl; } }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int m = (int)1e9 + 7; int mult(int a, int b) { return (long long)a * b % m; } void add(int& a, int b) { a += b; if (a >= m) { a -= m; } } int mpow(int a, int b) { int ret = 1; while (b) { if (b & 1) { ret = mult(ret, a); } a = mult(a, a); b >>= 1; } return ret; } int main() { int n, q; cin >> n >> q; string s; cin >> s; vector<int> arr; vector<int> ones = {0}; for (int i = 0; i < n; ++i) { arr.push_back(s[i] - '0'); ones.push_back(ones.back() + arr.back()); } for (int i = 0; i < q; ++i) { int l, r; cin >> l >> r; int length = r - l + 1; int sums = ones[r] - ones[l - 1]; int a = mpow(2, sums); int ans = 0; ans = (ans + a - 1) % m; int mul = a - 1; int b = mpow(2, length - sums); int c = 0; if (b <= 1) c = 0; else { c = (mult((b - 1), mul)) % m; } add(ans, c); ans = ans % m; cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long n, q, a[100005], b[100005], l, r, _2[100005]; char s[100005]; int main() { _2[0] = 1; cin >> n >> q; for (int i = 1; i <= n; i++) _2[i] = _2[i - 1] * 2 % 1000000007; scanf("%s", s); a[0] = b[0] = 0; for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + (s[i - 1] == '1'); b[i] = b[i - 1] + (s[i - 1] == '0'); } while (q--) { scanf("%lld%lld", &l, &r); int x = a[r] - a[l - 1], y = b[r] - b[l - 1]; printf("%lld\n", (_2[x] - 1 + 1000000007) * _2[y] % 1000000007); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, q, a[100005], b[100005], l, r, _2[100005]; char s[100005]; int main() { _2[0] = 1; cin >> n >> q; for (int i = 1; i <= n; i++) _2[i] = _2[i - 1] * 2 % 1000000007; scanf("%s", s); a[0] = b[0] = 0; for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + (s[i - 1] == '1'); b[i] = b[i - 1] + (s[i - 1] == '0'); } while (q--) { scanf("%lld%lld", &l, &r); int x = a[r] - a[l - 1], y = b[r] - b[l - 1]; printf("%lld\n", (_2[x] - 1 + 1000000007) * _2[y] % 1000000007); } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll N = 1e5 + 10; const ll inf = 1e18 + 10; const ll MOD = 1e9 + 7; ll n, q, cum[N]; ll add(ll x, ll y) { return (x + y) % MOD; } ll mult(ll a, ll b) { ll res = a * b; res = res % MOD; return res; } ll binpow(ll a, ll b, ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll d(ll x, ll y) { return mult(x, binpow(y, MOD - 2, MOD)); } ll subtract(ll x, ll y) { ll res = x - y; if (res < 0) res += MOD; return res; } int main() { cin >> n >> q; string s; cin >> s; for (ll i = 1; i <= n; i++) { cum[i] = cum[i - 1] + s[i - 1] - '0'; } while (q--) { ll l, r; cin >> l >> r; ll one = cum[r] - cum[l - 1]; ll zero = r - l + 1 - one; ll ans = mult(subtract(binpow(2, one, MOD), 1), binpow(2, zero, MOD)); printf("%d\n", ans); } return 0; }
### Prompt In CPP, your task is to solve the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; const ll N = 1e5 + 10; const ll inf = 1e18 + 10; const ll MOD = 1e9 + 7; ll n, q, cum[N]; ll add(ll x, ll y) { return (x + y) % MOD; } ll mult(ll a, ll b) { ll res = a * b; res = res % MOD; return res; } ll binpow(ll a, ll b, ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll d(ll x, ll y) { return mult(x, binpow(y, MOD - 2, MOD)); } ll subtract(ll x, ll y) { ll res = x - y; if (res < 0) res += MOD; return res; } int main() { cin >> n >> q; string s; cin >> s; for (ll i = 1; i <= n; i++) { cum[i] = cum[i - 1] + s[i - 1] - '0'; } while (q--) { ll l, r; cin >> l >> r; ll one = cum[r] - cum[l - 1]; ll zero = r - l + 1 - one; ll ans = mult(subtract(binpow(2, one, MOD), 1), binpow(2, zero, MOD)); printf("%d\n", ans); } return 0; } ```
#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; }
### Prompt Please formulate a CPP solution to the following problem: 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. ### Response ```cpp #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; } ```
#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); } }
### Prompt Please create a solution in CPP to the following problem: 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. ### Response ```cpp #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); } } ```
#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; }
### Prompt Create a solution in Cpp for the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Your task is to create a CPP solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; } }
### Prompt Please formulate a cpp solution to the following problem: 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. ### Response ```cpp #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; } } ```
#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; }
### Prompt Please provide a Cpp coded solution to the problem described below: 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. ### Response ```cpp #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; } ```
#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"; } }
### Prompt Create a solution in Cpp for the following problem: 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. ### Response ```cpp #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"; } } ```
#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; }
### Prompt Construct a Cpp code solution to the problem outlined: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Construct a CPP code solution to the problem outlined: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Please formulate a Cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Your task is to create a Cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Please provide a Cpp coded solution to the problem described below: 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. ### Response ```cpp #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; } ```
#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; } }
### Prompt Please provide a cpp coded solution to the problem described below: 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. ### Response ```cpp #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; } } ```
#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; }
### Prompt Construct a CPP code solution to the problem outlined: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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"; } }
### Prompt Generate a CPP solution to the following problem: 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. ### Response ```cpp #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"; } } ```
#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"; } }
### Prompt Please formulate a CPP solution to the following problem: 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. ### Response ```cpp #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"; } } ```
#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; }
### Prompt Construct a CPP code solution to the problem outlined: 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. ### Response ```cpp #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; } ```
#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; } }
### Prompt Generate a Cpp solution to the following problem: 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. ### Response ```cpp #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; } } ```
#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; }
### Prompt Please create a solution in CPP to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Your task is to create a cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Generate a Cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Develop a solution in Cpp to the problem described below: 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. ### Response ```cpp #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; } ```
#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; } }
### Prompt Please create a solution in Cpp to the following problem: 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. ### Response ```cpp #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; } } ```
#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; }
### Prompt In CPP, your task is to solve the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #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; } ```
#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'; } }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #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'; } } ```
#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); } }
### Prompt Construct a Cpp code solution to the problem outlined: 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. ### Response ```cpp #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); } } ```
#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; }
### Prompt Generate a Cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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"; } }
### Prompt Please create a solution in cpp to the following problem: 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. ### Response ```cpp #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"; } } ```
#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; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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(); }
### Prompt Please provide a Cpp coded solution to the problem described below: 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. ### Response ```cpp #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(); } ```
#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; }
### Prompt Please formulate a cpp solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Your challenge is to write a CPP solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; } }
### Prompt Please formulate a Cpp solution to the following problem: 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. ### Response ```cpp #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; } } ```
#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; }
### Prompt Please create a solution in CPP to the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #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; } ```
#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; }
### Prompt Develop a solution in Cpp to the problem described below: 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. ### Response ```cpp #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; } ```
#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)); } }
### Prompt Generate a Cpp solution to the following problem: 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. ### Response ```cpp #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)); } } ```
#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; }
### Prompt Your challenge is to write a CPP solution to the following problem: 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. ### Response ```cpp #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; } ```
#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; ; }
### Prompt Create a solution in cpp for the following problem: 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. ### Response ```cpp #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; ; } ```
#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"; } }
### Prompt Please provide a Cpp coded solution to the problem described below: 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. ### Response ```cpp #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"; } } ```
#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; } }
### Prompt Develop a solution in Cpp to the problem described below: 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. ### Response ```cpp #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; } } ```
#include <bits/stdc++.h> using namespace std; char ch[1000010]; int num[1000010]; int pre[1000010]; const long long mod = 1e9 + 7; int qpow(int a, int b) { long long ans = 1; long long ta = a, tb = b; while (tb) { if (tb & 1) ans = ans * ta % mod; tb >>= 1; ta = ta * ta % mod; } return (int)(ans % mod); } int solve(int l, int r) { int len = r - l + 1; int n = pre[r] - pre[l - 1]; long long ans = (mod + qpow(2, n) - 1) % mod; long long tmp = (qpow(2, len - n) - 1 + mod) % mod * ans % mod; ans = (ans + tmp) % mod; return (int)ans; } int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", ch + 1); for (int i = 1; i <= n; i++) { num[i] = ch[i] - '0'; pre[i] = pre[i - 1] + num[i]; } int l, r; for (int i = 0; i < q; i++) { scanf("%d%d", &l, &r); printf("%d\n", solve(l, r)); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char ch[1000010]; int num[1000010]; int pre[1000010]; const long long mod = 1e9 + 7; int qpow(int a, int b) { long long ans = 1; long long ta = a, tb = b; while (tb) { if (tb & 1) ans = ans * ta % mod; tb >>= 1; ta = ta * ta % mod; } return (int)(ans % mod); } int solve(int l, int r) { int len = r - l + 1; int n = pre[r] - pre[l - 1]; long long ans = (mod + qpow(2, n) - 1) % mod; long long tmp = (qpow(2, len - n) - 1 + mod) % mod * ans % mod; ans = (ans + tmp) % mod; return (int)ans; } int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", ch + 1); for (int i = 1; i <= n; i++) { num[i] = ch[i] - '0'; pre[i] = pre[i - 1] + num[i]; } int l, r; for (int i = 0; i < q; i++) { scanf("%d%d", &l, &r); printf("%d\n", solve(l, r)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXINT = 2147483647; const long long MAXLL = 9223372036854775807LL; const int MAX = 400000; long long n, q, col, pref[MAX], l, r, sum, ans, t[MAX]; string s; int main() { cin >> n >> q; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '1') col++; pref[i] = col; } t[0] = 0; for (int i = 1; i <= 100005; i++) t[i] = (t[i - 1] + t[i - 1] + 1) % 1000000007; for (int i = 0; i < q; i++) { ans = 0, col = 0; cin >> l >> r; l--, r--; if (l == 0) sum = pref[r]; else sum = pref[r] - pref[l - 1]; if (t[r - l + 1] >= t[r - l + 1 - sum]) ans += t[r - l + 1] - t[r - l + 1 - sum]; else ans += t[r - l + 1] + 1000000007 - t[r - l + 1 - sum]; cout << ans << '\n'; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXINT = 2147483647; const long long MAXLL = 9223372036854775807LL; const int MAX = 400000; long long n, q, col, pref[MAX], l, r, sum, ans, t[MAX]; string s; int main() { cin >> n >> q; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '1') col++; pref[i] = col; } t[0] = 0; for (int i = 1; i <= 100005; i++) t[i] = (t[i - 1] + t[i - 1] + 1) % 1000000007; for (int i = 0; i < q; i++) { ans = 0, col = 0; cin >> l >> r; l--, r--; if (l == 0) sum = pref[r]; else sum = pref[r] - pref[l - 1]; if (t[r - l + 1] >= t[r - l + 1 - sum]) ans += t[r - l + 1] - t[r - l + 1 - sum]; else ans += t[r - l + 1] + 1000000007 - t[r - l + 1 - sum]; cout << ans << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { long long n, q, a[100005]; int z[100005], o[100005]; scanf("%lld%lld", &n, &q); char s[100005]; scanf("%s", s); long long n2 = 1; z[0] = 0, o[0] = 0, a[0] = 0, a[1] = 1, n2 *= 2; for (long long i = 2; i < 100005; i++) a[i] = (a[i - 1] + n2) % mod, n2 = n2 * 2 % mod; for (int i = 1; i <= n; i++) { z[i] = z[i - 1], o[i] = o[i - 1]; if (s[i - 1] == '0') z[i]++; else o[i]++; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long an = ((a[z[r] - z[l - 1]] + 1) * (a[o[r] - o[l - 1]])) % mod; printf("%lld\n", an); } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { long long n, q, a[100005]; int z[100005], o[100005]; scanf("%lld%lld", &n, &q); char s[100005]; scanf("%s", s); long long n2 = 1; z[0] = 0, o[0] = 0, a[0] = 0, a[1] = 1, n2 *= 2; for (long long i = 2; i < 100005; i++) a[i] = (a[i - 1] + n2) % mod, n2 = n2 * 2 % mod; for (int i = 1; i <= n; i++) { z[i] = z[i - 1], o[i] = o[i - 1]; if (s[i - 1] == '0') z[i]++; else o[i]++; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long an = ((a[z[r] - z[l - 1]] + 1) * (a[o[r] - o[l - 1]])) % mod; printf("%lld\n", an); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int64_t MAXN = 1e5 + 134, M = 1e9 + 7; int64_t pr[MAXN], q[MAXN]; int64_t F(int64_t i) { if (i == 0) return 1; int64_t x = F(i / 2); if (i % 2 == 0) return (x * x) % M; else return (2 * x * x) % M; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int64_t n, k, x; cin >> n >> k; pr[0] = 0; for (int64_t i = 0; i < n; i++) { char c; cin >> c; q[i] = c - '0'; pr[i + 1] = pr[i] + q[i]; } for (int64_t i = 0; i < k; i++) { int64_t l, r; cin >> l >> r; l--; r--; int64_t a = pr[r + 1] - pr[l]; int64_t b = r - l + 1 - a; int64_t ans; ans = ((F(a) + M - 1) % M * F(b)) % M; cout << ans << "\n"; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int64_t MAXN = 1e5 + 134, M = 1e9 + 7; int64_t pr[MAXN], q[MAXN]; int64_t F(int64_t i) { if (i == 0) return 1; int64_t x = F(i / 2); if (i % 2 == 0) return (x * x) % M; else return (2 * x * x) % M; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int64_t n, k, x; cin >> n >> k; pr[0] = 0; for (int64_t i = 0; i < n; i++) { char c; cin >> c; q[i] = c - '0'; pr[i + 1] = pr[i] + q[i]; } for (int64_t i = 0; i < k; i++) { int64_t l, r; cin >> l >> r; l--; r--; int64_t a = pr[r + 1] - pr[l]; int64_t b = r - l + 1 - a; int64_t ans; ans = ((F(a) + M - 1) % M * F(b)) % M; cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> long long qs[100005], po[20]; char a[100005]; int main() { long long n, q, l, r, us, num, ans, i; scanf("%lld %lld", &n, &q); scanf(" %s", &a[1]); for (i = 1; i <= n; i++) qs[i] = qs[i - 1] + a[i] - '0'; po[0] = 2; for (i = 1; i < 20; i++) po[i] = (po[i - 1] * po[i - 1]) % 1000000007; while (q--) { scanf("%lld %lld", &l, &r); ans = 0; us = 1; num = qs[r] - qs[l - 1]; for (i = 0; i < 20; i++) { if ((1ll << i) & num) { us *= po[i]; us %= 1000000007; } } num = r - l + 1 - num; ans = (us - 1 + 1000000007) % 1000000007; for (i = 0; i < 20; i++) { if ((1ll << i) & num) { ans *= po[i]; ans %= 1000000007; } } printf("%lld\n", ans); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> long long qs[100005], po[20]; char a[100005]; int main() { long long n, q, l, r, us, num, ans, i; scanf("%lld %lld", &n, &q); scanf(" %s", &a[1]); for (i = 1; i <= n; i++) qs[i] = qs[i - 1] + a[i] - '0'; po[0] = 2; for (i = 1; i < 20; i++) po[i] = (po[i - 1] * po[i - 1]) % 1000000007; while (q--) { scanf("%lld %lld", &l, &r); ans = 0; us = 1; num = qs[r] - qs[l - 1]; for (i = 0; i < 20; i++) { if ((1ll << i) & num) { us *= po[i]; us %= 1000000007; } } num = r - l + 1 - num; ans = (us - 1 + 1000000007) % 1000000007; for (i = 0; i < 20; i++) { if ((1ll << i) & num) { ans *= po[i]; ans %= 1000000007; } } printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INFL = (int)1e9; const long long int INFLL = (long long int)1e18; const double INFD = numeric_limits<double>::infinity(); const double PI = 3.14159265358979323846; bool nearlyeq(double x, double y) { return abs(x - y) < 1e-9; } bool inrange(int x, int t) { return x >= 0 && x < t; } long long int rndf(double x) { return (long long int)(x + (x >= 0 ? 0.5 : -0.5)); } long long int floorsqrt(double x) { long long int m = (long long int)sqrt(x); return m + (m * m <= (long long int)(x) ? 0 : -1); } long long int ceilsqrt(double x) { long long int m = (long long int)sqrt(x); return m + ((long long int)x <= m * m ? 0 : 1); } long long int rnddiv(long long int a, long long int b) { return (a / b + (a % b * 2 >= b ? 1 : 0)); } long long int ceildiv(long long int a, long long int b) { return (a / b + (a % b == 0 ? 0 : 1)); } long long int gcd(long long int m, long long int n) { if (n == 0) return m; else return gcd(n, m % n); } namespace mod_op { const long long int MOD = (long long int)1e9 + 7; class modll { private: long long int val; inline long long int modify(long long int x) { long long int ret = x % MOD; if (ret < 0) ret += MOD; return ret; } inline long long int inv(long long int x) { if (x == 0) return 1 / x; else if (x == 1) return 1; else return modify(inv(MOD % x) * modify(-MOD / x)); } public: modll(long long int init = 0) { val = modify(init); return; } modll(const modll &another) { val = another.val; return; } inline modll &operator=(const modll &another) { val = another.val; return *this; } inline modll operator+(const modll &x) { return modify(val + x.val); } inline modll operator-(const modll &x) { return modify(val - x.val); } inline modll operator*(const modll &x) { return modify(val * x.val); } inline modll operator/(const modll &x) { return modify(val * inv(x.val)); } inline modll &operator+=(const modll &x) { val = modify(val + x.val); return *this; } inline modll &operator-=(const modll &x) { val = modify(val - x.val); return *this; } inline modll &operator*=(const modll &x) { val = modify(val * x.val); return *this; } inline modll &operator/=(const modll &x) { val = modify(val * inv(x.val)); return *this; } inline bool operator==(const modll &x) { return val == x.val; } inline bool operator!=(const modll &x) { return val != x.val; } friend inline istream &operator>>(istream &is, modll &x) { is >> x.val; return is; } friend inline ostream &operator<<(ostream &os, const modll &x) { os << x.val; return os; } long long int get_val() { return val; } }; modll pow(modll n, long long int p) { modll ret; if (p == 0) ret = 1; else if (p == 1) ret = n; else { ret = pow(n, p / 2); ret *= ret; if (p % 2 == 1) ret *= n; } return ret; } vector<modll> facts; inline void make_facts(int n) { if (facts.empty()) facts.push_back(modll(1)); for (int i = (int)facts.size(); i <= n; ++i) facts.push_back(modll(facts.back() * (long long int)i)); return; } vector<modll> ifacts; vector<modll> invs; inline void make_invs(int n) { if (invs.empty()) { invs.push_back(modll(0)); invs.push_back(modll(1)); } for (int i = (int)invs.size(); i <= n; ++i) { invs.push_back(invs[(int)MOD % i] * ((int)MOD - (int)MOD / i)); } return; } inline void make_ifacts(int n) { make_invs(n); if (ifacts.empty()) ifacts.push_back(modll(1)); for (int i = (int)ifacts.size(); i <= n; ++i) ifacts.push_back(modll(ifacts.back() * invs[i])); return; } modll combination(long long int n, long long int r) { if (n >= r && r >= 0) { modll ret; make_facts((int)n); make_ifacts((int)n); ret = facts[(unsigned)n] * ifacts[(unsigned)r] * ifacts[(unsigned)(n - r)]; return ret; } else return 0; } modll get_fact(long long int n) { make_facts((int)n); return facts[(int)n]; } modll get_ifact(long long int n) { make_ifacts((int)n); return ifacts[(int)n]; } long long int disc_log(modll a, modll b) { long long int ret = -1; long long int m = ceilsqrt(MOD); unordered_map<long long int, long long int> mp; modll x = 1; for (int i = 0; i < (int)m; i++) { mp[x.get_val()] = i; x *= a; } x = modll(1) / pow(a, m); modll k = b; for (int i = 0; i < (int)m; i++) { if (mp.find(k.get_val()) == mp.end()) k *= x; else { ret = i * m + mp[k.get_val()]; break; } } return ret; } } // namespace mod_op using namespace mod_op; int main() { ios::sync_with_stdio(false); cin.tie(0); ; int n; cin >> n; int q; cin >> q; string s; cin >> s; vector<long long int> sum(n + 1, 0); for (int i = 0; i < (int)n; i++) sum[i + 1] = sum[i] + (s[i] == '1' ? 1 : 0); for (int unused = 0; unused < (int)q; unused++) { int s, t; cin >> s >> t; long long int m = t - s + 1; long long int cnt1 = sum[t] - sum[s - 1]; long long int cnt0 = m - cnt1; modll buf = pow(modll(2), cnt1) - 1; modll buf2 = pow(modll(2), cnt0) - 1; modll ans = buf + buf * buf2; cout << ans << "\n"; } }
### Prompt Generate a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INFL = (int)1e9; const long long int INFLL = (long long int)1e18; const double INFD = numeric_limits<double>::infinity(); const double PI = 3.14159265358979323846; bool nearlyeq(double x, double y) { return abs(x - y) < 1e-9; } bool inrange(int x, int t) { return x >= 0 && x < t; } long long int rndf(double x) { return (long long int)(x + (x >= 0 ? 0.5 : -0.5)); } long long int floorsqrt(double x) { long long int m = (long long int)sqrt(x); return m + (m * m <= (long long int)(x) ? 0 : -1); } long long int ceilsqrt(double x) { long long int m = (long long int)sqrt(x); return m + ((long long int)x <= m * m ? 0 : 1); } long long int rnddiv(long long int a, long long int b) { return (a / b + (a % b * 2 >= b ? 1 : 0)); } long long int ceildiv(long long int a, long long int b) { return (a / b + (a % b == 0 ? 0 : 1)); } long long int gcd(long long int m, long long int n) { if (n == 0) return m; else return gcd(n, m % n); } namespace mod_op { const long long int MOD = (long long int)1e9 + 7; class modll { private: long long int val; inline long long int modify(long long int x) { long long int ret = x % MOD; if (ret < 0) ret += MOD; return ret; } inline long long int inv(long long int x) { if (x == 0) return 1 / x; else if (x == 1) return 1; else return modify(inv(MOD % x) * modify(-MOD / x)); } public: modll(long long int init = 0) { val = modify(init); return; } modll(const modll &another) { val = another.val; return; } inline modll &operator=(const modll &another) { val = another.val; return *this; } inline modll operator+(const modll &x) { return modify(val + x.val); } inline modll operator-(const modll &x) { return modify(val - x.val); } inline modll operator*(const modll &x) { return modify(val * x.val); } inline modll operator/(const modll &x) { return modify(val * inv(x.val)); } inline modll &operator+=(const modll &x) { val = modify(val + x.val); return *this; } inline modll &operator-=(const modll &x) { val = modify(val - x.val); return *this; } inline modll &operator*=(const modll &x) { val = modify(val * x.val); return *this; } inline modll &operator/=(const modll &x) { val = modify(val * inv(x.val)); return *this; } inline bool operator==(const modll &x) { return val == x.val; } inline bool operator!=(const modll &x) { return val != x.val; } friend inline istream &operator>>(istream &is, modll &x) { is >> x.val; return is; } friend inline ostream &operator<<(ostream &os, const modll &x) { os << x.val; return os; } long long int get_val() { return val; } }; modll pow(modll n, long long int p) { modll ret; if (p == 0) ret = 1; else if (p == 1) ret = n; else { ret = pow(n, p / 2); ret *= ret; if (p % 2 == 1) ret *= n; } return ret; } vector<modll> facts; inline void make_facts(int n) { if (facts.empty()) facts.push_back(modll(1)); for (int i = (int)facts.size(); i <= n; ++i) facts.push_back(modll(facts.back() * (long long int)i)); return; } vector<modll> ifacts; vector<modll> invs; inline void make_invs(int n) { if (invs.empty()) { invs.push_back(modll(0)); invs.push_back(modll(1)); } for (int i = (int)invs.size(); i <= n; ++i) { invs.push_back(invs[(int)MOD % i] * ((int)MOD - (int)MOD / i)); } return; } inline void make_ifacts(int n) { make_invs(n); if (ifacts.empty()) ifacts.push_back(modll(1)); for (int i = (int)ifacts.size(); i <= n; ++i) ifacts.push_back(modll(ifacts.back() * invs[i])); return; } modll combination(long long int n, long long int r) { if (n >= r && r >= 0) { modll ret; make_facts((int)n); make_ifacts((int)n); ret = facts[(unsigned)n] * ifacts[(unsigned)r] * ifacts[(unsigned)(n - r)]; return ret; } else return 0; } modll get_fact(long long int n) { make_facts((int)n); return facts[(int)n]; } modll get_ifact(long long int n) { make_ifacts((int)n); return ifacts[(int)n]; } long long int disc_log(modll a, modll b) { long long int ret = -1; long long int m = ceilsqrt(MOD); unordered_map<long long int, long long int> mp; modll x = 1; for (int i = 0; i < (int)m; i++) { mp[x.get_val()] = i; x *= a; } x = modll(1) / pow(a, m); modll k = b; for (int i = 0; i < (int)m; i++) { if (mp.find(k.get_val()) == mp.end()) k *= x; else { ret = i * m + mp[k.get_val()]; break; } } return ret; } } // namespace mod_op using namespace mod_op; int main() { ios::sync_with_stdio(false); cin.tie(0); ; int n; cin >> n; int q; cin >> q; string s; cin >> s; vector<long long int> sum(n + 1, 0); for (int i = 0; i < (int)n; i++) sum[i + 1] = sum[i] + (s[i] == '1' ? 1 : 0); for (int unused = 0; unused < (int)q; unused++) { int s, t; cin >> s >> t; long long int m = t - s + 1; long long int cnt1 = sum[t] - sum[s - 1]; long long int cnt0 = m - cnt1; modll buf = pow(modll(2), cnt1) - 1; modll buf2 = pow(modll(2), cnt0) - 1; modll ans = buf + buf * buf2; cout << ans << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; long long power(long long n, long long x) { if (x == 0) return 1; if (x & 1) { return (n * (power((n * n) % 1000000007, x / 2))) % 1000000007; } else { return (power((n * n) % 1000000007, x / 2)) % 1000000007; } } void solve() { long long n; long long q; cin >> n; cin >> q; string s; cin >> s; long long pre[n][2]; for (long long i = 0; i < n; i++) { if (i == 0) { if (s[i] == '1') { pre[i][0] = 0; pre[i][1] = 1; } else { pre[i][0] = 1; pre[i][1] = 0; } } else { if (s[i] == '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--) { long long l, r; cin >> l >> r; l--; r--; long long o, z; if (l - 1 >= 0) { o = pre[r][1] - pre[l - 1][1]; z = pre[r][0] - pre[l - 1][0]; } else { o = pre[r][1]; z = pre[r][0]; } long long oneSum = power(2, o) - 1; long long zeroSum = (oneSum * (power(2, z) - 1)) % 1000000007; cout << (oneSum + zeroSum) % 1000000007 << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T; T = 1; while (T--) { solve(); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long power(long long n, long long x) { if (x == 0) return 1; if (x & 1) { return (n * (power((n * n) % 1000000007, x / 2))) % 1000000007; } else { return (power((n * n) % 1000000007, x / 2)) % 1000000007; } } void solve() { long long n; long long q; cin >> n; cin >> q; string s; cin >> s; long long pre[n][2]; for (long long i = 0; i < n; i++) { if (i == 0) { if (s[i] == '1') { pre[i][0] = 0; pre[i][1] = 1; } else { pre[i][0] = 1; pre[i][1] = 0; } } else { if (s[i] == '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--) { long long l, r; cin >> l >> r; l--; r--; long long o, z; if (l - 1 >= 0) { o = pre[r][1] - pre[l - 1][1]; z = pre[r][0] - pre[l - 1][0]; } else { o = pre[r][1]; z = pre[r][0]; } long long oneSum = power(2, o) - 1; long long zeroSum = (oneSum * (power(2, z) - 1)) % 1000000007; cout << (oneSum + zeroSum) % 1000000007 << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T; T = 1; while (T--) { solve(); } return 0; } ```
#include <bits/stdc++.h> const int mod = 1e9 + 7; using namespace std; long long bin_expm(long long a, int b) { long long ret = 1; while (b) { if (b & 1) ret = (ret * a) % mod; b = b >> 1; a = (a * a) % mod; } return ret; } long long madd(long long a, long long b) { return (a % mod + b % mod) % mod; } long long mdif(long long a, long long b) { return madd(a, mod - b); } void solve() { int n, q; cin >> n >> q; string s; cin >> s; int p[n + 1]; p[0] = 0; for (int i = 1; i <= n; i++) { p[i] = p[i - 1] + (s[i - 1] == '0'); } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; int x = p[r] - p[l - 1]; cout << mdif(bin_expm(2, r - l + 1), bin_expm(2, x)) << "\n"; } } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int t = 1; while (t--) { solve(); } }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> const int mod = 1e9 + 7; using namespace std; long long bin_expm(long long a, int b) { long long ret = 1; while (b) { if (b & 1) ret = (ret * a) % mod; b = b >> 1; a = (a * a) % mod; } return ret; } long long madd(long long a, long long b) { return (a % mod + b % mod) % mod; } long long mdif(long long a, long long b) { return madd(a, mod - b); } void solve() { int n, q; cin >> n >> q; string s; cin >> s; int p[n + 1]; p[0] = 0; for (int i = 1; i <= n; i++) { p[i] = p[i - 1] + (s[i - 1] == '0'); } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; int x = p[r] - p[l - 1]; cout << mdif(bin_expm(2, r - l + 1), bin_expm(2, x)) << "\n"; } } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int t = 1; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const long long maxn = 100005; const long long mod = 1e9 + 7; long long sum[maxn << 2]; void build(long long rt, long long l, long long r) { if (l == r) { scanf("%1lld", &sum[rt]); } else { long long mid = (l + r) / 2; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } } long long query(long long rt, long long l, long long r, long long L, long long R) { if (L <= l && r <= R) { return sum[rt]; } long long mid = (l + r) / 2; long long ans = 0; if (L <= mid) ans += query(rt << 1, l, mid, L, R); if (mid < R) ans += query(rt << 1 | 1, mid + 1, r, L, R); return ans; } long long qpow(long long a, long long x) { long long ret = 1; while (x) { if (x & 1) ret = ret * a % mod; a = a * a % mod; x >>= 1; } return ret; } int main() { long long n, q; scanf("%lld%lld", &n, &q); build(1, 1, n); while (q--) { long long l, r; scanf("%lld%lld", &l, &r); long long x = query(1, 1, n, l, r); long long y = (r - l + 1) - x; long long ans = (qpow(2, x) - 1 + mod) % mod * qpow(2, y) % mod; printf("%lld\n", ans); } return 0; }
### Prompt Create a solution in Cpp for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long maxn = 100005; const long long mod = 1e9 + 7; long long sum[maxn << 2]; void build(long long rt, long long l, long long r) { if (l == r) { scanf("%1lld", &sum[rt]); } else { long long mid = (l + r) / 2; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } } long long query(long long rt, long long l, long long r, long long L, long long R) { if (L <= l && r <= R) { return sum[rt]; } long long mid = (l + r) / 2; long long ans = 0; if (L <= mid) ans += query(rt << 1, l, mid, L, R); if (mid < R) ans += query(rt << 1 | 1, mid + 1, r, L, R); return ans; } long long qpow(long long a, long long x) { long long ret = 1; while (x) { if (x & 1) ret = ret * a % mod; a = a * a % mod; x >>= 1; } return ret; } int main() { long long n, q; scanf("%lld%lld", &n, &q); build(1, 1, n); while (q--) { long long l, r; scanf("%lld%lld", &l, &r); long long x = query(1, 1, n, l, r); long long y = (r - l + 1) - x; long long ans = (qpow(2, x) - 1 + mod) % mod * qpow(2, y) % mod; printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int no = 3e6 + 5, modulo = 1e9 + 7, inf = 1e18, N = 3e3 + 1; long long int ar[no], br[no]; long long int used[no]; long long int mul(long long int x, long long int y, long long int mod) { return ((x % mod) * (y % mod)) % mod; } long long int powwmod(long long int x, long long int y, long long int mod) { long long int res = 1; while (y) { if (y & 1) { y--; res = mul(res, x, mod); res %= mod; } else { y /= 2; x = mul(x, x, mod); x %= mod; } } return res % mod; } long long int calc(long long int c1, long long int c0) { long long int x = powwmod(2, c1, modulo); x -= 1; x = (x + mul(x, (powwmod(2, c0, modulo) - 1), modulo)) % modulo; return x; } void solve() { long long int n = 0, m = 0, a = 0, b = 0, c = 0, d = 0, x = 0, y = 0, z = 0, w = 0, k = 0; cin >> n; long long int q; cin >> q; string s; cin >> s; for (long long int i = 0; i < n; i++) { ar[i + 1] = ar[i] + s[i] - '0'; } while (q--) { cin >> x >> y; z = calc(ar[y] - ar[x - 1], y - x + 1 - ar[y] + ar[x - 1]); cout << z << "\n"; } } inline void runn() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; for (long long int i = 1; i < t + 1; i++) { solve(); } }
### Prompt Create a solution in Cpp for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int no = 3e6 + 5, modulo = 1e9 + 7, inf = 1e18, N = 3e3 + 1; long long int ar[no], br[no]; long long int used[no]; long long int mul(long long int x, long long int y, long long int mod) { return ((x % mod) * (y % mod)) % mod; } long long int powwmod(long long int x, long long int y, long long int mod) { long long int res = 1; while (y) { if (y & 1) { y--; res = mul(res, x, mod); res %= mod; } else { y /= 2; x = mul(x, x, mod); x %= mod; } } return res % mod; } long long int calc(long long int c1, long long int c0) { long long int x = powwmod(2, c1, modulo); x -= 1; x = (x + mul(x, (powwmod(2, c0, modulo) - 1), modulo)) % modulo; return x; } void solve() { long long int n = 0, m = 0, a = 0, b = 0, c = 0, d = 0, x = 0, y = 0, z = 0, w = 0, k = 0; cin >> n; long long int q; cin >> q; string s; cin >> s; for (long long int i = 0; i < n; i++) { ar[i + 1] = ar[i] + s[i] - '0'; } while (q--) { cin >> x >> y; z = calc(ar[y] - ar[x - 1], y - x + 1 - ar[y] + ar[x - 1]); cout << z << "\n"; } } inline void runn() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; for (long long int i = 1; i < t + 1; i++) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, q, i; scanf("%d %d", &n, &q); char x[100005]; long long poow[100005]; poow[0] = 1; for (i = 1; i <= 100004; i++) { poow[i] = (2 * poow[i - 1]) % 1000000007; } scanf("%s", x); int arr[100005]; arr[0] = x[0] - '0'; for (i = 1; i < n; i++) { arr[i] = x[i] - '0' + arr[i - 1]; } for (int k = 1; k <= q; k++) { int u, v; scanf("%d %d", &u, &v); u--; v--; int c = arr[v] - arr[u - 1]; long long ans = poow[c] - 1; long long zero = v - u + 1 - c; long long ans1 = poow[zero] - 1; ans1 = (ans * ans1) % 1000000007; ans = (ans + ans1) % 1000000007; cout << ans << endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, q, i; scanf("%d %d", &n, &q); char x[100005]; long long poow[100005]; poow[0] = 1; for (i = 1; i <= 100004; i++) { poow[i] = (2 * poow[i - 1]) % 1000000007; } scanf("%s", x); int arr[100005]; arr[0] = x[0] - '0'; for (i = 1; i < n; i++) { arr[i] = x[i] - '0' + arr[i - 1]; } for (int k = 1; k <= q; k++) { int u, v; scanf("%d %d", &u, &v); u--; v--; int c = arr[v] - arr[u - 1]; long long ans = poow[c] - 1; long long zero = v - u + 1 - c; long long ans1 = poow[zero] - 1; ans1 = (ans * ans1) % 1000000007; ans = (ans + ans1) % 1000000007; cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, q; string s; int sz[100010], su[100010]; long long int sum[100010]; int main() { scanf("%d %d", &n, &q); cin >> s; for (int i = int(0); i < int(n); i++) { sz[i] = (s[i] == '0'); su[i] = (s[i] == '1'); if (i) sz[i] += sz[i - 1]; if (i) su[i] += su[i - 1]; } long long int p = 1; for (int i = int(1); i < int(n + 1); i++) { sum[i] = (p + sum[i - 1]) % mod; p = (p * 2) % mod; } int a, b; while (q--) { scanf("%d %d", &a, &b); a--, b--; int q1 = su[b]; if (a) q1 -= su[a - 1]; int q0 = sz[b]; if (a) q0 -= sz[a - 1]; long long int x = sum[q1]; long long int ans = (x + x * sum[q0]) % mod; printf("%lld\n", ans); } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, q; string s; int sz[100010], su[100010]; long long int sum[100010]; int main() { scanf("%d %d", &n, &q); cin >> s; for (int i = int(0); i < int(n); i++) { sz[i] = (s[i] == '0'); su[i] = (s[i] == '1'); if (i) sz[i] += sz[i - 1]; if (i) su[i] += su[i - 1]; } long long int p = 1; for (int i = int(1); i < int(n + 1); i++) { sum[i] = (p + sum[i - 1]) % mod; p = (p * 2) % mod; } int a, b; while (q--) { scanf("%d %d", &a, &b); a--, b--; int q1 = su[b]; if (a) q1 -= su[a - 1]; int q0 = sz[b]; if (a) q0 -= sz[a - 1]; long long int x = sum[q1]; long long int ans = (x + x * sum[q0]) % mod; printf("%lld\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int modular_exp(long long int A, long long int B, long long int C) { if (B == 0) return 1; if (B == 1) return A; long long int res = A; if (res > C) res = res % C; int counter = 2; while (counter < B) { res = res * res; if (res > C) res = res % C; counter *= 2; if (counter >= B) break; } counter /= 2; return ((res % C) * modular_exp(A, B - counter, C)) % C; } long long int Mod(long long int A, long long int B, long long int C) { if (A == 0) return 0; if (C == 1) return 0; long long int res = modular_exp(A, B, C); if (res < 0) return C + res; if (B == 0) return 1; return res; } long long int ans__(long long int num_one, long long int num_zero) { long long int ans = ((Mod(2, num_one, 1000000007) - 1) + 1000000007) % 1000000007; long long int ans_2 = ((Mod(2, num_zero, 1000000007) - 1) + 1000000007) % 1000000007; ans_2 = (ans % 1000000007 * ans_2 % 1000000007) % 1000000007; return (ans % 1000000007 + ans_2 % 1000000007) % 1000000007; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; for (int u = 0; u < t; u++) { long long int n, q; cin >> n >> q; string s; cin >> s; vector<long long int> ones, zeros; long long int num_ones = 0; long long int num_zeros = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') num_zeros++; if (s[i] == '1') num_ones++; ones.push_back(num_ones); zeros.push_back(num_zeros); } for (int i = 0; i < q; i++) { long long int l, r; cin >> l >> r; l--; r--; long long int one, zero; if (l > 0) { one = ones[r] - ones[l - 1]; zero = zeros[r] - zeros[l - 1]; } else if (l == 0) { one = ones[r]; zero = zeros[r]; } cout << ans__(one, zero) << "\n"; } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int modular_exp(long long int A, long long int B, long long int C) { if (B == 0) return 1; if (B == 1) return A; long long int res = A; if (res > C) res = res % C; int counter = 2; while (counter < B) { res = res * res; if (res > C) res = res % C; counter *= 2; if (counter >= B) break; } counter /= 2; return ((res % C) * modular_exp(A, B - counter, C)) % C; } long long int Mod(long long int A, long long int B, long long int C) { if (A == 0) return 0; if (C == 1) return 0; long long int res = modular_exp(A, B, C); if (res < 0) return C + res; if (B == 0) return 1; return res; } long long int ans__(long long int num_one, long long int num_zero) { long long int ans = ((Mod(2, num_one, 1000000007) - 1) + 1000000007) % 1000000007; long long int ans_2 = ((Mod(2, num_zero, 1000000007) - 1) + 1000000007) % 1000000007; ans_2 = (ans % 1000000007 * ans_2 % 1000000007) % 1000000007; return (ans % 1000000007 + ans_2 % 1000000007) % 1000000007; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; for (int u = 0; u < t; u++) { long long int n, q; cin >> n >> q; string s; cin >> s; vector<long long int> ones, zeros; long long int num_ones = 0; long long int num_zeros = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') num_zeros++; if (s[i] == '1') num_ones++; ones.push_back(num_ones); zeros.push_back(num_zeros); } for (int i = 0; i < q; i++) { long long int l, r; cin >> l >> r; l--; r--; long long int one, zero; if (l > 0) { one = ones[r] - ones[l - 1]; zero = zeros[r] - zeros[l - 1]; } else if (l == 0) { one = ones[r]; zero = zeros[r]; } cout << ans__(one, zero) << "\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; constexpr int MAXN = 5 + 100000; constexpr int MOD = 7 + 1000000000; int oac[MAXN]; int add(int a, int b) { int ans = a + b; if (ans >= MOD) ans -= MOD; return ans; } int sub(int a, int b) { int ans = a - b; if (ans < 0) ans += MOD; return ans; } int mul(int a, int b) { return (int)((1LL * a * b) % MOD); } int fpow(int b, int e) { int ans = 1, p = b; for (; e; e >>= 1) { if (e & 1) ans = mul(ans, p); p = mul(p, p); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string s; int n, q; cin >> n >> q >> s; for (int i = (int)0; i < (int)n; ++i) { oac[i] = (s[i] == '1'); if (i >= 1) oac[i] += oac[i - 1]; } while (q--) { int first, second; cin >> first >> second; --first; --second; int o = oac[second]; if (first >= 1) o -= oac[first - 1]; if (o == 0) { cout << 0 << '\n'; } else { int sum1 = sub(fpow(2, o), 1); int sum2 = mul(sum1, sub(fpow(2, 1 + second - first - o), 1)); cout << add(sum1, sum2) << '\n'; } } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; constexpr int MAXN = 5 + 100000; constexpr int MOD = 7 + 1000000000; int oac[MAXN]; int add(int a, int b) { int ans = a + b; if (ans >= MOD) ans -= MOD; return ans; } int sub(int a, int b) { int ans = a - b; if (ans < 0) ans += MOD; return ans; } int mul(int a, int b) { return (int)((1LL * a * b) % MOD); } int fpow(int b, int e) { int ans = 1, p = b; for (; e; e >>= 1) { if (e & 1) ans = mul(ans, p); p = mul(p, p); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string s; int n, q; cin >> n >> q >> s; for (int i = (int)0; i < (int)n; ++i) { oac[i] = (s[i] == '1'); if (i >= 1) oac[i] += oac[i - 1]; } while (q--) { int first, second; cin >> first >> second; --first; --second; int o = oac[second]; if (first >= 1) o -= oac[first - 1]; if (o == 0) { cout << 0 << '\n'; } else { int sum1 = sub(fpow(2, o), 1); int sum2 = mul(sum1, sub(fpow(2, 1 + second - first - o), 1)); cout << add(sum1, sum2) << '\n'; } } return 0; } ```
#include <bits/stdc++.h> bool debug = 1; const long long MOD = 1000000007; const double PI = acos(-1.0); const double eps = 1e-9; using namespace std; long long pot2[100100]; int s1[100100]; void pre() { pot2[0] = 1; for (int i = 1; i < 100100; i++) { pot2[i] = 2 * pot2[i - 1]; pot2[i] %= MOD; } } long long solve(int a, int b) { int x1, x0; x1 = s1[b] - s1[a - 1]; x0 = (b - a + 1) - x1; long long res = ((pot2[x1] - 1) + MOD) % MOD; res += ((pot2[x1] - 1 + MOD) % MOD) * ((pot2[x0] - 1 + MOD) % MOD); res %= MOD; return res; } int v[100100]; int main() { int n, q; pre(); cin >> n >> q; string s; cin >> s; s.insert(0, "x"); s1[0] = 0; for (int i = 1; i <= n; i++) { s1[i] = s1[i - 1] + (s[i] == '1'); } int a, b; for (int i = 0; i < q; i++) { scanf("%d %d", &a, &b); printf("%lld\n", solve(a, b)); } return 0; }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> bool debug = 1; const long long MOD = 1000000007; const double PI = acos(-1.0); const double eps = 1e-9; using namespace std; long long pot2[100100]; int s1[100100]; void pre() { pot2[0] = 1; for (int i = 1; i < 100100; i++) { pot2[i] = 2 * pot2[i - 1]; pot2[i] %= MOD; } } long long solve(int a, int b) { int x1, x0; x1 = s1[b] - s1[a - 1]; x0 = (b - a + 1) - x1; long long res = ((pot2[x1] - 1) + MOD) % MOD; res += ((pot2[x1] - 1 + MOD) % MOD) * ((pot2[x0] - 1 + MOD) % MOD); res %= MOD; return res; } int v[100100]; int main() { int n, q; pre(); cin >> n >> q; string s; cin >> s; s.insert(0, "x"); s1[0] = 0; for (int i = 1; i <= n; i++) { s1[i] = s1[i - 1] + (s[i] == '1'); } int a, b; for (int i = 0; i < q; i++) { scanf("%d %d", &a, &b); printf("%lld\n", solve(a, b)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const bool debug = false; const int maxn = 1e5 + 7; const int inf = 1e9 + 7; const long long mod = 1e9 + 7; int pre[maxn]; long long bp(long long a, long long p) { if (p == 0) return 1; if (p % 2) { return (bp(a, p - 1) * a) % mod; } else { long long re = bp(a, p / 2); return (re * re) % mod; } } int main() { int n, q; cin >> n >> q; string s; cin >> s; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1]; if (s[i - 1] == '1') { pre[i]++; } } while (q--) { long long l, r; cin >> l >> r; long long a = pre[r] - pre[l - 1], b = (r - l + 1) - a; cout << (bp(2, a) - 1 + ((bp(2, b) - 1) * (bp(2, a) - 1)) % mod + mod) % mod << "\n"; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const bool debug = false; const int maxn = 1e5 + 7; const int inf = 1e9 + 7; const long long mod = 1e9 + 7; int pre[maxn]; long long bp(long long a, long long p) { if (p == 0) return 1; if (p % 2) { return (bp(a, p - 1) * a) % mod; } else { long long re = bp(a, p / 2); return (re * re) % mod; } } int main() { int n, q; cin >> n >> q; string s; cin >> s; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1]; if (s[i - 1] == '1') { pre[i]++; } } while (q--) { long long l, r; cin >> l >> r; long long a = pre[r] - pre[l - 1], b = (r - l + 1) - a; cout << (bp(2, a) - 1 + ((bp(2, b) - 1) * (bp(2, a) - 1)) % mod + mod) % mod << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int gcd(int f, int s) { if (s == 0) return f; else return gcd(s, f % s); } int const N = 1007006; long long const M = 1000 * 1000 * 1000 + 7; long double const ep = .000000000000000001; int arr[N]; long long pw[N]; int cul0[N], cul1[N]; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { char c; cin >> c; cul0[i] = cul0[i - 1]; cul1[i] = cul1[i - 1]; if (c == '0') arr[i] = 0, cul0[i]++; else arr[i] = 1, cul1[i]++; } pw[0] = 1; for (int i = 1; i < 1000000; i++) pw[i] = (pw[i - 1] * 2) % M; while (q--) { int l, r; scanf("%d%d", &l, &r); l--; int ones = cul1[r] - cul1[l]; int zeros = cul0[r] - cul0[l]; long long sum = pw[ones] - 1; sum = (sum + (sum * (pw[zeros] - 1)) % M) % M; printf("%lld\n", sum); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gcd(int f, int s) { if (s == 0) return f; else return gcd(s, f % s); } int const N = 1007006; long long const M = 1000 * 1000 * 1000 + 7; long double const ep = .000000000000000001; int arr[N]; long long pw[N]; int cul0[N], cul1[N]; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { char c; cin >> c; cul0[i] = cul0[i - 1]; cul1[i] = cul1[i - 1]; if (c == '0') arr[i] = 0, cul0[i]++; else arr[i] = 1, cul1[i]++; } pw[0] = 1; for (int i = 1; i < 1000000; i++) pw[i] = (pw[i - 1] * 2) % M; while (q--) { int l, r; scanf("%d%d", &l, &r); l--; int ones = cul1[r] - cul1[l]; int zeros = cul0[r] - cul0[l]; long long sum = pw[ones] - 1; sum = (sum + (sum * (pw[zeros] - 1)) % M) % M; printf("%lld\n", sum); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long add(long long x, long long y) { x += y; while (x >= 1000000007) x -= 1000000007; while (x < 0) x += 1000000007; return x; } long long mul(long long x, long long y) { return (x * 1ll * y) % 1000000007; } long long binpow(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2 == 1) z = mul(z, x); x = mul(x, x); y /= 2; } return z; } long long inv(long long x) { return binpow(x, 1000000007 - 2); } long long divide(long long x, long long y) { return mul(x, inv(y)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, q; cin >> n >> q; string str; cin >> str; vector<long long> ar(str.length() + 1, 0); for (long long i = 1; i < (long long)str.length() + 1; ++i) ar[i] = str[i - 1] - '0'; for (long long i = 1; i < (long long)ar.size(); ++i) ar[i] += ar[i - 1]; while (q--) { long long l, r; cin >> l >> r; long long c1 = ar[r] - ar[l - 1]; long long c2 = r - l - c1 + 1; long long alpha = binpow(2, c1); alpha = add(alpha, -1); long long beta = binpow(2, c2); beta = add(beta, -1); beta = mul(beta, alpha); cout << add(alpha, beta) << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long add(long long x, long long y) { x += y; while (x >= 1000000007) x -= 1000000007; while (x < 0) x += 1000000007; return x; } long long mul(long long x, long long y) { return (x * 1ll * y) % 1000000007; } long long binpow(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2 == 1) z = mul(z, x); x = mul(x, x); y /= 2; } return z; } long long inv(long long x) { return binpow(x, 1000000007 - 2); } long long divide(long long x, long long y) { return mul(x, inv(y)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, q; cin >> n >> q; string str; cin >> str; vector<long long> ar(str.length() + 1, 0); for (long long i = 1; i < (long long)str.length() + 1; ++i) ar[i] = str[i - 1] - '0'; for (long long i = 1; i < (long long)ar.size(); ++i) ar[i] += ar[i - 1]; while (q--) { long long l, r; cin >> l >> r; long long c1 = ar[r] - ar[l - 1]; long long c2 = r - l - c1 + 1; long long alpha = binpow(2, c1); alpha = add(alpha, -1); long long beta = binpow(2, c2); beta = add(beta, -1); beta = mul(beta, alpha); cout << add(alpha, beta) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; pair<int, int> pref[100005]; long long zeroes[1000005], ones[1000005]; int main() { int n, q; cin >> n >> q; ones[0] = 1; zeroes[0] = 1; for (int i = 1; i <= n; ++i) { ones[i] = ones[i - 1] * 2; ones[i] %= 1000000007; zeroes[i] = zeroes[i - 1] * 2; zeroes[i] %= 1000000007; } int count_1 = 0, count_0 = 0; string str; cin >> str; pref[0].first = 0; pref[0].second = 0; for (int i = 0; i < n; ++i) { if (str[i] == '0') ++count_0; else ++count_1; pref[i + 1].first = count_1; pref[i + 1].second = count_0; } while (q--) { int a, b; cin >> a >> b; int z = pref[b].second - pref[a - 1].second, o = pref[b].first - pref[a - 1].first; unsigned long long ans = zeroes[z] * (ones[o] - 1); ans %= 1000000007; cout << ans << '\n'; } }
### Prompt Generate a CPP solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<int, int> pref[100005]; long long zeroes[1000005], ones[1000005]; int main() { int n, q; cin >> n >> q; ones[0] = 1; zeroes[0] = 1; for (int i = 1; i <= n; ++i) { ones[i] = ones[i - 1] * 2; ones[i] %= 1000000007; zeroes[i] = zeroes[i - 1] * 2; zeroes[i] %= 1000000007; } int count_1 = 0, count_0 = 0; string str; cin >> str; pref[0].first = 0; pref[0].second = 0; for (int i = 0; i < n; ++i) { if (str[i] == '0') ++count_0; else ++count_1; pref[i + 1].first = count_1; pref[i + 1].second = count_0; } while (q--) { int a, b; cin >> a >> b; int z = pref[b].second - pref[a - 1].second, o = pref[b].first - pref[a - 1].first; unsigned long long ans = zeroes[z] * (ones[o] - 1); ans %= 1000000007; cout << ans << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int PS[100001]; long long fast_pow(long long a, long long b) { long long ret = 1; for (; b; b >>= 1) { if (b & 1) ret = (ret * a) % MOD; a = (a * a) % MOD; } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ((void)0); ((void)0); ((void)0); int N, M; string s; cin >> N >> M >> s; for (int i = 0; i < N; i++) PS[i + 1] = (s[i] == '0') + PS[i]; for (; M; M--) { int l, r; cin >> l >> r; l--; cout << (MOD + fast_pow(2, r - l) - fast_pow(2, PS[r] - PS[l])) % MOD << '\n'; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int PS[100001]; long long fast_pow(long long a, long long b) { long long ret = 1; for (; b; b >>= 1) { if (b & 1) ret = (ret * a) % MOD; a = (a * a) % MOD; } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ((void)0); ((void)0); ((void)0); int N, M; string s; cin >> N >> M >> s; for (int i = 0; i < N; i++) PS[i + 1] = (s[i] == '0') + PS[i]; for (; M; M--) { int l, r; cin >> l >> r; l--; cout << (MOD + fast_pow(2, r - l) - fast_pow(2, PS[r] - PS[l])) % MOD << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { long long int r, i; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long int power(long long int x, long long int y, long long int mod) { long long int temp, ty, my; if (y == 0) return 1; temp = power(x, y / 2, mod); ty = (temp % mod) * (temp % mod); if (y % 2 == 0) { return ty % mod; } else { my = (x % mod) * (ty % mod); return my % mod; } } long long int mycbrt(long long int n) { long long int start = 0; long long int end = 1000000LL; long long int ans = -1; while (start <= end) { long long int mid = (start + end) / 2; if ((mid * mid * mid) > n) end = mid - 1; else if ((mid * mid * mid) == n) { ans = mid; break; } else { start = mid + 1; } } if (n == 1) { ans = 1; } return ans; } void SieveOfEratosthenes(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * 2; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) if (prime[p]) cout << p << " "; } struct abhi { long long int val1; long long int val2; long long int po; }; bool cmp(struct abhi x, struct abhi y) { if (x.val1 == y.val1) return x.val2 < y.val2; return x.val1 < y.val1; } void fastscan(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c == '-') { negative = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48; if (negative) number *= -1; } vector<pair<long long int, long long int> > po; long long int dp[2000010]; long long int dp2[200010]; long long int ar[2000010]; vector<long long int> vec; vector<pair<long long int, long long int> > vec2; long long int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, i, j, k, q; long long int t; cin >> n >> t; string str; cin >> str; dp[0] = 0; for (i = (0); i < (n); i++) { dp[i + 1] = dp[i] + ((str[i] == '1') ? 1 : 0); } while (t--) { long long int a, b; cin >> a >> b; long long int ones = dp[b] - dp[a - 1]; long long int zeroes = (b - a + 1) - ones; long long int pot = (power(2, ones, mod) - 1 + mod) % mod; long long int ans = pot; long long int pot2 = (power(2, zeroes, mod) - 1 + mod) % mod; long long int ko = (pot * pot2) % mod; ans = (ans + ko) % mod; cout << ans << "\n"; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { long long int r, i; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long int power(long long int x, long long int y, long long int mod) { long long int temp, ty, my; if (y == 0) return 1; temp = power(x, y / 2, mod); ty = (temp % mod) * (temp % mod); if (y % 2 == 0) { return ty % mod; } else { my = (x % mod) * (ty % mod); return my % mod; } } long long int mycbrt(long long int n) { long long int start = 0; long long int end = 1000000LL; long long int ans = -1; while (start <= end) { long long int mid = (start + end) / 2; if ((mid * mid * mid) > n) end = mid - 1; else if ((mid * mid * mid) == n) { ans = mid; break; } else { start = mid + 1; } } if (n == 1) { ans = 1; } return ans; } void SieveOfEratosthenes(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * 2; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) if (prime[p]) cout << p << " "; } struct abhi { long long int val1; long long int val2; long long int po; }; bool cmp(struct abhi x, struct abhi y) { if (x.val1 == y.val1) return x.val2 < y.val2; return x.val1 < y.val1; } void fastscan(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c == '-') { negative = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48; if (negative) number *= -1; } vector<pair<long long int, long long int> > po; long long int dp[2000010]; long long int dp2[200010]; long long int ar[2000010]; vector<long long int> vec; vector<pair<long long int, long long int> > vec2; long long int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, i, j, k, q; long long int t; cin >> n >> t; string str; cin >> str; dp[0] = 0; for (i = (0); i < (n); i++) { dp[i + 1] = dp[i] + ((str[i] == '1') ? 1 : 0); } while (t--) { long long int a, b; cin >> a >> b; long long int ones = dp[b] - dp[a - 1]; long long int zeroes = (b - a + 1) - ones; long long int pot = (power(2, ones, mod) - 1 + mod) % mod; long long int ans = pot; long long int pot2 = (power(2, zeroes, mod) - 1 + mod) % mod; long long int ko = (pot * pot2) % mod; ans = (ans + ko) % mod; cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, q, i, l, r, MOD = 1e9 + 7, t[100005 * 4], sum[100005]; int solve(int k, int len) { return (sum[len] - sum[len - k] + MOD) % MOD; } void build(int l, int r, int node) { if (l == r) { t[node] = getchar() - 48; return; } int mid = (l + r) >> 1; build(l, mid, node << 1), build(mid + 1, r, node << 1 | 1); t[node] = t[node << 1] + t[node << 1 | 1]; } int ask(int l, int r, int a, int b, int node) { if (l == a && r == b) return t[node]; int mid = (l + r) >> 1; if (b <= mid) return ask(l, mid, a, b, node << 1); if (a > mid) return ask(mid + 1, r, a, b, node << 1 | 1); return ask(l, mid, a, mid, node << 1) + ask(mid + 1, r, mid + 1, b, node << 1 | 1); } int main() { scanf("%d%d\n", &n, &q); build(1, n, 1); for (i = 1, sum[0] = 1; i <= n; i++) sum[i] = sum[i - 1] * 2 % MOD; for (i = 1; i <= q; i++) { scanf("%d%d", &l, &r); printf("%d\n", solve(ask(1, n, l, r, 1), r - l + 1)); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, q, i, l, r, MOD = 1e9 + 7, t[100005 * 4], sum[100005]; int solve(int k, int len) { return (sum[len] - sum[len - k] + MOD) % MOD; } void build(int l, int r, int node) { if (l == r) { t[node] = getchar() - 48; return; } int mid = (l + r) >> 1; build(l, mid, node << 1), build(mid + 1, r, node << 1 | 1); t[node] = t[node << 1] + t[node << 1 | 1]; } int ask(int l, int r, int a, int b, int node) { if (l == a && r == b) return t[node]; int mid = (l + r) >> 1; if (b <= mid) return ask(l, mid, a, b, node << 1); if (a > mid) return ask(mid + 1, r, a, b, node << 1 | 1); return ask(l, mid, a, mid, node << 1) + ask(mid + 1, r, mid + 1, b, node << 1 | 1); } int main() { scanf("%d%d\n", &n, &q); build(1, n, 1); for (i = 1, sum[0] = 1; i <= n; i++) sum[i] = sum[i - 1] * 2 % MOD; for (i = 1; i <= q; i++) { scanf("%d%d", &l, &r); printf("%d\n", solve(ask(1, n, l, r, 1), r - l + 1)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, q; cin >> n >> q; string s; cin >> s; int ao[n + 3]; memset(ao, 0, sizeof ao); for (int i = 0; i < (int)s.size(); i++) { ao[i] += (s[i] == '1'); if (i) ao[i] += ao[i - 1]; } long long sp[100001], asp[100001]; sp[0] = 1; asp[0] = 1; for (int i = 1; i < 100001; i++) sp[i] = sp[i - 1] * 2, sp[i] %= 1000000007; for (int i = 1; i < 100001; i++) asp[i] = sp[i], asp[i] += asp[i - 1], asp[i] %= 1000000007; while (q--) { int l, r; long long ans = 0; cin >> l >> r; l--; r--; int ones = (!l ? ao[r] : ao[r] - ao[l - 1]); ans += (ones ? asp[ones - 1] : 0); long long fac, z = r - l + 1 - ones, u; if (ones) { fac = sp[ones - 1]; fac *= 2; fac %= 1000000007; fac--; u = (z ? asp[z - 1] : 0); u %= 1000000007; u *= fac; u %= 1000000007; cout << (ans % 1000000007 + u) % 1000000007 << endl; } else cout << 0 << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, q; cin >> n >> q; string s; cin >> s; int ao[n + 3]; memset(ao, 0, sizeof ao); for (int i = 0; i < (int)s.size(); i++) { ao[i] += (s[i] == '1'); if (i) ao[i] += ao[i - 1]; } long long sp[100001], asp[100001]; sp[0] = 1; asp[0] = 1; for (int i = 1; i < 100001; i++) sp[i] = sp[i - 1] * 2, sp[i] %= 1000000007; for (int i = 1; i < 100001; i++) asp[i] = sp[i], asp[i] += asp[i - 1], asp[i] %= 1000000007; while (q--) { int l, r; long long ans = 0; cin >> l >> r; l--; r--; int ones = (!l ? ao[r] : ao[r] - ao[l - 1]); ans += (ones ? asp[ones - 1] : 0); long long fac, z = r - l + 1 - ones, u; if (ones) { fac = sp[ones - 1]; fac *= 2; fac %= 1000000007; fac--; u = (z ? asp[z - 1] : 0); u %= 1000000007; u *= fac; u %= 1000000007; cout << (ans % 1000000007 + u) % 1000000007 << endl; } else cout << 0 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); cerr << "Completed in " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds\n"; } const long long INF = 1e9; const long long N = 1e5 + 5; const long long MOD = 1e9 + 7; long long n, q, l, r, f[N]; string s; long long modpow(long long a, long long b, long long mod) { long long res = 1; while (b) { if (b & 1) (res *= a) %= mod; (a *= a) %= mod; b >>= 1; } return res; } void solve() { cin >> n >> q; cin >> s; for (long long i = 1; i <= n; i++) { f[i] = f[i - 1] + (s[i - 1] == '1'); } for (long long i = 1; i <= q; i++) { cin >> l >> r; long long x = r - l + 1; long long y = x - (f[r] - f[l - 1]); long long ans = modpow(2, x, MOD) - 1; ans -= modpow(2, y, MOD) - 1; ans %= MOD; if (ans < 0) ans += MOD; cout << ans << "\n"; } }
### Prompt Create a solution in CPP for the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); cerr << "Completed in " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds\n"; } const long long INF = 1e9; const long long N = 1e5 + 5; const long long MOD = 1e9 + 7; long long n, q, l, r, f[N]; string s; long long modpow(long long a, long long b, long long mod) { long long res = 1; while (b) { if (b & 1) (res *= a) %= mod; (a *= a) %= mod; b >>= 1; } return res; } void solve() { cin >> n >> q; cin >> s; for (long long i = 1; i <= n; i++) { f[i] = f[i - 1] + (s[i - 1] == '1'); } for (long long i = 1; i <= q; i++) { cin >> l >> r; long long x = r - l + 1; long long y = x - (f[r] - f[l - 1]); long long ans = modpow(2, x, MOD) - 1; ans -= modpow(2, y, MOD) - 1; ans %= MOD; if (ans < 0) ans += MOD; cout << ans << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 100; const int MOD = 1e9 + 7; int Pow(int a, int k, int p) { int ans = 1; while (k) { if (k & 1) ans = 1ll * ans * a % p; k >>= 1; a = 1ll * a * a % p; } return ans; } int pre1[maxn]; int pre0[maxn]; int main() { int n, q; cin >> n >> q; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { pre1[i + 1] = pre1[i] + 1; pre0[i + 1] = pre0[i]; } else { pre0[i + 1] = pre0[i] + 1; pre1[i + 1] = pre1[i]; } } while (q--) { int l, r; scanf("%d %d", &l, &r); int cnt1 = pre1[r] - pre1[l - 1]; int cnt0 = pre0[r] - pre0[l - 1]; cout << 1ll * (Pow(2, cnt1, MOD) - 1) * (Pow(2, cnt0, MOD)) % MOD << endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 100; const int MOD = 1e9 + 7; int Pow(int a, int k, int p) { int ans = 1; while (k) { if (k & 1) ans = 1ll * ans * a % p; k >>= 1; a = 1ll * a * a % p; } return ans; } int pre1[maxn]; int pre0[maxn]; int main() { int n, q; cin >> n >> q; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { pre1[i + 1] = pre1[i] + 1; pre0[i + 1] = pre0[i]; } else { pre0[i + 1] = pre0[i] + 1; pre1[i + 1] = pre1[i]; } } while (q--) { int l, r; scanf("%d %d", &l, &r); int cnt1 = pre1[r] - pre1[l - 1]; int cnt0 = pre0[r] - pre0[l - 1]; cout << 1ll * (Pow(2, cnt1, MOD) - 1) * (Pow(2, cnt0, MOD)) % MOD << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long int K = 1e9 + 7; long long int mu[100005]; int a[100005]; int s[100005]; int main() { ios::sync_with_stdio(0); cin.tie(NULL); mu[0] = 1; for (int i = 1; i <= 100000; i++) { mu[i] = mu[i - 1] * 2ll % K; } int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { char c; cin >> c; if (c == '1') a[i] = 1; else a[i] = 0; s[i] = s[i - 1] + a[i]; } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; int a = s[r] - s[l - 1]; int b = r - l + 1 - a; long long int res = mu[b] * (mu[a] + K - 1) % K; cout << res << endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int K = 1e9 + 7; long long int mu[100005]; int a[100005]; int s[100005]; int main() { ios::sync_with_stdio(0); cin.tie(NULL); mu[0] = 1; for (int i = 1; i <= 100000; i++) { mu[i] = mu[i - 1] * 2ll % K; } int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { char c; cin >> c; if (c == '1') a[i] = 1; else a[i] = 0; s[i] = s[i - 1] + a[i]; } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; int a = s[r] - s[l - 1]; int b = r - l + 1 - a; long long int res = mu[b] * (mu[a] + K - 1) % K; cout << res << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; const long long mod = 1e9 + 7; long long m[maxn], pre[maxn]; char s[maxn]; int sum[maxn]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", s + 1); m[0] = 1; long long base = 1; for (int i = 1; i <= n; i++) { base *= 2; base %= mod; m[i] = (m[i - 1] + base) % mod; } pre[0] = 1; for (int i = 1; i <= n; i++) pre[i] = (pre[i - 1] + m[i]) % mod; for (int i = 1; i <= n; i++) { if (s[i] == '1') sum[i] = sum[i - 1] + 1; else sum[i] = sum[i - 1]; } for (int i = 1; i <= q; i++) { int l, r; scanf("%d%d", &l, &r); int high = r - l - 1; int low = r - l - sum[r] + sum[l - 1]; if (l == r) { if (s[l] == '1') printf("1\n"); else printf("0\n"); continue; } if (sum[r] - sum[l - 1] == 0) printf("0\n"); else { int num = sum[r] - sum[l - 1]; if (low <= 0) printf("%lld\n", (pre[high] + num) % mod); else printf("%lld\n", ((pre[high] - pre[low - 1] + mod) % mod + num) % mod); } } return 0; }
### Prompt In CPP, your task is to solve the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; const long long mod = 1e9 + 7; long long m[maxn], pre[maxn]; char s[maxn]; int sum[maxn]; int main() { int n, q; scanf("%d%d", &n, &q); scanf("%s", s + 1); m[0] = 1; long long base = 1; for (int i = 1; i <= n; i++) { base *= 2; base %= mod; m[i] = (m[i - 1] + base) % mod; } pre[0] = 1; for (int i = 1; i <= n; i++) pre[i] = (pre[i - 1] + m[i]) % mod; for (int i = 1; i <= n; i++) { if (s[i] == '1') sum[i] = sum[i - 1] + 1; else sum[i] = sum[i - 1]; } for (int i = 1; i <= q; i++) { int l, r; scanf("%d%d", &l, &r); int high = r - l - 1; int low = r - l - sum[r] + sum[l - 1]; if (l == r) { if (s[l] == '1') printf("1\n"); else printf("0\n"); continue; } if (sum[r] - sum[l - 1] == 0) printf("0\n"); else { int num = sum[r] - sum[l - 1]; if (low <= 0) printf("%lld\n", (pre[high] + num) % mod); else printf("%lld\n", ((pre[high] - pre[low - 1] + mod) % mod + num) % mod); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int dx4[] = {0, 0, -1, 1}; int dy4[] = {-1, 1, 0, 0}; int dx8[] = {0, 0, -1, 1, -1, -1, 1, 1}; int dy8[] = {-1, 1, 0, 0, -1, 1, -1, 1}; int knightx[] = {-1, 1, -2, 2, -2, 2, -1, 1}; int knighty[] = {-2, -2, -1, -1, 1, 1, 2, 2}; template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { if (ch < '0' || ch > '9') break; n = n * 10 + (ch - '0'); ch = getchar(); } return (ng ? -n : n); } template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return (POW(B, P / 2) * POW(B, P / 2)); } template <typename T> inline T Gcd(T a, T b) { if (a < 0) return Gcd(-a, b); if (b < 0) return Gcd(a, -b); return (b == 0) ? a : Gcd(b, a % b); } template <typename T> inline T Lcm(T a, T b) { if (a < 0) return Lcm(-a, b); if (b < 0) return Lcm(a, -b); return a * (b / Gcd(a, b)); } template <typename T> T Bigmod(T base, T power, T MOD) { T ret = T(1) % MOD; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } template <typename T> long long isLeft(T a, T b, T c) { return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); } template <typename T> T ModInverse(T number, T MOD) { return Bigmod(number, MOD - T(2), MOD); } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } bool isPrime(long long val) { if (val == 2) return true; if (val % 2 == 0 || val == 1) return false; long long sqrt_N = (long long)((double)sqrt(val)); for (long long i = 3; i <= sqrt_N; i += 2) { if (val % i == 0) return false; } return true; } template <class T> string convert(T _input) { stringstream blah; blah << _input; return blah.str(); } bool valid(int r, int c, int x, int y) { if (x >= 1 && x <= r && y >= 1 && y <= c) return 1; return 0; } map<string, long long> month; void Month() { month["January"] = 1, month["February"] = 2, month["March"] = 3, month["April"] = 4, month["May"] = 5, month["June"] = 6; month["July"] = 7, month["August"] = 8, month["September"] = 9, month["October"] = 10, month["November"] = 11, month["December"] = 12; } bool Check(int val, int pos) { return bool(val & (1 << pos)); } int Set(int val, int pos) { return val | (1 << pos); } int Reset(int val, int pos) { return val & (~(1 << pos)); } int Flip(int val, int pos) { return val ^ (1 << pos); } const long long maxn = 1e5 + 5; long long n, m, caseno; vector<long long> tree[4 * maxn]; long long a[maxn]; void init(long long node, long long left, long long right) { if (left == right) { tree[node].push_back(a[left]); return; } long long mid = left + ((right - left) >> 1); long long Lnode = (node << 1); long long Rnode = (node << 1) + 1; init(Lnode, left, mid); init(Rnode, mid + 1, right); merge(tree[Lnode].begin(), tree[Lnode].end(), tree[Rnode].begin(), tree[Rnode].end(), back_inserter(tree[node])); } long long query(long long node, long long left, long long right, long long i, long long j, long long val) { if (left > j || right < i) return 0; if (left >= i && right <= j) { return lower_bound(tree[node].begin(), tree[node].end(), val + 1LL) - tree[node].begin(); } long long mid = left + ((right - left) >> 1); long long Lnode = (node << 1); long long Rnode = (node << 1) + 1; return query(Lnode, left, mid, i, j, val) + query(Rnode, mid + 1, right, i, j, val); } long long Fun(long long L, long long R, long long k) { long long left = -1000000000LL, right = 1000000000LL, mid, res; while (left <= right) { mid = left + ((right - left) >> 1); long long koyta = query(1, 1, n, L, R, mid); if (koyta >= k) { res = mid; right = mid - 1; } else left = mid + 1; } return res; } bool prm[1000007]; long long N = 1000000; void sieve() { prm[0] = 1; for (long long i = 2; i <= N; i++) prm[i] = 0; for (long long i = 3; i * i <= N; i += 2) { if (prm[i >> 1] == 0) { for (long long j = i * i; j <= N; j += (i << 1)) { prm[j >> 1] = 1; } } } } long long Factor[100005], Sz; long long cnt[100005], Mx; long long Pre[100005]; long long Pre1[100005]; void PrimeFactorize(long long val) { long long i; if (val % 2 == 0) { Sz++; Factor[Sz] = 2; long long tot = 0; while (val % 2 == 0) { val /= 2; tot++; } cnt[2] = tot; Mx = max(Mx, tot); } for (i = 3; i <= sqrt(val); i += 2) { if (val % i == 0) { Sz++; Factor[Sz] = i; long long tot = 0; while (val % i == 0) { val /= i; tot++; } cnt[i] = tot; Mx = max(Mx, tot); } } if (val > 2) { Sz++; Factor[Sz] = val; cnt[val] = 1; } } char s[100005]; int main() { long long i, j, q; n = in<long long>(), q = in<long long>(); scanf("%s", &s); for (i = n; i >= 1; i--) s[i] = s[i - 1]; for (i = 1; i <= n; i++) { cnt[i] = s[i] - 48; } for (i = 1; i <= n; i++) { cnt[i] += cnt[i - 1]; } long long d = 1; for (i = 1; i <= n; i++) { Pre[i] = (Pre[i - 1] + d) % 1000000007LL; Pre1[i] = (Pre1[i - 1] + d * i) % 1000000007LL; d *= 2; d %= 1000000007LL; } for (i = 1; i <= q; i++) { long long l, r; l = in<long long>(), r = in<long long>(); long long m = r - l + 1; long long d = cnt[r] - cnt[l - 1]; long long d1 = m - d; long long res = 0; if (d) { res = (d + d * (Pre[m - 1])) % 1000000007LL; res -= ((Bigmod(2LL, d1, 1000000007LL) * Pre1[d - 1])) % 1000000007LL; res %= 1000000007LL; res += 1000000007LL; res %= 1000000007LL; } printf("%lld\n", res); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dx4[] = {0, 0, -1, 1}; int dy4[] = {-1, 1, 0, 0}; int dx8[] = {0, 0, -1, 1, -1, -1, 1, 1}; int dy8[] = {-1, 1, 0, 0, -1, 1, -1, 1}; int knightx[] = {-1, 1, -2, 2, -2, 2, -1, 1}; int knighty[] = {-2, -2, -1, -1, 1, 1, 2, 2}; template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { if (ch < '0' || ch > '9') break; n = n * 10 + (ch - '0'); ch = getchar(); } return (ng ? -n : n); } template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return (POW(B, P / 2) * POW(B, P / 2)); } template <typename T> inline T Gcd(T a, T b) { if (a < 0) return Gcd(-a, b); if (b < 0) return Gcd(a, -b); return (b == 0) ? a : Gcd(b, a % b); } template <typename T> inline T Lcm(T a, T b) { if (a < 0) return Lcm(-a, b); if (b < 0) return Lcm(a, -b); return a * (b / Gcd(a, b)); } template <typename T> T Bigmod(T base, T power, T MOD) { T ret = T(1) % MOD; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } template <typename T> long long isLeft(T a, T b, T c) { return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); } template <typename T> T ModInverse(T number, T MOD) { return Bigmod(number, MOD - T(2), MOD); } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } bool isPrime(long long val) { if (val == 2) return true; if (val % 2 == 0 || val == 1) return false; long long sqrt_N = (long long)((double)sqrt(val)); for (long long i = 3; i <= sqrt_N; i += 2) { if (val % i == 0) return false; } return true; } template <class T> string convert(T _input) { stringstream blah; blah << _input; return blah.str(); } bool valid(int r, int c, int x, int y) { if (x >= 1 && x <= r && y >= 1 && y <= c) return 1; return 0; } map<string, long long> month; void Month() { month["January"] = 1, month["February"] = 2, month["March"] = 3, month["April"] = 4, month["May"] = 5, month["June"] = 6; month["July"] = 7, month["August"] = 8, month["September"] = 9, month["October"] = 10, month["November"] = 11, month["December"] = 12; } bool Check(int val, int pos) { return bool(val & (1 << pos)); } int Set(int val, int pos) { return val | (1 << pos); } int Reset(int val, int pos) { return val & (~(1 << pos)); } int Flip(int val, int pos) { return val ^ (1 << pos); } const long long maxn = 1e5 + 5; long long n, m, caseno; vector<long long> tree[4 * maxn]; long long a[maxn]; void init(long long node, long long left, long long right) { if (left == right) { tree[node].push_back(a[left]); return; } long long mid = left + ((right - left) >> 1); long long Lnode = (node << 1); long long Rnode = (node << 1) + 1; init(Lnode, left, mid); init(Rnode, mid + 1, right); merge(tree[Lnode].begin(), tree[Lnode].end(), tree[Rnode].begin(), tree[Rnode].end(), back_inserter(tree[node])); } long long query(long long node, long long left, long long right, long long i, long long j, long long val) { if (left > j || right < i) return 0; if (left >= i && right <= j) { return lower_bound(tree[node].begin(), tree[node].end(), val + 1LL) - tree[node].begin(); } long long mid = left + ((right - left) >> 1); long long Lnode = (node << 1); long long Rnode = (node << 1) + 1; return query(Lnode, left, mid, i, j, val) + query(Rnode, mid + 1, right, i, j, val); } long long Fun(long long L, long long R, long long k) { long long left = -1000000000LL, right = 1000000000LL, mid, res; while (left <= right) { mid = left + ((right - left) >> 1); long long koyta = query(1, 1, n, L, R, mid); if (koyta >= k) { res = mid; right = mid - 1; } else left = mid + 1; } return res; } bool prm[1000007]; long long N = 1000000; void sieve() { prm[0] = 1; for (long long i = 2; i <= N; i++) prm[i] = 0; for (long long i = 3; i * i <= N; i += 2) { if (prm[i >> 1] == 0) { for (long long j = i * i; j <= N; j += (i << 1)) { prm[j >> 1] = 1; } } } } long long Factor[100005], Sz; long long cnt[100005], Mx; long long Pre[100005]; long long Pre1[100005]; void PrimeFactorize(long long val) { long long i; if (val % 2 == 0) { Sz++; Factor[Sz] = 2; long long tot = 0; while (val % 2 == 0) { val /= 2; tot++; } cnt[2] = tot; Mx = max(Mx, tot); } for (i = 3; i <= sqrt(val); i += 2) { if (val % i == 0) { Sz++; Factor[Sz] = i; long long tot = 0; while (val % i == 0) { val /= i; tot++; } cnt[i] = tot; Mx = max(Mx, tot); } } if (val > 2) { Sz++; Factor[Sz] = val; cnt[val] = 1; } } char s[100005]; int main() { long long i, j, q; n = in<long long>(), q = in<long long>(); scanf("%s", &s); for (i = n; i >= 1; i--) s[i] = s[i - 1]; for (i = 1; i <= n; i++) { cnt[i] = s[i] - 48; } for (i = 1; i <= n; i++) { cnt[i] += cnt[i - 1]; } long long d = 1; for (i = 1; i <= n; i++) { Pre[i] = (Pre[i - 1] + d) % 1000000007LL; Pre1[i] = (Pre1[i - 1] + d * i) % 1000000007LL; d *= 2; d %= 1000000007LL; } for (i = 1; i <= q; i++) { long long l, r; l = in<long long>(), r = in<long long>(); long long m = r - l + 1; long long d = cnt[r] - cnt[l - 1]; long long d1 = m - d; long long res = 0; if (d) { res = (d + d * (Pre[m - 1])) % 1000000007LL; res -= ((Bigmod(2LL, d1, 1000000007LL) * Pre1[d - 1])) % 1000000007LL; res %= 1000000007LL; res += 1000000007LL; res %= 1000000007LL; } printf("%lld\n", res); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long int INFL = 1e18; const int MAX_N = 1e5; const long long int MOD = 1e9 + 7; long long int cntsum[MAX_N + 2]; long long int psum[MAX_N + 2]; long long int get_sum(int l, int r, long long int* arr) { if (l == 0) return arr[r]; else return (arr[r] - arr[l - 1] + MOD) % MOD; } int main(void) { cin.sync_with_stdio(false), cin.tie(NULL); long long cur = 1; for (long long int i = 1; i <= MAX_N; i++) { psum[i] = cur; psum[i] += psum[i - 1]; psum[i] %= MOD; cur = (cur * 2) % MOD; } int N, Q; cin >> N >> Q; for (int i = 1; i <= N; i++) { char ch; cin >> ch; cntsum[i] = ch - '0'; cntsum[i] += cntsum[i - 1]; } while (Q--) { int l, r; cin >> l >> r; int one_cnt = get_sum(l, r, cntsum); cout << get_sum((r - l + 2 - one_cnt), r - l + 1, psum) << '\n'; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long int INFL = 1e18; const int MAX_N = 1e5; const long long int MOD = 1e9 + 7; long long int cntsum[MAX_N + 2]; long long int psum[MAX_N + 2]; long long int get_sum(int l, int r, long long int* arr) { if (l == 0) return arr[r]; else return (arr[r] - arr[l - 1] + MOD) % MOD; } int main(void) { cin.sync_with_stdio(false), cin.tie(NULL); long long cur = 1; for (long long int i = 1; i <= MAX_N; i++) { psum[i] = cur; psum[i] += psum[i - 1]; psum[i] %= MOD; cur = (cur * 2) % MOD; } int N, Q; cin >> N >> Q; for (int i = 1; i <= N; i++) { char ch; cin >> ch; cntsum[i] = ch - '0'; cntsum[i] += cntsum[i - 1]; } while (Q--) { int l, r; cin >> l >> r; int one_cnt = get_sum(l, r, cntsum); cout << get_sum((r - l + 2 - one_cnt), r - l + 1, psum) << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long ps[100005]; long long p2[100005]; inline long long mathh(long long x, long long n) { n -= x; long long ans = (p2[x] - 1) % mod; ans += ans * (p2[n] - 1); return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, q, a, b; string s; cin >> n >> q; cin >> s; for (long long x = 1; x <= n; x++) { ps[x] = ps[x - 1]; if (s[x - 1] == '1') ps[x]++; } p2[0] = 1; for (long long x = 1; x <= n; x++) { p2[x] = p2[x - 1] * 2; p2[x] %= mod; } while (q--) { cin >> a >> b; cout << mathh(ps[b] - ps[a - 1], b - a + 1) % mod << '\n'; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long ps[100005]; long long p2[100005]; inline long long mathh(long long x, long long n) { n -= x; long long ans = (p2[x] - 1) % mod; ans += ans * (p2[n] - 1); return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, q, a, b; string s; cin >> n >> q; cin >> s; for (long long x = 1; x <= n; x++) { ps[x] = ps[x - 1]; if (s[x - 1] == '1') ps[x]++; } p2[0] = 1; for (long long x = 1; x <= n; x++) { p2[x] = p2[x - 1] * 2; p2[x] %= mod; } while (q--) { cin >> a >> b; cout << mathh(ps[b] - ps[a - 1], b - a + 1) % mod << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long pow_2[200010]; int a[200010]; int b[200010]; int main() { pow_2[0] = 1; for (long long i = 1; i <= 200000; i++) { pow_2[i] = (pow_2[i - 1] * 2) % mod; } fill(a, a + 100010, 0); fill(b, b + 100010, 0); int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%1d", &a[i]); } for (int i = 1; i <= n; i++) { b[i] = b[i - 1]; if (a[i] == 1) b[i]++; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long s1 = b[r] - b[l - 1]; long long s2 = r - l + 1 - s1; int a1 = (pow_2[s1] - 1); long long s = (a1 * (pow_2[s2] - 1)) % mod; s = (s + (pow_2[s1] - 1)) % mod; cout << s << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long pow_2[200010]; int a[200010]; int b[200010]; int main() { pow_2[0] = 1; for (long long i = 1; i <= 200000; i++) { pow_2[i] = (pow_2[i - 1] * 2) % mod; } fill(a, a + 100010, 0); fill(b, b + 100010, 0); int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%1d", &a[i]); } for (int i = 1; i <= n; i++) { b[i] = b[i - 1]; if (a[i] == 1) b[i]++; } while (q--) { int l, r; scanf("%d%d", &l, &r); long long s1 = b[r] - b[l - 1]; long long s2 = r - l + 1 - s1; int a1 = (pow_2[s1] - 1); long long s = (a1 * (pow_2[s2] - 1)) % mod; s = (s + (pow_2[s1] - 1)) % mod; cout << s << endl; } return 0; } ```