Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1062_C. Banh-mi
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.
2
9
/** * Created by Aminul on 11/14/2018. */ import java.io.*; import java.util.*; import static java.lang.Math.*; public class C { public static void main(String[] args)throws Exception { FastReader in = new FastReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(), q = in.nextInt(); char [] s = in.next(n); int a[] = new int[n+1]; for (int i = 1; i <= n; i++) { a[i] = (s[i-1] - '0') + a[i-1]; } for (int i = 0; i < q; i++) { int l = in.nextInt(), r = in.nextInt(); int range = (r-l+1), ones = ones(a, l, r), zero = range - ones; if(ones == 0) { pw.println(0); continue; } long N = geometricSum_2(1, ones); long zeroStart = mod(bigMod(2, ones, mod)-1, mod); long M = geometricSum_2(zeroStart, zero); long res = (N + M + mod) % mod; pw.println(res); } pw.close(); } static long geometricSum_2(long a, long n) { long res = mod(bigMod(2, n, mod)-1, mod); res = (res * a) % mod; return res; } static int ones(int a[], int i, int j) { return a[j] - a[i-1]; } static long mod = (long)1e9+7; static long inv2; static long bigMod ( long a, long p, long m ) { // returns (a^p) % m long res = 1 % m, x = a % m; while ( p > 0 ) { if ( (p & 1) > 0 ) res = ( res * x ) % m; x = ( x * x ) % m; p >>= 1; } return res; } public static long modInverse(long a, long m) { a = mod(a, m); return a == 0 ? 0 : mod((1 - modInverse(m % a, a) * m) / a, m); } public static long mod(long a, long m) { long A = (a % m); return A >= 0 ? A : A + m; } static void debug(Object...obj) { System.err.println(Arrays.deepToString(obj)); } static class FastReader { InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; public FastReader(InputStream is) { this.is = is; } public int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { int c = skip(); StringBuilder sb = new StringBuilder(); while (!isEndOfLine(c)){ sb.appendCodePoint(c); c = readByte(); } return sb.toString(); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } public char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public char readChar() { return (char) skip(); } } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
//package math_codet; import java.io.*; import java.util.*; public class lets_do { InputReader in; PrintWriter out; Helper_class h; final long mod = 1000000007; public static void main(String[] args) throws java.lang.Exception{ new lets_do().run(); } void run() throws Exception{ in=new InputReader(); out = new PrintWriter(System.out); h = new Helper_class(); int t = 1; while(t-->0) solve(); out.flush(); out.close(); } void solve(){ int n = h.ni(); int q = h.ni(); int i = 0; String st = h.n(); int[] pf = new int[n]; pf[0] = st.charAt(0) - '0'; for(i = 1; i < n; i++) pf[i] = pf[i - 1] + st.charAt(i) - '0'; while(q-->0){ int l = h.ni() - 1; int r = h.ni() - 1; int ones = l == 0 ? pf[r] : pf[r] - pf[l - 1]; int zeroes = r - l + 1 - ones; //h.pn(ones+" "+zeroes); long xx = (h.modPow(2, ones) - 1 + mod) % mod; long yy = (h.modPow(2, zeroes) - 1 + mod) % mod; xx = (xx * (yy + 1)) % mod; h.pn(xx); } } static final Comparator<Entity> com=new Comparator<Entity>(){ public int compare(Entity a, Entity b){ int xx=Integer.compare(a.x,b.x); if(xx==0) return Integer.compare(a.y,b.y); else return xx; } }; class Entity{ int x, y; Entity(int p, int q){ x=p; y=q; } } class Helper_class{ long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bitcount(long n){return (n==0)?0:(1+bitcount(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n(){return in.next();} String nln(){return in.nextLine();} int ni(){return Integer.parseInt(in.next());} long nl(){return Long.parseLong(in.next());} double nd(){return Double.parseDouble(in.next());} long mul(long a,long b){ if(a>=mod)a%=mod; if(b>=mod)b%=mod; a*=b; if(a>=mod)a%=mod; return a; } long modPow(long a, long p){ long o = 1; while(p>0){ if((p&1)==1)o = mul(o,a); a = mul(a,a); p>>=1; } return o; } long add(long a, long b){ if(a>=mod)a%=mod; if(b>=mod)b%=mod; if(b<0)b+=mod; a+=b; if(a>=mod)a-=mod; return a; } } class InputReader{ BufferedReader br; StringTokenizer st; public InputReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public InputReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Nikita Mikhailov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); CBanMi solver = new CBanMi(); solver.solve(1, in, out); out.close(); } static class CBanMi { long mod = (long) (1e9 + 7); long calc(long n, long k) { long res = 0; long r1 = (Utils.pow(2, n, mod) + mod - 1) % mod; long a2 = r1 * (Utils.pow(2, k, mod) + mod - 1) % mod; return (r1 + a2) % mod; } public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.readInt(); int q = in.readInt(); String str = in.readToken(); SumCounterInt sc = new SumCounterInt(n); for (int i = 0; i < str.length(); i++) { sc.next(str.charAt(i) == '1' ? 1 : 0); } for (int i = 0; i < q; i++) { int l = in.readInt() - 1, r = in.readInt() - 1; long tot = (int) sc.getSum(l, r + 1); tot = calc(tot, r - l + 1 - tot); out.println(tot); } } } static class FastScanner { private StringTokenizer st; private BufferedReader in; public FastScanner(final InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public String readToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (final IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(readToken()); } } static final class Utils { private Utils() { } public static long pow(long x, long n, long mod) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) { if ((n & 1) != 0) { res = res * p % mod; } } return res; } public static int constrain(int value, int min, int max) { return Math.min(Math.max(value, min), max); } } static class SumCounterInt { private int n; private int pos; private int[] sums; private boolean fixRanges; public SumCounterInt(int n) { this(n, true); } public SumCounterInt(int n, boolean fixRanges) { this.n = n; this.pos = 1; this.sums = new int[n + 1]; this.fixRanges = fixRanges; } public void next(int value) { sums[pos] = sums[pos - 1] + value; pos++; } public long getSum(int from, int to) { if (fixRanges) { to = Utils.constrain(to, 0, n); from = Utils.constrain(from, 0, n - 1); if (from >= to) { return 0; } } return sums[to] - sums[from]; } } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
n,q=map(int,raw_input().split()) s=raw_input() mod=1000000007 powers=[1 for i in range(n+1)] for i in range(1,n+1): powers[i]=(powers[i-1]*2)%mod cum=[0 for i in range(n+1)] for i in range(n): if s[i]=='0': cum[i+1]=cum[i]+1 else: cum[i+1]=cum[i] while q: l,r=map(int,raw_input().split()) lent=r-l+1 zeroes=cum[r]-cum[l-1] if zeroes==0: print powers[lent]-1 else: temp=powers[zeroes]%mod temp2=(powers[lent-zeroes]-1)%mod print (temp*temp2)%mod q-=1
PYTHON
1062_C. Banh-mi
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.
2
9
import java.util.*; import java.io.*; public class Main { public static int mod = 1000000007; public static long pow(long x, long n) { if (n == 0) { return 1; } long x2 = x * x % mod; return pow(x2, n/2) * (n % 2 == 0 ? 1 : x) % mod; } public static void solve(InputReader in) { int n = in.readInt(); int q = in.readInt(); char s[] = in.readString().toCharArray(); char a[] = new char[n + 1]; for (int i = 1; i < n + 1; i++) { a[i] = s[i - 1]; } int pref[] = new int[n + 1]; for (int i = 1; i < n + 1; i++) { if (a[i] == '1') { pref[i] = pref[i - 1] + 1; } else pref[i] = pref[i - 1]; } for (int i = 0; i < q; i++) { int l = in.readInt(), r = in.readInt(); long noOf1s = pref[r] - pref[l - 1]; long noOf0s = (r - (l - 1)) - noOf1s; long res = pow(2, noOf1s) - 1; res = (res * pow(2, noOf0s))%mod; System.out.println(res); } } public static void main(String[] args) { InputReader in = new InputReader(System.in); int t = 1; while (t-- > 0) solve(in); } } class InputReader{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
1062_C. Banh-mi
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.
2
9
#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"; } }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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(); } }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Wolfgang Beyer */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { long MOD = 1000000007; public void solve(int testNumber, InputReader in, PrintWriter out) { long[] prec = new long[100001]; long current = 1; prec[0] = 1L; for (int i = 1; i <= 100000; i++) { current *= 2; current %= MOD; prec[i] = current; } int n = in.nextInt(); int q = in.nextInt(); String str = in.next(); int[] s = new int[n + 1]; for (int i = 1; i <= n; i++) { if (str.charAt(i - 1) == '1') { s[i] = s[i - 1] + 1; } else { s[i] = s[i - 1]; } } for (int i = 0; i < q; i++) { int left = in.nextInt(); int right = in.nextInt(); int len = right - left + 1; int ones = s[right] - s[left - 1]; int zeroes = len - ones; //long result = twoPow(len) - twoPow(zeroes); long result = prec[len] - prec[zeroes]; result = (result + MOD) % MOD; out.println(result); } } } static class InputReader { private static BufferedReader in; private static StringTokenizer tok; public InputReader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public int nextInt() { return Integer.parseInt(next()); } public String next() { try { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter } } catch (IOException ex) { System.err.println("An IOException was caught :" + ex.getMessage()); } return tok.nextToken(); } } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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(); } }
CPP
1062_C. Banh-mi
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.
2
9
import java.util.*; import java.lang.*; public class c { public static final long MOD = (long)1000000007; public static void main(String[] args) { Scanner in = new Scanner(System.in); Locale.setDefault(Locale.US); int n = in.nextInt(); int q = in.nextInt(); int[] pref = new int[n + 1]; in.nextLine(); char[] s = in.nextLine().toCharArray(); long[] st = new long[n + 1]; st[0] = 1; for (int i = 0; i < n; ++i) { int x = s[i] - '0'; st[i + 1] = st[i] * 2 % MOD; pref[i + 1] = pref[i] + x; } for (int i = 0; i < q; ++i) { int l = in.nextInt(); int r = in.nextInt(); l--; int cnt = pref[r] - pref[l]; int len = r - l; if (cnt == 0) { System.out.print("0\n"); continue; } System.out.print((MOD + st[cnt] - 1 + (st[cnt] - 1) * ((st[len - cnt] - 1 + MOD) % MOD)) % MOD + "\n"); } } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; InputStream is; PrintWriter out; String INPUT = ""; int[][] m; List<Integer> g[]; private static final long MOD = 1000000007L; void solve() { StringBuffer sb = new StringBuffer(); int N = ni(); int Q = ni(); char[] a = ns().toCharArray(); int pre[] = new int[N + 1]; for (int i = 0; i < N; i++) pre[i + 1] = pre[i] + (a[i] - '0'); long pow[] = new long[N + 1]; pow[0] = 1; for (int i = 1; i <= N; i++) pow[i] = (pow[i - 1] * 2) % MOD; while (Q-- > 0) { int l = ni(); int r = ni(); int ones = pre[r] - pre[l - 1]; int zero = r - l + 1 - ones; long ans = pow[ones] - 1; long base = ans; ans = ans + (pow[zero] - 1) * base % MOD; ans = (ans + MOD) % MOD; out.println(ans); } } private int next(int p) { int n = Integer.highestOneBit(p); if (n != p) return n << 1; return n; } long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private boolean vis[]; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Integer[] na2(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private Integer[][] na2(int n, int m) { Integer[][] a = new Integer[n][]; for (int i = 0; i < n; i++) a[i] = na2(m); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
JAVA
1062_C. Banh-mi
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.
2
9
import math if __name__ == '__main__': n,q = [int(x) for x in raw_input().split()] qq = str(raw_input()) s = [ int(x) for x in qq] prefix = [0]*n prefix[0]= s[0] temp = [0]*(n+1) temp[0]=1 mod = (pow(10,9)//1)+7 for i in range(1,n): prefix[i] += prefix[i-1] + s[i] temp[i] =( 2*(temp[i-1]%mod) )%mod temp[n] = (2*(temp[n-1]%mod))%mod ansarr=[] while q> 0: q-=1 l,r = [int(x)-1 for x in raw_input().split()] a = prefix[r]-prefix[l]+s[l] d = r-l+1 val1 = temp[d] val2 = temp[d-a] # val2 = val2%mod ansarr.append((val1-val2)%mod) print('\n'.join(map(str, ansarr)))
PYTHON
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.math.*; import java.util.*; public class C { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.next(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.charAt(i) - '0'; for (int i = 1; i < n; i++) { arr[i] += arr[i - 1]; } long[] pow2 = new long[n + 5]; int mod = (int) 1e9 + 7; pow2[0] = 1; for (int i = 1; i < pow2.length; i++) pow2[i] = (pow2[i-1] * 2) % mod; while (q-- > 0) { int l = sc.nextInt() - 1; int r = sc.nextInt() - 1; int ones = arr[r]; if (l > 0) ones -= arr[l - 1]; long zeros = (r - l + 1) - ones; long x = (pow2[ones] - 1); long ans = (x * (pow2[(int)zeros] - 1))%mod; pw.println((ans + x + mod)%mod); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; } }
CPP
1062_C. Banh-mi
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.
2
9
import sys input = sys.stdin.readline n, q = map(int, input().split()) s = input() pref = [0 for i in range(n + 1)] for i in range(1, n + 1): pref[i] = pref[i - 1] + (s[i - 1] == '1') mod = 1000000007 ans = [] for i in range(q): a, b = map(int, input().split()) k = pref[b] - pref[a - 1]; N = b - a + 1 z = N - k ans.append((pow(2, k, mod) - 1) * pow(2, z, mod) % mod) print(*ans)
PYTHON3
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { static long mod = (int)1e9 + 7 ; public static void main(String[] args) { int n = fsca.nextInt(); int q = fsca.nextInt(); char[] s = fsca.next().toCharArray(); int[] sum = new int[s.length + 1]; long[] e2 = new long[s.length + 1]; long mod = 1000000007L; e2[0] = 1; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + ((s[i - 1] == '1') ? 1 : 0); e2[i] = e2[i - 1] * 2; e2[i] %= mod; } while (q-- > 0) { int l = fsca.nextInt(); int r = fsca.nextInt(); int len = r - l + 1; int one = sum[r] - sum[l - 1]; int zero = len - one; long ans = e2[one] - 1; long base = ans; ans = ans + (e2[zero] - 1) * base % mod; ans %= mod; fop.println(ans); } fop.flush(); fop.close(); } /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ static PrintWriter fop = new PrintWriter(System.out); static FastScanner fsca = new FastScanner(); static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } ; static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[][] readMat(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } char[][] readCharMat(int n, int m) { char a[][] = new char[n][m]; for (int i = 0; i < n; i++) { String s = next(); for (int j = 0; j < m; j++) a[i][j] = s.charAt(j); } return a; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void print(int a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } static void print(long a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } }
JAVA
1062_C. Banh-mi
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.
2
9
import sys mod=10**9+7 n,q=map(int,sys.stdin.readline().split()) S=sys.stdin.readline().strip() LR=[list(map(int,sys.stdin.readline().split())) for i in range(q)] LIST=[0] for s in S: if s=="1": LIST.append(LIST[-1]+1) else: LIST.append(LIST[-1]) def count(m,n,mod): return (pow(2,m,mod)-1)*pow(2,n,mod)%mod for l,r in LR: print(count(LIST[r]-LIST[l-1],r-l+1-LIST[r]+LIST[l-1],mod))
PYTHON3
1062_C. Banh-mi
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.
2
9
//package Div2_520; import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class C { public static BufferedReader input; public static PrintWriter out; public StringTokenizer stoken = new StringTokenizer(""); public static void main(String[] args) throws IOException { input = new BufferedReader( new InputStreamReader(System.in) ); out = new PrintWriter(System.out); new C(); out.close(); } C() throws IOException { int n = nextInt(); int q = nextInt(); long mod = 1000_000_007; long s[] = new long[n + 1]; s[0] = 1; for (int i = 1; i < n + 1; i++) { s[i] = s[i - 1] * 2; s[i] %= mod; } String c = nextString(); int mas[] = new int[n]; for (int i = 0; i < n; i++) { mas[i] = Integer.parseInt(String.valueOf(c.charAt(i))); } int pref[] = new int[n + 1]; pref[1] = mas[0]; for (int i = 1; i < n; i++) { pref[i + 1] += pref[i] + mas[i]; } for (int i = 0; i < q; i++) { int l = nextInt(); int r = nextInt(); int d = r - l + 1; int one = pref[r] - pref[l - 1]; int nu = d - one; long ans = s[one] - 1 + (s[one] - 1) * (s[nu] - 1); ans %= mod; out.println(ans); } } private String nextString() throws IOException { while (!stoken.hasMoreTokens()) { String st = input.readLine(); stoken = new StringTokenizer(st); } return stoken.nextToken(); } private Integer nextInt() throws IOException { return Integer.parseInt(nextString()); } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.lang.reflect.Array; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int q = Integer.parseInt(st.nextToken()); String s = br.readLine(); long mod = 1000000007; long [] p2s = new long[100002]; p2s[0]=1; for (int i=1; i<p2s.length; ++i){ p2s[i]=p2s[i-1]*2; p2s[i]%=mod; } int[] arr = new int[n]; int[] acc = new int[n+1]; for (int i=0; i<n; ++i){ arr[i] = s.charAt(i)-'0'; acc[i+1] = acc[i] + arr[i]; } for (int qq=0; qq<q; ++qq){ st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int d = r-l+1; int c = acc[r]-acc[l-1]; if (c == 0){ bw.write(0); bw.newLine(); } else { long cc = ((p2s[c]+mod)-1)%mod; long cc2 = ((p2s[d-c]+mod)-1)%mod; cc2*= cc; cc2%=mod; cc+=cc2; cc%=mod; bw.write(""+cc); bw.newLine(); } } sc.close(); br.close(); bw.close(); } }
JAVA
1062_C. Banh-mi
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.
2
9
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split() n = int(n) q = int(q) s = r[1] p = [0] for v in range(n): p.append(p[v] + int(s[v])) ans = [] for k in range(q): a, b = r[k + 2].strip().split() a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] ans.append(str((pow(2, l, M) - pow(2, l - one, M) + M) % M)) sys.stdout.write("\n".join(ans))
PYTHON3
1062_C. Banh-mi
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.
2
9
from __future__ import division from sys import stdin, stdout m = 10 ** 9 + 7 def write(x): stdout.write(str(x) + "\n") n, q = map(int, stdin.readline().split()) x = stdin.readline().rstrip("\n") sums = [0] * (n + 1) s = 0 for i, c in enumerate(x): if c == "1": s += 1 sums[i + 1] = s for line in stdin.readlines(): l, r = map(int, line.split()) # l -= 1 # r -= 1 eating = r - l + 1 s1 = sums[r] - sums[l - 1] s0 = eating - s1 mel = pow(2, s1, m) res = mel - 1 res = res + (mel - 1) * (pow(2, s0, m) - 1) % m write(res % m)
PYTHON
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
n,q=map(int,raw_input().split()) powers=[1]+[0]*n for i in xrange(n): powers[i+1]=(2*powers[i])%1000000007 s=raw_input() zeroes=[0]*(n+1) for i in xrange(n): zeroes[i+1]=zeroes[i]+1-int(s[i]) for i in xrange(q): l,r=map(int,raw_input().split()) guy=powers[r-l+1]-powers[zeroes[r]-zeroes[l-1]] guy%=1000000007 print(guy)
PYTHON
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
import java.util.*; import java.io.*; import java.math.*; public class C{ static void precompute(int n){ pow2 = new long[n + 1]; pow2[0] = 1l; for(int i = 1; i <= n; i++) pow2[i] = (pow2[i - 1] * 2l) % mod; } public static void process(int testNumber){ int n = ni(), q = ni(); precompute(n); char arr[] = (" " + nln()).toCharArray(); long pref[] = new long[n + 1]; for(int i = 1; i <= n; i++){ pref[i] += pref[i - 1] + (arr[i] - '0'); } for(int query = 1; query <= q; query++){ int l = ni(), r = ni(); long ones = pref[r] - pref[l - 1], zeroes = (r - l + 1)*1l - ones, res = 0l; res += pow2[(int)ones] - 1; res += mod; res %= mod; long x = res; res += (x * ((pow2[(int)zeroes] - 1) + mod)%mod) % mod; res %= mod; pn(res); } } static long pow2[]; static final long mod = (long)1e9+7l; static boolean DEBUG = true; static FastReader sc; static PrintWriter out = new PrintWriter(System.out); public static void main(String[]args){ sc = new FastReader(); long s = System.currentTimeMillis(); int t = 1; // t = ni(); for(int i = 1; i <= t; i++) process(i); out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); } static void trace(Object... o){ if(!DEBUG) return; System.err.println(Arrays.deepToString(o)); }; static void pn(Object o){ out.println(o); } static void p(Object o){ out.print(o); } static int ni(){ return Integer.parseInt(sc.next()); } static long nl(){ return Long.parseLong(sc.next()); } static double nd(){ return Double.parseDouble(sc.next()); } static String nln(){ return sc.nextLine(); } static long gcd(long a, long b){ return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b){ return (b==0)?a:gcd(b,a%b); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class pair implements Comparable<pair> { int first, second; public pair(int first, int second){ this.first = first; this.second = second; } @Override public int compareTo(pair ob){ if(this.first != ob.first) return this.first - ob.first; return this.second - ob.second; } static public pair from(int f, int s){ return new pair(f, s); } }
JAVA
1062_C. Banh-mi
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.
2
9
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split() n = int(n) q = int(q) s = r[1] p = [0] k = 0 for v in range(n): d = int(s[v]) k += (1 - d) p.append(k) for k in range(q): a, b = r[k + 2].strip().split() a = int(a) b = int(b) l = b - a + 1 zero = p[b] - p[a - 1] print((pow(2, l, M) - pow(2, zero, M) + M) % M)
PYTHON3
1062_C. Banh-mi
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.
2
9
from sys import stdin, stdout def BigMod(a, b, m): if b == 0: return 1 % m x = BigMod(int(a), int(b / 2), int(m)) x = (x * x) % m if b % 2 == 1: return (x * a) % m return x n, m = map(int, stdin.readline().split()) ch = stdin.readline() base = [1 for i in range(n + 1)] pw = [1 for i in range(n + 1)] cum = [0 for i in range(n + 1)] # print(len(cum)) for i in range(n): cum[i + 1] = cum[i] if ch[i] == '1': cum[i + 1] += 1 # print(cum[i]) Mod = int(1e9 + 7) pw[0] = int(1) for i in range(1, n + 1): pw[i] = int(int(pw[i - 1] * 2) % Mod) # print(pw[i]) for i in range(1, n + 1): base[i] = ((base[i - 1] % Mod) + (pw[i] % Mod)) % Mod # print(base[i]) while m > 0: l, r = map(int, stdin.readline().split()) cur = cum[r] - cum[l - 1] m -= 1 if cur == 0: stdout.write("0" + '\n') continue ans = int(((base[cur - 1] % Mod) * (pw[int((r - l + 1) - cur)] % Mod)) % Mod) stdout.write(str(ans) + '\n')
PYTHON3
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
import java.util.*; import java.math.*; import java.io.*; public class CF1062C { long exp(long b, long e) { if(e == 0) return 1; long tmp = exp(b, e / 2); tmp = (tmp * tmp) % MOD; if((e & 1) == 1) tmp = (tmp * b) % MOD; return tmp; } final int MOD = (int) 1e9 + 7; public CF1062C() { FS scan = new FS(); PrintWriter out = new PrintWriter(System.out); int n = scan.nextInt(), q = scan.nextInt(); char[] x = scan.next().toCharArray(); int[] pre1 = new int[n + 1]; int[] pre0 = new int[n + 1]; for(int i = 1 ; i <= n ; i++) { pre0[i] = pre0[i - 1] + (x[i - 1] == '0' ? 1 : 0); pre1[i] = pre1[i - 1] + (x[i - 1] == '1' ? 1 : 0); } for(int qq = 0 ; qq < q ; qq++) { int l = scan.nextInt(), r = scan.nextInt(); int z = pre0[r] - pre0[l - 1]; int o = pre1[r] - pre1[l - 1]; long res = (exp(2, z) - 1 + MOD) % MOD; res *= (exp(2, o) - 1 + MOD) % MOD; res %= MOD; res += (exp(2, o) - 1 + MOD) % MOD; res %= MOD; out.println(res); } out.close(); } class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] args) { new CF1062C(); } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.util.*; import java.util.Collections; import java.util.Arrays; public class Codechef { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); long powers[]=new long[100001]; powers[0]=1; for(int i=1;i<100001;i++){ powers[i]=(2*powers[i-1])%1000000007; } int n=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); int[] a=new int[n+1]; for(int h=1;h<=n;h++){ if(s.charAt(h-1)=='1'){ a[h]=a[h-1]+1; }else a[h]=a[h-1]; //System.out.print("a["+(h)+"] = "+a[h]+" "); } for(int i=0;i<q;i++){ int l=sc.nextInt(); int r=sc.nextInt(); int ones=a[r]-a[l-1]; int zeros=r-l+1-ones; long onesum=powers[ones]-1; long zerosum=(onesum*(powers[zeros]))%1000000007; System.out.println(zerosum); // System.out.println("ones:"+ones+" zeros: "+zeros); // long sum=(long)Math.pow(2,k.length())-1; // long sum2=(long)Math.pow(2,count)-1; // long result=powers(2,r-l+1)-powers(2,zeros); //System.out.println(result%1000000007); } } }
JAVA
1062_C. Banh-mi
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.
2
9
//package baobab; import java.io.*; import java.util.*; public class C { public static void main(String[] args) { Solver solver = new Solver(); } static class Solver { IO io; public Solver() { this.io = new IO(); try { solve(); } catch (RuntimeException e) { if (!e.getMessage().equals("Clean exit")) { throw e; } } finally { io.close(); } } /****************************** START READING HERE ********************************/ void solve() { int n = io.nextInt(); int q = io.nextInt(); String banhmi = io.next(); SegmentTree seg = new SegmentTree(n+5, true, false, false); for (int i=0; i<n; i++) { char c = banhmi.charAt(i); if (c == '1') seg.modifyRange(1, i, i); } long[] h1 = new long[100001]; long remainingOnesValue = 1; for (int i=1; i<=100000; i++) { h1[i] = h1[i-1] + remainingOnesValue; h1[i] %= MOD; remainingOnesValue += remainingOnesValue; remainingOnesValue %= MOD; } long[] h2 = new long[100001]; long next = 1; for (int i=1; i<=100000; i++) { h2[i] = h2[i-1] + next; h2[i] %= MOD; next *= 2; next %= MOD; } for (int i=0; i<q; i++) { int left = io.nextInt()-1; int right = io.nextInt()-1; int sumOfOnesInRange = (int) seg.getSum(left, right); int sumOfZeroesInRange = (right-left+1-sumOfOnesInRange); if (sumOfOnesInRange == 0) { // special case io.println(0); continue; } // general case long ans = h1[right-left+1]; ans %= MOD; ans -= h2[sumOfZeroesInRange]; if (ans < 0) ans += MOD; io.println(ans); } } /************************** UTILITY CODE BELOW THIS LINE **************************/ long MOD = (long)1e9 + 7; boolean closeToZero(double v) { // Check if double is close to zero, considering precision issues. return Math.abs(v) <= 0.0000000001; } class DrawGrid { void draw(boolean[][] d) { System.out.print(" "); for (int x=0; x<d[0].length; x++) { System.out.print(" " + x + " "); } System.out.println(""); for (int y=0; y<d.length; y++) { System.out.print(y + " "); for (int x=0; x<d[0].length; x++) { System.out.print((d[y][x] ? "[x]" : "[ ]")); } System.out.println(""); } } void draw(int[][] d) { int max = 1; for (int y=0; y<d.length; y++) { for (int x=0; x<d[0].length; x++) { max = Math.max(max, ("" + d[y][x]).length()); } } System.out.print(" "); String format = "%" + (max+2) + "s"; for (int x=0; x<d[0].length; x++) { System.out.print(String.format(format, x) + " "); } format = "%" + (max) + "s"; System.out.println(""); for (int y=0; y<d.length; y++) { System.out.print(y + " "); for (int x=0; x<d[0].length; x++) { System.out.print(" [" + String.format(format, (d[y][x])) + "]"); } System.out.println(""); } } } class IDval implements Comparable<IDval> { int id; long val; public IDval(int id, long val) { this.val = val; this.id = id; } @Override public int compareTo(IDval o) { if (this.val < o.val) return -1; if (this.val > o.val) return 1; return this.id - o.id; } } private class ElementCounter { private HashMap<Long, Integer> elements; public ElementCounter() { elements = new HashMap<>(); } public void add(long element) { int count = 1; Integer prev = elements.get(element); if (prev != null) count += prev; elements.put(element, count); } public void remove(long element) { int count = elements.remove(element); count--; if (count > 0) elements.put(element, count); } public int get(long element) { Integer val = elements.get(element); if (val == null) return 0; return val; } public int size() { return elements.size(); } } class StringCounter { HashMap<String, Integer> elements; public StringCounter() { elements = new HashMap<>(); } public void add(String identifier) { int count = 1; Integer prev = elements.get(identifier); if (prev != null) count += prev; elements.put(identifier, count); } public void remove(String identifier) { int count = elements.remove(identifier); count--; if (count > 0) elements.put(identifier, count); } public long get(String identifier) { Integer val = elements.get(identifier); if (val == null) return 0; return val; } public int size() { return elements.size(); } } class DisjointSet { /** Union Find / Disjoint Set data structure. */ int[] size; int[] parent; int componentCount; public DisjointSet(int n) { componentCount = n; size = new int[n]; parent = new int[n]; for (int i=0; i<n; i++) parent[i] = i; for (int i=0; i<n; i++) size[i] = 1; } public void join(int a, int b) { /* Find roots */ int rootA = parent[a]; int rootB = parent[b]; while (rootA != parent[rootA]) rootA = parent[rootA]; while (rootB != parent[rootB]) rootB = parent[rootB]; if (rootA == rootB) { /* Already in the same set */ return; } /* Merge smaller set into larger set. */ if (size[rootA] > size[rootB]) { size[rootA] += size[rootB]; parent[rootB] = rootA; } else { size[rootB] += size[rootA]; parent[rootA] = rootB; } componentCount--; } } class Trie { int N; int Z; int nextFreeId; int[][] pointers; boolean[] end; /** maxLenSum = maximum possible sum of length of words */ public Trie(int maxLenSum, int alphabetSize) { this.N = maxLenSum; this.Z = alphabetSize; this.nextFreeId = 1; pointers = new int[N+1][alphabetSize]; end = new boolean[N+1]; } public void addWord(String word) { int curr = 0; for (int j=0; j<word.length(); j++) { int c = word.charAt(j) - 'a'; int next = pointers[curr][c]; if (next == 0) { next = nextFreeId++; pointers[curr][c] = next; } curr = next; } end[curr] = true; } public boolean hasWord(String word) { int curr = 0; for (int j=0; j<word.length(); j++) { int c = word.charAt(j) - 'a'; int next = pointers[curr][c]; if (next == 0) return false; curr = next; } return end[curr]; } } private static class Prob { /** For heavy calculations on probabilities, this class * provides more accuracy & efficiency than doubles. * Math explained: https://en.wikipedia.org/wiki/Log_probability * Quick start: * - Instantiate probabilities, eg. Prob a = new Prob(0.75) * - add(), multiply() return new objects, can perform on nulls & NaNs. * - get() returns probability as a readable double */ /** Logarithmized probability. Note: 0% represented by logP NaN. */ private double logP; /** Construct instance with real probability. */ public Prob(double real) { if (real > 0) this.logP = Math.log(real); else this.logP = Double.NaN; } /** Construct instance with already logarithmized value. */ static boolean dontLogAgain = true; public Prob(double logP, boolean anyBooleanHereToChooseThisConstructor) { this.logP = logP; } /** Returns real probability as a double. */ public double get() { return Math.exp(logP); } @Override public String toString() { return ""+get(); } /***************** STATIC METHODS BELOW ********************/ /** Note: returns NaN only when a && b are both NaN/null. */ public static Prob add(Prob a, Prob b) { if (nullOrNaN(a) && nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain); if (nullOrNaN(a)) return copy(b); if (nullOrNaN(b)) return copy(a); double x = Math.max(a.logP, b.logP); double y = Math.min(a.logP, b.logP); double sum = x + Math.log(1 + Math.exp(y - x)); return new Prob(sum, dontLogAgain); } /** Note: multiplying by null or NaN produces NaN (repping 0% real prob). */ public static Prob multiply(Prob a, Prob b) { if (nullOrNaN(a) || nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain); return new Prob(a.logP + b.logP, dontLogAgain); } /** Returns true if p is null or NaN. */ private static boolean nullOrNaN(Prob p) { return (p == null || Double.isNaN(p.logP)); } /** Returns a new instance with the same value as original. */ private static Prob copy(Prob original) { return new Prob(original.logP, dontLogAgain); } } class Binary implements Comparable<Binary> { /** * Use example: Binary b = new Binary(Long.toBinaryString(53249834L)); * * When manipulating small binary strings, instantiate new Binary(string) * When just reading large binary strings, instantiate new Binary(string,true) * get(int i) returns a character '1' or '0', not an int. */ private boolean[] d; private int first; // Starting from left, the first (most remarkable) '1' public int length; public Binary(String binaryString) { this(binaryString, false); } public Binary(String binaryString, boolean initWithMinArraySize) { length = binaryString.length(); int size = Math.max(2*length, 1); first = length/4; if (initWithMinArraySize) { first = 0; size = Math.max(length, 1); } d = new boolean[size]; for (int i=0; i<length; i++) { if (binaryString.charAt(i) == '1') d[i+first] = true; } } public void addFirst(char c) { if (first-1 < 0) doubleArraySize(); first--; d[first] = (c == '1' ? true : false); length++; } public void addLast(char c) { if (first+length >= d.length) doubleArraySize(); d[first+length] = (c == '1' ? true : false); length++; } private void doubleArraySize() { boolean[] bigArray = new boolean[(d.length+1) * 2]; int newFirst = bigArray.length / 4; for (int i=0; i<length; i++) { bigArray[i + newFirst] = d[i + first]; } first = newFirst; d = bigArray; } public boolean flip(int i) { boolean value = (this.d[first+i] ? false : true); this.d[first+i] = value; return value; } public void set(int i, char c) { boolean value = (c == '1' ? true : false); this.d[first+i] = value; } public char get(int i) { return (this.d[first+i] ? '1' : '0'); } @Override public int compareTo(Binary o) { if (this.length != o.length) return this.length - o.length; int len = this.length; for (int i=0; i<len; i++) { int diff = this.get(i) - o.get(i); if (diff != 0) return diff; } return 0; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i=0; i<length; i++) { sb.append(d[i+first] ? '1' : '0'); } return sb.toString(); } } /************************** Range queries **************************/ class FenwickMin { long n; long[] original; long[] bottomUp; long[] topDown; public FenwickMin(int n) { this.n = n; original = new long[n+2]; bottomUp = new long[n+2]; topDown = new long[n+2]; } public void set(int modifiedNode, long value) { long replaced = original[modifiedNode]; original[modifiedNode] = value; // Update left tree int i = modifiedNode; long v = value; while (i <= n) { if (v > bottomUp[i]) { if (replaced == bottomUp[i]) { v = Math.min(v, original[i]); for (int r=1 ;; r++) { int x = (i&-i)>>>r; if (x == 0) break; int child = i-x; v = Math.min(v, bottomUp[child]); } } else break; } if (v == bottomUp[i]) break; bottomUp[i] = v; i += (i&-i); } // Update right tree i = modifiedNode; v = value; while (i > 0) { if (v > topDown[i]) { if (replaced == topDown[i]) { v = Math.min(v, original[i]); for (int r=1 ;; r++) { int x = (i&-i)>>>r; if (x == 0) break; int child = i+x; if (child > n+1) break; v = Math.min(v, topDown[child]); } } else break; } if (v == topDown[i]) break; topDown[i] = v; i -= (i&-i); } } public long getMin(int a, int b) { long min = original[a]; int prev = a; int curr = prev + (prev&-prev); // parent right hand side while (curr <= b) { min = Math.min(min, topDown[prev]); // value from the other tree prev = curr; curr = prev + (prev&-prev);; } min = Math.min(min, original[prev]); prev = b; curr = prev - (prev&-prev); // parent left hand side while (curr >= a) { min = Math.min(min,bottomUp[prev]); // value from the other tree prev = curr; curr = prev - (prev&-prev); } return min; } } class FenwickSum { public long[] d; public FenwickSum(int n) { d=new long[n+1]; } /** a[0] must be unused. */ public FenwickSum(long[] a) { d=new long[a.length]; for (int i=1; i<a.length; i++) { modify(i, a[i]); } } /** Do not modify i=0. */ void modify(int i, long v) { while (i<d.length) { d[i] += v; // Move to next uplink on the RIGHT side of i i += (i&-i); } } /** Returns sum from a to b, *BOTH* inclusive. */ long getSum(int a, int b) { return getSum(b) - getSum(a-1); } private long getSum(int i) { long sum = 0; while (i>0) { sum += d[i]; // Move to next uplink on the LEFT side of i i -= (i&-i); } return sum; } } class SegmentTree { /* Provides log(n) operations for: * - Range query (sum, min or max) * - Range update ("+8 to all values between indexes 4 and 94") */ int N; long[] lazy; long[] sum; long[] min; long[] max; boolean supportSum; boolean supportMin; boolean supportMax; public SegmentTree(int n) { this(n, true, true, true); } public SegmentTree(int n, boolean supportSum, boolean supportMin, boolean supportMax) { for (N=2; N<n;) N*=2; this.lazy = new long[2*N]; this.supportSum = supportSum; this.supportMin = supportMin; this.supportMax = supportMax; if (this.supportSum) this.sum = new long[2*N]; if (this.supportMin) this.min = new long[2*N]; if (this.supportMax) this.max = new long[2*N]; } void modifyRange(long x, int a, int b) { modifyRec(a, b, 1, 0, N-1, x); } void setRange() { //TODO } long getSum(int a, int b) { return querySum(a, b, 1, 0, N-1); } long getMin(int a, int b) { return queryMin(a, b, 1, 0, N-1); } long getMax(int a, int b) { return queryMax(a, b, 1, 0, N-1); } private long querySum(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return 0; } if (wantedLeft == actualLeft && wantedRight == actualRight) { int count = wantedRight - wantedLeft + 1; return sum[i] + count * lazy[i]; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; long left = querySum(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1); long right = querySum(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight); return left + right; } private long queryMin(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return 0; } if (wantedLeft == actualLeft && wantedRight == actualRight) { return min[i] + lazy[i]; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; long left = queryMin(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1); long right = queryMin(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight); return min(left, right); } private long queryMax(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return 0; } if (wantedLeft == actualLeft && wantedRight == actualRight) { return max[i] + lazy[i]; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; long left = queryMax(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1); long right = queryMax(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight); return max(left, right); } private void modifyRec(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight, long value) { if (wantedLeft > actualRight || wantedRight < actualLeft) { return; } if (wantedLeft == actualLeft && wantedRight == actualRight) { lazy[i] += value; return; } if (lazy[i] != 0) propagate(i, actualLeft, actualRight); int d = (actualRight - actualLeft + 1) / 2; modifyRec(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1, value); modifyRec(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight, value); if (supportSum) sum[i] += value * (min(actualRight, wantedRight) - max(actualLeft, wantedLeft) + 1); if (supportMin) min[i] = min(min[2*i] + lazy[2*i], min[2*i+1] + lazy[2*i+1]); if (supportMax) max[i] = max(max[2*i] + lazy[2*i], max[2*i+1] + lazy[2*i+1]); } private void propagate(int i, int actualLeft, int actualRight) { lazy[2*i] += lazy[i]; lazy[2*i+1] += lazy[i]; if (supportSum) sum[i] += lazy[i] * (actualRight - actualLeft + 1); if (supportMin) min[i] += lazy[i]; if (supportMax) max[i] += lazy[i]; lazy[i] = 0; } } /***************************** Graphs *****************************/ List<Integer>[] toGraph(IO io, int n) { List<Integer>[] g = new ArrayList[n+1]; for (int i=1; i<=n; i++) g[i] = new ArrayList<>(); for (int i=1; i<=n-1; i++) { int a = io.nextInt(); int b = io.nextInt(); g[a].add(b); g[b].add(a); } return g; } class Graph { HashMap<Long, List<Long>> edges; public Graph() { edges = new HashMap<>(); } List<Long> getSetNeighbors(Long node) { List<Long> neighbors = edges.get(node); if (neighbors == null) { neighbors = new ArrayList<>(); edges.put(node, neighbors); } return neighbors; } void addBiEdge(Long a, Long b) { addEdge(a, b); addEdge(b, a); } void addEdge(Long from, Long to) { getSetNeighbors(to); // make sure all have initialized lists List<Long> neighbors = getSetNeighbors(from); neighbors.add(to); } // topoSort variables int UNTOUCHED = 0; int FINISHED = 2; int INPROGRESS = 1; HashMap<Long, Integer> vis; List<Long> topoAns; List<Long> failDueToCycle = new ArrayList<Long>() {{ add(-1L); }}; List<Long> topoSort() { topoAns = new ArrayList<>(); vis = new HashMap<>(); for (Long a : edges.keySet()) { if (!topoDFS(a)) return failDueToCycle; } Collections.reverse(topoAns); return topoAns; } boolean topoDFS(long curr) { Integer status = vis.get(curr); if (status == null) status = UNTOUCHED; if (status == FINISHED) return true; if (status == INPROGRESS) { return false; } vis.put(curr, INPROGRESS); for (long next : edges.get(curr)) { if (!topoDFS(next)) return false; } vis.put(curr, FINISHED); topoAns.add(curr); return true; } } public class StronglyConnectedComponents { /** Kosaraju's algorithm */ ArrayList<Integer>[] forw; ArrayList<Integer>[] bacw; /** Use: getCount(2, new int[] {1,2}, new int[] {2,1}) */ public int getCount(int n, int[] mista, int[] minne) { forw = new ArrayList[n+1]; bacw = new ArrayList[n+1]; for (int i=1; i<=n; i++) { forw[i] = new ArrayList<Integer>(); bacw[i] = new ArrayList<Integer>(); } for (int i=0; i<mista.length; i++) { int a = mista[i]; int b = minne[i]; forw[a].add(b); bacw[b].add(a); } int count = 0; List<Integer> list = new ArrayList<Integer>(); boolean[] visited = new boolean[n+1]; for (int i=1; i<=n; i++) { dfsForward(i, visited, list); } visited = new boolean[n+1]; for (int i=n-1; i>=0; i--) { int node = list.get(i); if (visited[node]) continue; count++; dfsBackward(node, visited); } return count; } public void dfsForward(int i, boolean[] visited, List<Integer> list) { if (visited[i]) return; visited[i] = true; for (int neighbor : forw[i]) { dfsForward(neighbor, visited, list); } list.add(i); } public void dfsBackward(int i, boolean[] visited) { if (visited[i]) return; visited[i] = true; for (int neighbor : bacw[i]) { dfsBackward(neighbor, visited); } } } class LCAFinder { /* O(n log n) Initialize: new LCAFinder(graph) * O(log n) Queries: find(a,b) returns lowest common ancestor for nodes a and b */ int[] nodes; int[] depths; int[] entries; int pointer; FenwickMin fenwick; public LCAFinder(List<Integer>[] graph) { this.nodes = new int[(int)10e6]; this.depths = new int[(int)10e6]; this.entries = new int[graph.length]; this.pointer = 1; boolean[] visited = new boolean[graph.length+1]; dfs(1, 0, graph, visited); fenwick = new FenwickMin(pointer-1); for (int i=1; i<pointer; i++) { fenwick.set(i, depths[i] * 1000000L + i); } } private void dfs(int node, int depth, List<Integer>[] graph, boolean[] visited) { visited[node] = true; entries[node] = pointer; nodes[pointer] = node; depths[pointer] = depth; pointer++; for (int neighbor : graph[node]) { if (visited[neighbor]) continue; dfs(neighbor, depth+1, graph, visited); nodes[pointer] = node; depths[pointer] = depth; pointer++; } } public int find(int a, int b) { int left = entries[a]; int right = entries[b]; if (left > right) { int temp = left; left = right; right = temp; } long mixedBag = fenwick.getMin(left, right); int index = (int) (mixedBag % 1000000L); return nodes[index]; } } /**************************** Geometry ****************************/ class Point { int y; int x; public Point(int y, int x) { this.y = y; this.x = x; } } boolean segmentsIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { // Returns true if segment 1-2 intersects segment 3-4 if (x1 == x2 && x3 == x4) { // Both segments are vertical if (x1 != x3) return false; if (min(y1,y2) < min(y3,y4)) { return max(y1,y2) >= min(y3,y4); } else { return max(y3,y4) >= min(y1,y2); } } if (x1 == x2) { // Only segment 1-2 is vertical. Does segment 3-4 cross it? y = a*x + b double a34 = (y4-y3)/(x4-x3); double b34 = y3 - a34*x3; double y = a34 * x1 + b34; return y >= min(y1,y2) && y <= max(y1,y2) && x1 >= min(x3,x4) && x1 <= max(x3,x4); } if (x3 == x4) { // Only segment 3-4 is vertical. Does segment 1-2 cross it? y = a*x + b double a12 = (y2-y1)/(x2-x1); double b12 = y1 - a12*x1; double y = a12 * x3 + b12; return y >= min(y3,y4) && y <= max(y3,y4) && x3 >= min(x1,x2) && x3 <= max(x1,x2); } double a12 = (y2-y1)/(x2-x1); double b12 = y1 - a12*x1; double a34 = (y4-y3)/(x4-x3); double b34 = y3 - a34*x3; if (closeToZero(a12 - a34)) { // Parallel lines return closeToZero(b12 - b34); } // Non parallel non vertical lines intersect at x. Is x part of both segments? double x = -(b12-b34)/(a12-a34); return x >= min(x1,x2) && x <= max(x1,x2) && x >= min(x3,x4) && x <= max(x3,x4); } boolean pointInsideRectangle(Point p, List<Point> r, boolean countBorderAsInside) { Point a = r.get(0); Point b = r.get(1); Point c = r.get(2); Point d = r.get(3); double apd = areaOfTriangle(a, p, d); double dpc = areaOfTriangle(d, p, c); double cpb = areaOfTriangle(c, p, b); double pba = areaOfTriangle(p, b, a); double sumOfAreas = apd + dpc + cpb + pba; if (closeToZero(sumOfAreas - areaOfRectangle(r))) { if (closeToZero(apd) || closeToZero(dpc) || closeToZero(cpb) || closeToZero(pba)) { return countBorderAsInside; } return true; } return false; } double areaOfTriangle(Point a, Point b, Point c) { return 0.5 * Math.abs((a.x-c.x)*(b.y-a.y)-(a.x-b.x)*(c.y-a.y)); } double areaOfRectangle(List<Point> r) { double side1xDiff = r.get(0).x - r.get(1).x; double side1yDiff = r.get(0).y - r.get(1).y; double side2xDiff = r.get(1).x - r.get(2).x; double side2yDiff = r.get(1).y - r.get(2).y; double side1 = Math.sqrt(side1xDiff * side1xDiff + side1yDiff * side1yDiff); double side2 = Math.sqrt(side2xDiff * side2xDiff + side2yDiff * side2yDiff); return side1 * side2; } boolean pointsOnSameLine(double x1, double y1, double x2, double y2, double x3, double y3) { double areaTimes2 = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); return (closeToZero(areaTimes2)); } class PointToLineSegmentDistanceCalculator { // Just call this double minDistFromPointToLineSegment(double point_x, double point_y, double x1, double y1, double x2, double y2) { return Math.sqrt(distToSegmentSquared(point_x, point_y, x1, y1, x2, y2)); } private double distToSegmentSquared(double point_x, double point_y, double x1, double y1, double x2, double y2) { double l2 = dist2(x1,y1,x2,y2); if (l2 == 0) return dist2(point_x, point_y, x1, y1); double t = ((point_x - x1) * (x2 - x1) + (point_y - y1) * (y2 - y1)) / l2; if (t < 0) return dist2(point_x, point_y, x1, y1); if (t > 1) return dist2(point_x, point_y, x2, y2); double com_x = x1 + t * (x2 - x1); double com_y = y1 + t * (y2 - y1); return dist2(point_x, point_y, com_x, com_y); } private double dist2(double x1, double y1, double x2, double y2) { return Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2); } } /****************************** Math ******************************/ long pow(long base, int exp) { if (exp == 0) return 1L; long x = pow(base, exp/2); long ans = x * x; if (exp % 2 != 0) ans *= base; return ans; } long gcd(long... v) { /** Chained calls to Euclidean algorithm. */ if (v.length == 1) return v[0]; long ans = gcd(v[1], v[0]); for (int i=2; i<v.length; i++) { ans = gcd(ans, v[i]); } return ans; } long gcd(long a, long b) { /** Euclidean algorithm. */ if (b == 0) return a; return gcd(b, a%b); } int[] generatePrimesUpTo(int last) { /* Sieve of Eratosthenes. Practically O(n). Values of 0 indicate primes. */ int[] div = new int[last+1]; for (int x=2; x<=last; x++) { if (div[x] > 0) continue; for (int u=2*x; u<=last; u+=x) { div[u] = x; } } return div; } long lcm(long a, long b) { /** Least common multiple */ return a * b / gcd(a,b); } class BaseConverter { /* Palauttaa luvun esityksen kannassa base */ public String convert(Long number, int base) { return Long.toString(number, base); } /* Palauttaa luvun esityksen kannassa baseTo, kun annetaan luku StringinΓ€ kannassa baseFrom */ public String convert(String number, int baseFrom, int baseTo) { return Long.toString(Long.parseLong(number, baseFrom), baseTo); } /* Tulkitsee kannassa base esitetyn luvun longiksi (kannassa 10) */ public long longify(String number, int baseFrom) { return Long.parseLong(number, baseFrom); } } class BinomialCoefficients { /** Total number of K sized unique combinations from pool of size N (unordered) N! / ( K! (N - K)! ) */ /** For simple queries where output fits in long. */ public long biCo(long n, long k) { long r = 1; if (k > n) return 0; for (long d = 1; d <= k; d++) { r *= n--; r /= d; } return r; } /** For multiple queries with same n, different k. */ public long[] precalcBinomialCoefficientsK(int n, int maxK) { long v[] = new long[maxK+1]; v[0] = 1; // nC0 == 1 for (int i=1; i<=n; i++) { for (int j=Math.min(i,maxK); j>0; j--) { v[j] = v[j] + v[j-1]; // Pascal's triangle } } return v; } /** When output needs % MOD. */ public long[] precalcBinomialCoefficientsK(int n, int k, long M) { long v[] = new long[k+1]; v[0] = 1; // nC0 == 1 for (int i=1; i<=n; i++) { for (int j=Math.min(i,k); j>0; j--) { v[j] = v[j] + v[j-1]; // Pascal's triangle v[j] %= M; } } return v; } } int invertNumber(int a, int k) { // Inverts the binary representation of a, using only k last bits e.g. 01101 -> 10010 int inv32k = ~a; int mask = 1; for (int i = 1; i < k; ++i) mask |= mask << 1; return inv32k & mask; } /**************************** Strings ****************************/ class Zalgo { public int pisinEsiintyma(String haku, String kohde) { char[] s = new char[haku.length() + 1 + kohde.length()]; for (int i=0; i<haku.length(); i++) { s[i] = haku.charAt(i); } int j = haku.length(); s[j++] = '#'; for (int i=0; i<kohde.length(); i++) { s[j++] = kohde.charAt(i); } int[] z = toZarray(s); int max = 0; for (int i=haku.length(); i<z.length; i++) { max = Math.max(max, z[i]); } return max; } public int[] toZarray(char[] s) { int n = s.length; int[] z = new int[n]; int a = 0, b = 0; for (int i = 1; i < n; i++) { if (i > b) { for (int j = i; j < n && s[j - i] == s[j]; j++) z[i]++; } else { z[i] = z[i - a]; if (i + z[i - a] > b) { for (int j = b + 1; j < n && s[j - i] == s[j]; j++) z[i]++; a = i; b = i + z[i] - 1; } } } return z; } public List<Integer> getStartIndexesWhereWordIsFound(String haku, String kohde) { // this is alternative use case char[] s = new char[haku.length() + 1 + kohde.length()]; for (int i=0; i<haku.length(); i++) { s[i] = haku.charAt(i); } int j = haku.length(); s[j++] = '#'; for (int i=0; i<kohde.length(); i++) { s[j++] = kohde.charAt(i); } int[] z = toZarray(s); List<Integer> indexes = new ArrayList<>(); for (int i=haku.length(); i<z.length; i++) { if (z[i] < haku.length()) continue; indexes.add(i); } return indexes; } } class StringHasher { class HashedString { long[] hashes; long[] modifiers; public HashedString(long[] hashes, long[] modifiers) { this.hashes = hashes; this.modifiers = modifiers; } } long P; long M; public StringHasher() { initializePandM(); } HashedString hashString(String s) { int n = s.length(); long[] hashes = new long[n]; long[] modifiers = new long[n]; hashes[0] = s.charAt(0); modifiers[0] = 1; for (int i=1; i<n; i++) { hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M; modifiers[i] = (modifiers[i-1] * P) % M; } return new HashedString(hashes, modifiers); } /** * Indices are inclusive. */ long getHash(HashedString hashedString, int startIndex, int endIndex) { long[] hashes = hashedString.hashes; long[] modifiers = hashedString.modifiers; long result = hashes[endIndex]; if (startIndex > 0) result -= (hashes[startIndex-1] * modifiers[endIndex-startIndex+1]) % M; if (result < 0) result += M; return result; } // Less interesting methods below /** * Efficient for 2 input parameter strings in particular. */ HashedString[] hashString(String first, String second) { HashedString[] array = new HashedString[2]; int n = first.length(); long[] modifiers = new long[n]; modifiers[0] = 1; long[] firstHashes = new long[n]; firstHashes[0] = first.charAt(0); array[0] = new HashedString(firstHashes, modifiers); long[] secondHashes = new long[n]; secondHashes[0] = second.charAt(0); array[1] = new HashedString(secondHashes, modifiers); for (int i=1; i<n; i++) { modifiers[i] = (modifiers[i-1] * P) % M; firstHashes[i] = (firstHashes[i-1] * P + first.charAt(i)) % M; secondHashes[i] = (secondHashes[i-1] * P + second.charAt(i)) % M; } return array; } /** * Efficient for 3+ strings * More efficient than multiple hashString calls IF strings are same length. */ HashedString[] hashString(String... strings) { HashedString[] array = new HashedString[strings.length]; int n = strings[0].length(); long[] modifiers = new long[n]; modifiers[0] = 1; for (int j=0; j<strings.length; j++) { // if all strings are not same length, defer work to another method if (strings[j].length() != n) { for (int i=0; i<n; i++) { array[i] = hashString(strings[i]); } return array; } // otherwise initialize stuff long[] hashes = new long[n]; hashes[0] = strings[j].charAt(0); array[j] = new HashedString(hashes, modifiers); } for (int i=1; i<n; i++) { modifiers[i] = (modifiers[i-1] * P) % M; for (int j=0; j<strings.length; j++) { String s = strings[j]; long[] hashes = array[j].hashes; hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M; } } return array; } void initializePandM() { ArrayList<Long> modOptions = new ArrayList<>(20); modOptions.add(353873237L); modOptions.add(353875897L); modOptions.add(353878703L); modOptions.add(353882671L); modOptions.add(353885303L); modOptions.add(353888377L); modOptions.add(353893457L); P = modOptions.get(new Random().nextInt(modOptions.size())); modOptions.clear(); modOptions.add(452940277L); modOptions.add(452947687L); modOptions.add(464478431L); modOptions.add(468098221L); modOptions.add(470374601L); modOptions.add(472879717L); modOptions.add(472881973L); M = modOptions.get(new Random().nextInt(modOptions.size())); } } int editDistance(String a, String b) { a = "#"+a; b = "#"+b; int n = a.length(); int m = b.length(); int[][] dp = new int[n+1][m+1]; for (int y=0; y<=n; y++) { for (int x=0; x<=m; x++) { if (y == 0) dp[y][x] = x; else if (x == 0) dp[y][x] = y; else { int e1 = dp[y-1][x] + 1; int e2 = dp[y][x-1] + 1; int e3 = dp[y-1][x-1] + (a.charAt(y-1) != b.charAt(x-1) ? 1 : 0); dp[y][x] = min(e1, e2, e3); } } } return dp[n][m]; } /*************************** Technical ***************************/ private class IO extends PrintWriter { private InputStreamReader r; private static final int BUFSIZE = 1 << 15; private char[] buf; private int bufc; private int bufi; private StringBuilder sb; public IO() { super(new BufferedOutputStream(System.out)); r = new InputStreamReader(System.in); buf = new char[BUFSIZE]; bufc = 0; bufi = 0; sb = new StringBuilder(); } /** Print, flush, return nextInt. */ private int queryInt(String s) { io.println(s); io.flush(); return nextInt(); } /** Print, flush, return nextLong. */ private long queryLong(String s) { io.println(s); io.flush(); return nextLong(); } /** Print, flush, return next word. */ private String queryNext(String s) { io.println(s); io.flush(); return next(); } private void fillBuf() throws IOException { bufi = 0; bufc = 0; while(bufc == 0) { bufc = r.read(buf, 0, BUFSIZE); if(bufc == -1) { bufc = 0; return; } } } private boolean pumpBuf() throws IOException { if(bufi == bufc) { fillBuf(); } return bufc != 0; } private boolean isDelimiter(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'; } private void eatDelimiters() throws IOException { while(true) { if(bufi == bufc) { fillBuf(); if(bufc == 0) throw new RuntimeException("IO: Out of input."); } if(!isDelimiter(buf[bufi])) break; ++bufi; } } public String next() { try { sb.setLength(0); eatDelimiters(); int start = bufi; while(true) { if(bufi == bufc) { sb.append(buf, start, bufi - start); fillBuf(); start = 0; if(bufc == 0) break; } if(isDelimiter(buf[bufi])) break; ++bufi; } sb.append(buf, start, bufi - start); return sb.toString(); } catch(IOException e) { throw new RuntimeException("IO.next: Caught IOException."); } } public int nextInt() { try { int ret = 0; eatDelimiters(); boolean positive = true; if(buf[bufi] == '-') { ++bufi; if(!pumpBuf()) throw new RuntimeException("IO.nextInt: Invalid int."); positive = false; } boolean first = true; while(true) { if(!pumpBuf()) break; if(isDelimiter(buf[bufi])) { if(first) throw new RuntimeException("IO.nextInt: Invalid int."); break; } first = false; if(buf[bufi] >= '0' && buf[bufi] <= '9') { if(ret < -214748364) throw new RuntimeException("IO.nextInt: Invalid int."); ret *= 10; ret -= (int)(buf[bufi] - '0'); if(ret > 0) throw new RuntimeException("IO.nextInt: Invalid int."); } else { throw new RuntimeException("IO.nextInt: Invalid int."); } ++bufi; } if(positive) { if(ret == -2147483648) throw new RuntimeException("IO.nextInt: Invalid int."); ret = -ret; } return ret; } catch(IOException e) { throw new RuntimeException("IO.nextInt: Caught IOException."); } } public long nextLong() { try { long ret = 0; eatDelimiters(); boolean positive = true; if(buf[bufi] == '-') { ++bufi; if(!pumpBuf()) throw new RuntimeException("IO.nextLong: Invalid long."); positive = false; } boolean first = true; while(true) { if(!pumpBuf()) break; if(isDelimiter(buf[bufi])) { if(first) throw new RuntimeException("IO.nextLong: Invalid long."); break; } first = false; if(buf[bufi] >= '0' && buf[bufi] <= '9') { if(ret < -922337203685477580L) throw new RuntimeException("IO.nextLong: Invalid long."); ret *= 10; ret -= (long)(buf[bufi] - '0'); if(ret > 0) throw new RuntimeException("IO.nextLong: Invalid long."); } else { throw new RuntimeException("IO.nextLong: Invalid long."); } ++bufi; } if(positive) { if(ret == -9223372036854775808L) throw new RuntimeException("IO.nextLong: Invalid long."); ret = -ret; } return ret; } catch(IOException e) { throw new RuntimeException("IO.nextLong: Caught IOException."); } } public double nextDouble() { return Double.parseDouble(next()); } } void print(Object output) { io.println(output); } void done(Object output) { print(output); done(); } void done() { io.close(); throw new RuntimeException("Clean exit"); } long min(long... v) { long ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.min(ans, v[i]); } return ans; } double min(double... v) { double ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.min(ans, v[i]); } return ans; } int min(int... v) { int ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.min(ans, v[i]); } return ans; } long max(long... v) { long ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.max(ans, v[i]); } return ans; } double max(double... v) { double ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.max(ans, v[i]); } return ans; } int max(int... v) { int ans = v[0]; for (int i=1; i<v.length; i++) { ans = Math.max(ans, v[i]); } return ans; } } }
JAVA
1062_C. Banh-mi
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.
2
9
// Author @ BlackRise :) // // Birla Institute of Technology, Mesra// import java.io.*; import java.util.*; public class Banh_mi { static void Blackrise() { //The name Blackrise is my pen name... you can change the name according to your wish int n=ni(); int q=ni(); tsc(); //calculates the starting time of execution char ch[]= ns().toCharArray(); int ar[]=new int[n+1]; ar[1]=ch[0]=='1'?1:0; for(int i=2;i<=n;i++)ar[i]=ar[i-1]+(ch[i-1]=='1'?1:0); long twoKaPower[]=new long[n+1]; twoKaPower[0]=1; for(int i=1;i<=n;i++)twoKaPower[i]=(2*twoKaPower[i-1])%mod9; while(q-->0) { int l=ni(); int r=ni(); int size=r-l+1; int one=ar[r]-ar[l-1]; int zero=size-one; long ans=0; ans+=twoKaPower[one] - 1; ans+=(((twoKaPower[one]-1)*(twoKaPower[zero]-1))%mod9); ans%=mod9; if(ans<0)ans+=mod9; pl(ans); } tec(); //calculates the ending time of execution //pwt(); //prints the time taken to execute the program } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static Calendar ts, te; //For time calculation static int mod9 = (int) 1e9 + 7; static Lelo input = new Lelo(System.in); static PrintWriter pw = new PrintWriter(System.out, true); public static void main(String[] args) { //threading has been used to increase the stack size. new Thread(null, null, "BlackRise", 1<<25) //the last parameter is stack size which is desired, { public void run() { try { Blackrise(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static class Lelo { //Lelo class for fast input private InputStream ayega; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public Lelo(InputStream ayega) { this.ayega = ayega; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = ayega.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // functions to take input// static int ni() { return input.nextInt(); } static long nl() { return input.nextLong(); } static double nd() { return input.nextDouble(); } static String ns() { return input.readString(); } //functions to give output static void pl() { pw.println(); } static void p(Object o) { pw.print(o + " "); } static void pws(Object o){ pw.print(o+""); } static void pl(Object o) { pw.println(o); } static void tsc() //calculates the starting time of execution { ts = Calendar.getInstance(); ts.setTime(new Date()); } static void tec() //calculates the ending time of execution { te = Calendar.getInstance(); te.setTime(new Date()); } static void pwt() //prints the time taken for execution { pw.printf("\nExecution time was :- %f s\n", (te.getTimeInMillis() - ts.getTimeInMillis()) / 1000.00); } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Washoum */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; inputClass in = new inputClass(inputStream); PrintWriter out = new PrintWriter(outputStream); CBanhMi solver = new CBanhMi(); solver.solve(1, in, out); out.close(); } static class CBanhMi { static final int MOD = (int) 1e9 + 7; public void solve(int testNumber, inputClass sc, PrintWriter out) { int n = sc.nextInt(); int q = sc.nextInt(); String s = sc.nextLine(); int[] tab = new int[n + 1]; int cnt = 0; for (int i = 1; i <= n; i++) { if (s.charAt(i - 1) == '1') { cnt++; } tab[i] = cnt; } int l, r; int nb1, nb0; long ans; long[] pow = new long[n + 1]; pow[0] = 1; for (int i = 1; i < n + 1; i++) { pow[i] = (pow[i - 1] * 2) % MOD; } for (int i = 0; i < q; i++) { l = sc.nextInt(); r = sc.nextInt(); nb1 = tab[r] - tab[l - 1]; nb0 = r - l + 1 - nb1; ans = pow[r - l + 1] - pow[nb0]; if (ans < 0) { ans += MOD; } out.println(ans); } } } static class inputClass { BufferedReader br; StringTokenizer st; public inputClass(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1062_C. Banh-mi
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.
2
9
import sys import math input=sys.stdin.readline def power(x, y, p) : res = 1 # Initialize result # Update x if it is more # than or equal to p x = x % p while (y > 0) : # If y is odd, multiply # x with result if ((y & 1) == 1) : res = (res * x) % p # y must be even now y = y >> 1 # y = y/2 x = (x * x) % p return res modi=1000000007 ones=[0]*(100001) for i in range(1,100001): ones[i]=(2*ones[i-1]+1)%modi zeroes=[1]*(100001) for i in range(1,100001): zeroes[i]=(2*zeroes[i-1])%modi n,q=map(int,input().split()) s=input() cones=[0]*(n+1) for i in range(1,n+1): if(s[i-1]=='1'): cones[i]=cones[i-1]+1 else: cones[i]=cones[i-1] for i in range(q): l,r=map(int,input().split()) curr=cones[r]-cones[l-1] ans=(ones[curr])%modi ans=((ans%modi)*(zeroes[r-l+1-curr]%modi))%modi print(ans)
PYTHON3
1062_C. Banh-mi
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.
2
9
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; /** * * @author is2ac */ public class C_CF { public static void main(String[] args) { FastScanner57 fs = new FastScanner57(); PrintWriter pw = new PrintWriter(System.out); //int t = fs.ni(); int t = 1; for (int tc = 0; tc < t; tc++) { int n = fs.ni(), q = fs.ni(); long mod = (long)(1e9+7); long[] dp = new long[n+1]; long[] b = new long[n+1]; dp[0] = 1L; b[0] = 1L; long p = 1; for (int i = 1; i < n+1; i++) { p += dp[i-1]; p %= mod; dp[i] = p; b[i] = p; } for (int i = 1; i < n+1; i++) { b[i] += b[i-1]; b[i] %= mod; } String s = fs.next(); long[] a = new long[n]; for (int i= 0; i < s.length(); i++) { a[i] += s.charAt(i)-'0'; } for (int i = 1; i < n; i++) { a[i] += a[i-1]; } for (int i = 0; i < q; i++) { int l = fs.ni()-1, r = fs.ni()-1; long o = a[r]; if (l>0) o -= a[l-1]; long z = r-l+1 - o; if (o==0) { pw.println(0); continue; } long res = b[(int)o-1]; if (z>0) { res *= b[(int)z-1]; } else { res = 0; } res %= mod; res += b[(int)o-1]; res %= mod; pw.println(res); } //pw.println(Arrays.toString(dp)); //pw.println(Arrays.toString(b)); } pw.close(); } public static String print(int[] b) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < b.length; i++) { sb.append(b[i]); } return sb.toString(); } public static int recur(int ind, int p, int[] a, Integer[][] dp) { if (ind == a.length) { return 0; } int n = (int) (1e9); if (dp[ind][p] != null) { return dp[ind][p]; } for (int i = 0; i < 3; i++) { int c = 1; if (i == a[ind]) { c--; } if (i == p) { continue; } n = Math.min(n, recur(ind + 1, i, a, dp) + c); } return dp[ind][p] = n; } public static void sort(long[] a) { List<Long> list = new ArrayList(); for (int i = 0; i < a.length; i++) { list.add(a[i]); } Collections.sort(list); for (int i = 0; i < a.length; i++) { a[i] = list.get(i); } } public static long gcd(long n1, long n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } } class UnionFind16 { int[] id; public UnionFind16(int size) { id = new int[size]; for (int i = 0; i < size; i++) { id[i] = i; } } public int find(int p) { int root = p; while (root != id[root]) { root = id[root]; } while (p != root) { int next = id[p]; id[p] = root; p = next; } return root; } public void union(int p, int q) { int a = find(p), b = find(q); if (a == b) { return; } id[b] = a; } } class FastScanner57 { BufferedReader br; StringTokenizer st; public FastScanner57() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) { ret[i] = ni(); } return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) { ret[i] = nl(); } return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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'; } }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
from sys import stdin input = stdin.readline def power(a, b, mod): res = 1 while b: if b%2: res = (res*a)%mod b //= 2 a = (a*a)%mod return res%mod n, q = map(int, input().split()) s = list(map(int, input().strip())) MOD = 10**9+7 zeroes = [0]*(n+1) ones = [0]*(n+1) for i in range(1, 1+n): zeroes[i] = zeroes[i-1] ones[i] = ones[i-1] if s[i-1]: ones[i] += 1 else: zeroes[i] += 1 for i in range(q): l, r = map(int, input().split()) one = (power(2, ones[r]-ones[l]+(s[l-1]==1), MOD)-1)%MOD two = (power(2, zeroes[r]-zeroes[l]+(s[l-1]==0), MOD)-1)%MOD res = (one*two)%MOD res = (res+one)%MOD print(res)
PYTHON3
1062_C. Banh-mi
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.
2
9
#Wolve from sys import * m = 1000000007 n, q = map(int, stdin.readline().split()) a = stdin.readline() ans = [] t = [] count = 0 for i in a: if i == '1': count+=1 t.append(count) for _ in range(q): x,y=map(int,input().split()) if(x==1): p=t[y-1] else: p=t[y-1]-t[x-2] q=(y-x+1-p) s=pow(2,p+q,m)%m s=(((s%m)-(pow(2,q,m)%m))%m) ans.append(s) stdout.write('\n'.join(map(str, ans)))
PYTHON3
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.sqrt; @SuppressWarnings("unchecked") public class P1062C { final static BigInteger TWO = BigInteger.valueOf(2); final static BigInteger MOD = BigInteger.valueOf(1_000_000_007); BigInteger modSum(BigInteger b, BigInteger e) { return b.multiply(TWO.modPow(e, MOD)).subtract(b); } public void run() throws Exception { int n = nextInt() + 1, q = nextInt(), s [] = new int [n]; String str = next(); for (int i = 1; i < n; s[i] = s[i - 1] + (str.charAt(i - 1) - '0'), i++); while ((q--) > 0) { int l = nextInt() - 1, r = nextInt(), oc = s[r] - s[l], zc = r - l - oc; BigInteger u1 = modSum(BigInteger.ONE, BigInteger.valueOf(oc)); BigInteger u0 = modSum(u1, BigInteger.valueOf(zc)); println(u1.add(u0).mod(MOD)); } } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P1062C().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); } else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } long gcd(long a, long b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); } else { b -= a; b >>>= Long.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } void shuffle(int [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); int t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(long [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); long t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(Object [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); Object t = a[i]; a[i] = a[j]; a[j] = t; } } void flush() { pw.flush(); } void pause() { flush(); System.console().readLine(); } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
import math import sys from collections import defaultdict,Counter,deque,OrderedDict import bisect #sys.setrecursionlimit(1000000) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] #def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] #INF = 10 ** 18 #MOD = 1000000000 + 7 from itertools import accumulate,groupby import sys M = 1000000000 + 7 def fast_exp(bas, exp): t = 1; while(exp > 0): if (exp % 2 != 0): t = (t * bas) % M; bas = (bas * bas) % M; exp = exp // 2; return t % M; def fun(x,y): a= fast_exp(2,y) - 1 b = fast_exp(2,x) return (a*b)%M n,q = ilele() S = input() pre = [(0,0)] zeroes =0;ones = 0 for i in S: if i=="1": ones+=1 else: zeroes+=1 pre.append((zeroes,ones)) #print(pre) for i in range(q): l,r = ilele() x = pre[l-1] y = pre[r] z = y[0] - x[0] o = y[1] - x[1] #print(z,o) print(fun(z,o))
PYTHON3
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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"; } }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Set; import java.util.StringTokenizer; public class q5 { static int[] visited; public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } // Credits TO GFG static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Reader.init(System.in); int n=Reader.nextInt(); int q=Reader.nextInt(); String s=Reader.next(); int[] arr=new int[n+1]; long mod=1000000007; for(int i=0;i<n;i++) { if(s.charAt(i)=='1') { arr[i+1]=arr[i]+1; } else { arr[i+1]=arr[i]; } } for(int i=0;i<q;i++) { int l=Reader.nextInt(); int r=Reader.nextInt(); int length=r-l+1; int no=arr[r]-arr[l-1]; int nz=length-no; long val=q5.power(2, no, mod)-1; long tv=q5.power(2, nz, mod)-1; long ans=(val*tv)%mod+val; ans%=mod; System.out.println(ans); } } } class Node2{ int edge1; int edge2; long weight; int number; Node2(int a,int b,long c,int d){ edge1=a;edge2=b; weight=c;number=d; } } class DisJoint { int[] parent; int[] rank; int[] size; DisJoint(int n){ parent=new int[n+1]; rank=new int[n+1]; size=new int[n+1]; for(int i=0;i<=n;i++) { parent[i]=i; size[i]=1; } } int find(int value) { int par=parent[value]; if(par==value) return par; parent[value]=find(par); return parent[value]; } void union(int data1,int data2) { int parent1=find(data1); int parent2=find(data2); if(parent1!=parent2) { if(rank[parent1]>=rank[parent2]) { parent[parent2]=parent1; if(rank[parent1]==rank[parent2]) rank[parent1]++; size[parent1]+=size[parent2]; } else { parent[parent1]=parent2; size[parent2]+=size[parent1]; } } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
JAVA
1062_C. Banh-mi
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.
2
9
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split() n = int(n) q = int(q) s = r[1] p = [0] k = 0 for v in range(n): d = int(s[v]) k += (1 - d) p.append(k) for k in range(q): a, b = r[k + 2].strip().split() a = int(a) b = int(b) l = b - a + 1 zero = p[b] - p[a - 1] sys.stdout.write(str((pow(2, l, M) - pow(2, zero, M) + M) % M)) sys.stdout.write("\n")
PYTHON3
1062_C. Banh-mi
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.
2
9
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; /* abhi2601 */ public class Practice implements Runnable{ final static long mod = (long)1e9 + 7; public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=sc.nextInt(); int q=sc.nextInt(); String s=sc.next(); long dp[]=new long[n+1]; long two[]=new long[n+1]; two[0]=1; for(int i=1;i<=n;i++){ if(s.charAt(i-1)=='1') dp[i]=dp[i-1]+1; else dp[i]=dp[i-1]; two[i]=(two[i-1]*2)%mod; } for(int i=0;i<q;i++){ int l=sc.nextInt(); int r=sc.nextInt(); long temp1=dp[r]-dp[l-1]; long temp0=r-l+1-temp1; long ans=(two[(int)temp1]-1+mod)%mod; ans=(ans+ans*(two[(int)temp0]-1))%mod; w.println(ans); } w.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Practice(),"cf3",1<<26).start(); } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int q = scan.nextInt(); String S = scan.next(); int[] ones = new int[n+1]; int[] zeros = new int[n+1]; int numOfOnes = 0; int numOFZeros = 0; for (int i = 0 ; i < n ; i++) { if (S.charAt(i) == '1') { numOfOnes++; ones[i+1] = numOfOnes; zeros[i+1] = numOFZeros; } else { numOFZeros++; zeros[i+1] = numOFZeros; ones[i+1] = numOfOnes; } } long[] pow = new long[100001]; pow[0] = 1; for (int i = 1 ; i < 100001 ; i++) { pow[i] = (pow[i-1] * 2) % 1000000007; } for (int i = 0 ; i < q ; i++) { int l = scan.nextInt(); int r = scan.nextInt(); int numOfOne = ones[r] - ones[l-1]; int numOfZero = zeros[r] - zeros[l-1]; long first = pow[numOfOne]-1; // System.out.println(first); long second = pow[numOfOne + numOfZero]-1; // System.out.println(second); long third = pow[numOfOne]-1; long fourth = pow[numOfZero]-1; // System.out.println(fourth); // System.out.println(third); if (numOfZero > 0) { third = (third + fourth + 1000000007) % 1000000007; second -= third; first = (first + second + 1000000007) % 1000000007; System.out.println(first % 1000000007); } else { System.out.println(first % 1000000007); } } } static long power(long x, long y, long p) { long res = 1; // Initialize result // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply // x with the result if ((y & 1) > 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } }
JAVA
1062_C. Banh-mi
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.
2
9
mod = 1000000007 n, q = list(map(int,input().split())) arr = list(map(int,list(input()))) s = 0 preSum = [] for i in range(n): s+=arr[i] preSum.append(s) ans = [] for i in range(q): l,r = list(map(int,input().split())) am = r-l+1 s = preSum[r-1] - preSum[l-1] + arr[l-1] perfect = pow(2,am,mod) minus = pow(2,am-s,mod) ans += [((perfect-minus)+mod)%mod] print(*ans,sep="\n")
PYTHON3
1062_C. Banh-mi
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.
2
9
#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; } }
CPP
1062_C. Banh-mi
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.
2
9
import sys def numIN(): return(map(int,sys.stdin.readline().strip().split())) MOD = 10**9+7 n,q = numIN() l = [int(i) for i in input()] pre = [] s = 0 for i in l: s+=i pre.append(s) for i in range(q): l,r = numIN() l-=1 r-=1 if l!=0: one = pre[r]-pre[l-1] zero = r-l+1-one x = pow(2,one,MOD)-1 y = pow(2,zero,MOD) ans = x*y ans%=MOD print(ans) else: one = pre[r] zero = r-l+1-one x = pow(2,one,MOD)-1 y = pow(2,zero,MOD) ans = x*y ans%=MOD print(ans)
PYTHON3
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class temp4 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } */ static class Print { private final BufferedWriter bw; public Print() { bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str)throws IOException { bw.append(str); } public void println(String str)throws IOException { print(str); bw.append("\n"); } public void close()throws IOException { bw.close(); }} public static void main(String[] args) throws IOException { FastReader scn=new FastReader(); // Print pr=new Print(); PrintWriter out=new PrintWriter(System.out); int n=scn.nextInt(),q=scn.nextInt(),l=0,r=0,mod=1000000007; String s=scn.next(); long[] arr=new long[n+1]; long[] pow=new long[n+1]; pow[0]=1; for(int i=1;i<=n;i++){ arr[i]=arr[i-1]+s.charAt(i-1)-'0'; pow[i]=(pow[i-1]*2)%mod; } for(int i=0;i<q;i++){ l=scn.nextInt();r=scn.nextInt(); long n1=arr[r]-arr[l-1]; long n0=(r-l+1)-n1; long ans=(pow[(int) (n0+n1)]-pow[(int) n0]+mod)%mod; out.println(ans); } out.close(); } public static class node{ int val; int idx; public node(int val,int idx){ this.val=val;this.idx=idx; } } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.math.BigInteger; import java.util.StringTokenizer; public class Banhi { static long modPow(int a, int e, int mod) { a %= mod; long res = 1; while(e > 0) { if((e & 1) == 1) res =(1l*res * a) % mod; a = (int)((1l*a * a) % mod); e >>= 1; } return res; } public static void main(String[]args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int q = sc.nextInt(); zz []l= new zz[n]; String s= sc.nextLine(); int o=0; int z =0; for (int i =0;i<n;i++){ if (s.charAt(i)=='1') o++; else z++; l[i]=new zz(o,z); } while (q-->0){ int a = sc.nextInt()-2; int b = sc.nextInt()-1; long ans =0; if (a<0) ans=(modPow(2,l[b].o,1000000007)-1)*modPow(2,l[b].z,1000000007); else ans=(modPow(2,l[b].o-l[a].o,1000000007)-1)*modPow(2,l[b].z-l[a].z,1000000007); out.println(ans<0?0:ans%1000000007); } out.flush(); } static class zz{ int o ; int z; zz(int o ,int z){ this.o=o; this.z=z; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
1062_C. Banh-mi
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.
2
9
from sys import stdin, stdout MOD = pow(10, 9) + 7 n, q = map(int, raw_input().strip().split()) s = list(raw_input().strip()) pre = [0 for i in xrange(n + 1)] for i in xrange(1, n + 1): pre[i] = pre[i - 1] + int(s[i - 1] == '1') inp = stdin.readlines() out = [] for line in inp: l, r = map(int, line.strip().split()) b = pre[r] - pre[l - 1] a = (r - l + 1) - b ans = (pow(2, a, MOD) * (pow(2, b, MOD) - 1)) % MOD out.append(str(ans)) stdout.write("\n".join(out))
PYTHON
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scn=new Scanner(System.in); BufferedWriter output = new BufferedWriter( new OutputStreamWriter(System.out)); int p=1000000000+7; int[] arr=new int[100001]; arr[0]=1; for(int i=1;i<=100000;i++){ arr[i]=arr[i-1]*2; arr[i]=arr[i]%p; } int n=scn.nextInt(); int q=scn.nextInt(); String s=scn.next(); int[] arr1=new int[n+1]; int[] arr0=new int[n+1]; for(int i=1;i<=n;i++){ if(s.charAt(i-1)=='0'){ arr1[i]=arr1[i-1]; arr0[i]=arr0[i-1]+1; } else{ arr1[i]=arr1[i-1]+1; arr0[i]=arr0[i-1]; } } for(int i=0;i<q;i++){ int l=scn.nextInt(); int r=scn.nextInt(); int a=arr1[r]-arr1[l-1]; int b=arr0[r]-arr0[l-1]; long ans=(arr[a]-1)*(long)(arr[b]); ans=ans%p; output.write((int)(ans)+"\n"); } output.close(); } }
JAVA
1062_C. Banh-mi
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.
2
9
import sys r = sys.stdin.readlines() M = 10 ** 9 + 7 n, q = r[0].strip().split(' ') n = int(n) q = int(q) s = r[1] p = [0] for v in range(n): p.append(p[v] + int(s[v])) ans = [] for k in range(q): a, b = r[k + 2].strip().split(' ') a = int(a) b = int(b) l = b - a + 1 one = p[b] - p[a - 1] ans.append(str((pow(2, l, M) - pow(2, l - one, M) + M) % M)) sys.stdout.write("\n".join(ans))
PYTHON3
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { long mod = (long) 1e9 + 7; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.i(); int q = in.i(); char[] s = in.s().toCharArray(); int[] zero = new int[n + 1]; int[] one = new int[n + 1]; for (int i = 0; i < s.length; i++) { if (s[i] == '0') { zero[i + 1] = zero[i] + 1; one[i + 1] = one[i]; } else { one[i + 1] = one[i] + 1; zero[i + 1] = zero[i]; } } while (q-- > 0) { int l = in.i(); int r = in.i(); int no_zero = zero[r] - zero[l - 1]; int no_one = one[r] - one[l - 1]; long ans = IntegerUtil.modPow(2, no_zero, mod); ans = ans % mod * (IntegerUtil.modPow(2, no_one, mod) - 1 + mod) % mod; out.println(ans % mod); } } } static class IntegerUtil { public static long modPow(long base, long exp, long mod) { long res = 1L; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % mod; base = (base * base) % mod; exp >>= 1; } return res; } } static class InputReader { InputStream is; private byte[] inbuf = new byte[1024]; public int lenbuf = 0; public int ptrbuf = 0; public InputReader(InputStream is) { this.is = is; } private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String s() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int i() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CBanhMi solver = new CBanhMi(); solver.solve(1, in, out); out.close(); } static class CBanhMi { long mod = (int) 1e9 + 7; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int q = in.nextInt(); char[] smh = in.next().toCharArray(); int[] one = new int[n + 1]; for (int i = 1; i <= n; i++) { one[i] = one[i - 1] + ((smh[i - 1] == '1') ? 1 : 0); } for (int i = 0; i < q; i++) { int l = in.nextInt(); int r = in.nextInt(); int oo = one[r] - one[l - 1]; // out.println((pow(oo) - 1)); // out.println(pow(r - l - oo + 1)); long ans = ((pow(oo) - 1) * pow(r - l - oo + 1)) % mod; out.println(ans); } } long pow(int a) { if (a == 0) { return 1; } long z = pow(a / 2); if ((a & 1) == 0) { return (z * z) % mod; } else return ((z * z) % mod * 2) % mod; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
1062_C. Banh-mi
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.
2
9
#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; }
CPP
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> long long i, j, k, n, m, x, z, q, a[100009], pw[100009]; using namespace std; int main() { std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); string s; cin >> x >> q >> s; for (i = 0; i < x; i++) { if (s[i] == '0') a[i + 1] = a[i] + 1; else a[i + 1] = a[i]; } pw[0] = 1; for (i = 1; i < 100001; i++) pw[i] = (pw[i - 1] * 2) % 1000000007; while (q--) { cin >> n >> m; x = a[m] - a[n - 1]; cout << (pw[m - n + 1] - pw[x] + 1000000007) % 1000000007 << endl; } return 0; }
CPP
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; const int MAX = 1e5 + 10; const long long BIG = 1e11 + 10; const double eps = 1e-6; const double PI = 3.14159; const long long mod = 1000000007; int n, q; string s; int one[MAX], zero[MAX]; long long mypow(long long a, long long n, long long MOD) { long long res = 1; while (n) { if (n % 2 == 1) { res *= a; res = res % MOD; n--; } n = n / 2; a = (a * a) % MOD; } return res; } int main() { while (cin >> n >> q) { cin >> s; int len = s.size(); memset(one, 0, sizeof(one)); memset(zero, 0, sizeof(zero)); for (int i = 0; i < len; i++) { one[i + 1] = one[i]; zero[i + 1] = zero[i]; if (s[i] == '0') zero[i + 1]++; else one[i + 1]++; ; } int l, r; long long a, b; while (q--) { scanf("%d%d", &l, &r); a = one[r]; b = zero[r]; a -= one[l - 1]; b -= zero[l - 1]; long long ans = mypow(2, a, mod) - 1; ans = (ans * mypow(2, b, mod)) % mod; cout << ans << endl; } } return 0; }
CPP
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; const long long mod = 1e9 + 7; int cnt[maxn][2]; long long powMod(long long a, long long b) { long long sum = 1; a %= mod; while (b > 0) { if (b % 2 == 1) sum = (sum * a) % mod; b /= 2; a = (a * a) % mod; } return sum; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; int c0 = 0, c1 = 0; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '0') c0++; else c1++; cnt[i][0] = c0; cnt[i][1] = c1; } int l, r; while (m--) { cin >> l >> r; l--, r--; c0 = cnt[r][0] - cnt[l][0] + (s[l] == '0'); c1 = cnt[r][1] - cnt[l][1] + (s[l] == '1'); long long ans = powMod(2, c1) - 1; if (ans < 0) ans += mod; cout << powMod(2, c0) * ans % mod << endl; } }
CPP
1062_C. Banh-mi
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.
2
9
from sys import * m = 1000000007 n, q = map(int, stdin.readline().split()) a = stdin.readline() ans = [] t = [] count = 0 for i in a: if i == '1': count+=1 t.append(count) for _ in range(q): x,y=map(int,input().split()) if(x==1): p=t[y-1] else: p=t[y-1]-t[x-2] q=(y-x+1-p) s=pow(2,p+q,m)%m s=(((s%m)-(pow(2,q,m)%m))%m) ans.append(s) stdout.write('\n'.join(map(str, ans)))
PYTHON3
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; const long long maxn = 5e5 + 7; const long long mod = 1e9 + 7; string a; long long a1[maxn]; long long counting(long long a, long long b) { long long sum = 1; while (a) { if (a & 1) sum = (b * sum) % mod; b = (b * b) % mod; a >>= 1; } return sum % mod; } int main() { long long n, q; while (~scanf("%lld%lld", &n, &q)) { cin >> a; for (long long b = 1; b <= a.size(); b++) a1[b] += a1[b - 1] + a[b - 1] - '0'; while (q--) { long long l, r; cin >> l >> r; long long num = 0; long long num1 = a1[r] - a1[l - 1]; num = (num + counting(num1, 2) - 1) % mod; num = (num * counting((r - l + 1) - num1, 2)) % mod; printf("%lld\n", num % mod); } } return 0; }
CPP
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; using ll = long long; void dout() { cerr << endl; } template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; string s; cin >> s; vector<int> pw(n + 1); vector<int> pws(n + 1); pw[0] = 1; pws[0] = 1; for (int i = 1; i <= n; i++) { pw[i] = (pw[i - 1] * 2) % mod; pws[i] = (pw[i] + pws[i - 1]) % mod; } vector<int> psum(n + 1); for (int i = 0; i < n; i++) { psum[i + 1] = psum[i] + (s[i] - '0'); } for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; int len = r - l + 1; int c = psum[r] - psum[l - 1]; int ans = pw[c] - 1; if (c > 0) { ans += pws[len - 1]; ans %= mod; ans -= pws[c - 1]; ans += mod; ans %= mod; if (c < len) { ans -= pws[len - c - 1]; ans += mod; ans %= mod; } } cout << ans << '\n'; } return 0; }
CPP
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007ll; int n, q; char s[100100]; long long count0[100100]; long long count1[100100]; long long QPow(long long x, long long n) { long long ret = 1; long long tmp = x % MOD; while (n) { if (n & 1) { ret = (ret * tmp) % MOD; } tmp = (tmp * tmp) % MOD; n >>= 1; } return ret; } int main() { scanf("%d%d", &n, &q); scanf("%s", s); for (int i = 0; i < n; i++) { if (s[i] == '0') { count0[i + 1] = 1 + count0[i]; count1[i + 1] = count1[i]; } else { count1[i + 1] = 1 + count1[i]; count0[i + 1] = count0[i]; } } while (q--) { int l, r; scanf("%d%d", &l, &r); long long c0 = count0[r] - count0[l - 1]; long long c1 = count1[r] - count1[l - 1]; long long ans = QPow(2, c1) - 1; long long ans2 = ((QPow(2, c0) - 1) * ans) % MOD; ans = (ans + ans2) % MOD; printf("%lld\n", ans); } return 0; }
CPP
1062_C. Banh-mi
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.
2
9
#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] + MOD - 1ll) % MOD; ans = (ans * (unsigned long long)f[zeros]) % MOD; printf("%lld\n", ans); } return EXIT_SUCCESS; }
CPP
1062_C. Banh-mi
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.
2
9
from bisect import bisect_right as br from bisect import bisect_left as bl from collections import defaultdict import sys import math MAX = sys.maxsize MOD = 10**9+7 MAXN = 10**6+10 def isprime(n): n = abs(int(n)) if n < 2: return False if n == 2: return True if not n & 1: return False for x in range(3, int(n**0.5) + 1, 2): if n % x == 0: return False return True def mhd(a,b,x,y): return abs(a-x)+abs(b-y) def numIN(): return(map(int,sys.stdin.readline().strip().split())) def charIN(): return(sys.stdin.readline().strip().split()) def maxSubArraySum(a,size): max_so_far = 0 max_ending_here = 0 for i in range(size): max_ending_here = max_ending_here + a[i] if max_ending_here < 0: max_ending_here = 0 elif (max_so_far < max_ending_here): max_so_far = max_ending_here return max_so_far def isPer(n): return((math.floor(n**0.5))==(math.ceil(n**0.5))) def modInverse(a, m) : m0 = m y = 0 x = 1 if (m == 1) : return 0 while (a > 1) : # q is quotient q = a // m t = m # m is remainder now, process # same as Euclid's algo m = a % m a = t t = y # Update x and y y = x - q * y x = t # Make x positive if (x < 0) : x = x + m0 return x n,q = numIN() l = [int(i) for i in input()] pre = [] s = 0 for i in l: s+=i pre.append(s) for i in range(q): l,r = numIN() l-=1 r-=1 if l!=0: one = pre[r]-pre[l-1] zero = r-l+1-one x = pow(2,one,MOD)-1 y = pow(2,zero,MOD) ans = x*y ans%=MOD print(ans) else: one = pre[r] zero = r-l+1-one x = pow(2,one,MOD)-1 y = pow(2,zero,MOD) ans = x*y ans%=MOD print(ans)
PYTHON3
1062_C. Banh-mi
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.
2
9
def beki(a,b): waru=10**9+7 ans=1 while(b>0): if(1 & b): ans= ans * a %waru b >>= 1 a=a * a % waru return ans n,m=map(int,input().split()) s=input() ans=[] waru=10**9+7 ru=[0]*(n+1) b=[1]*(n+1) for i in range(n): ru[i+1]=ru[i]+int(s[i]) b[i+1]=(b[i]*2)%waru for i in range(m): l,r=map(int,input().split()) ko=ru[r]-ru[l-1] ans.append((((b[ko] -1)) * ((b[r+1-l - ko]))) %waru) print("\n".join(list(map(str,ans))))
PYTHON3
1062_C. Banh-mi
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.
2
9
import java.util.Arrays; import java.util.Scanner; /** * Created by Jamie on 2/14/2019. */ public class Bahnmi { public static int MODULUS = 1_000_000_007; public static void main(String[] args) { Scanner scan = new Scanner(System.in); FTree ftree = new FTree(100_000); int n = scan.nextInt(); int q = scan.nextInt(); scan.nextLine(); String temp = scan.nextLine(); int[] bahn = new int[n]; for (int i = 0; i < n; i++) { bahn[i] = temp.charAt(i) - '0'; if (bahn[i] == 1) { ftree.update(i+1, 1); } } int total1s = ftree.getSum(n); long[] partials = new long[total1s + 1]; partials[0] = 0; for (int i = 1; i < total1s + 1; i++) { long val = (partials[i-1] + 1) * 2 - 1; partials[i] = val % MODULUS; } //System.out.println(Arrays.toString(partials)); long[] pow2s = new long[n - total1s + 1]; pow2s[0] = 1; for (int i = 1; i < n-total1s + 1; i++) { pow2s[i] = (pow2s[i-1] * 2) % MODULUS; } for (int j = 0; j < q; j++) { int l = scan.nextInt(); int r = scan.nextInt(); int num1s = ftree.getRange(l, r); if (num1s < 0) { System.out.println("Overflow " + temp); System.exit(0); } int size = r-l+1; // long total = 0; long total = partials[num1s]; //Double for every 0 total *= pow2s[size-num1s]; total %= MODULUS; System.out.println(total); } } static class FTree { //1 index fenwick tree private int[] tree; private int size; public FTree(int n) { size = n+1; tree = new int[size]; } //Get range with inclusive boundaries public int getRange(int a, int b) { return getSum(b) - getSum(a-1); } public void update(int pos, int val) { while(pos < size) { tree[pos] += val; pos += Integer.lowestOneBit(pos); } } public int getSum(int pos) { int retVal = 0; while (pos >= 1) { retVal += tree[pos]; pos -= Integer.lowestOneBit(pos); } return retVal; } } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.util.*; public class Task { public static void main(String[] args) throws IOException { new Task().go(); } PrintWriter out; Reader in; BufferedReader br; Task() throws IOException { try { //br = new BufferedReader( new FileReader("input.txt") ); in = new Reader("input.txt"); out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) ); } catch (Exception e) { //br = new BufferedReader( new InputStreamReader( System.in ) ); in = new Reader(); out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) ); } } void go() throws IOException { int t = 1; while (t > 0) { solve(); //out.println(); t--; } out.flush(); out.close(); } int inf = 2000000000; int mod = 1000000007; double eps = 0.000000001; int n; int m; int[] a; ArrayList<Integer>[] g; void solve() throws IOException { int n = in.nextInt(); int m = in.nextInt(); String s = in.next(); long st = 1; long[] sum = new long[n + 1]; for (int i = 1; i < n + 1; i++) { sum[i] = (sum[i - 1] + st) % mod; st <<= 1; st %= mod; } int[] pref = new int[n + 1]; for (int i = 1; i < n + 1; i++) pref[i] += pref[i - 1] + s.charAt(i - 1) - '0'; long[] sums = new long[n + 1]; for (int i = 1; i < n + 1; i++) sums[i] = (sums[i - 1] + sum[i]) % mod; for (int i = 0; i < m; i++) { int l = in.nextInt(); int r = in.nextInt(); int cnt = pref[r] - pref[l - 1]; long ans = 0; if (cnt > 0) ans = (sums[r - l] - sums[Math.max(r - l - cnt, 0)] + mod) % mod; out.println((ans + cnt) % mod); } } class Pair implements Comparable<Pair>{ int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if (a > p.a) return 1; if (a < p.a) return -1; if (b > p.b) return 1; if (b < p.b) return -1; return 0; } } class Item { int a; int b; int c; Item(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } } class Reader { BufferedReader br; StringTokenizer tok; Reader(String file) throws IOException { br = new BufferedReader( new FileReader(file) ); } Reader() throws IOException { br = new BufferedReader( new InputStreamReader(System.in) ); } String next() throws IOException { while (tok == null || !tok.hasMoreElements()) tok = new StringTokenizer(br.readLine()); return tok.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.valueOf(next()); } long nextLong() throws NumberFormatException, IOException { return Long.valueOf(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.valueOf(next()); } String nextLine() throws IOException { return br.readLine(); } } }
JAVA
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; void smain(); signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(12); smain(); } vector<long long> pf; const long long N = 1 << 17; const long long M = 1e9 + 7; long long cnt[N]; inline long long MOD(long long v) { if (v < 0) { v %= M; return v < 0 ? v + M : v; } return v % M; } long long prog(long long l, long long r) { return (l + r) * (r - l + 1) / 2; } long long hlp[N] = {0, 1}; long long quer(long long l, long long r) { l--, r--; long long first = pf[r]; if (l) first -= pf[l - 1]; if (!first) return 0; long long ans = cnt[r - l + 1] - 1; long long v = (r - l + 1) - first; return MOD(ans - hlp[v]); } void smain() { cnt[0] = 1; for (long long i = 1; i < N; ++i) cnt[i] = (cnt[i - 1] * 2) % M; for (long long i = 2; i < N; ++i) hlp[i] = MOD(hlp[i - 1] + cnt[i - 1]); long long n, q; cin >> n >> q; string s; cin >> s; pf.resize(n); for (long long i = 0; i < n; ++i) pf[i] = s[i] - '0'; for (long long i = 1; i < n; ++i) pf[i] += pf[i - 1]; for (long long i = 0; i < q; ++i) { long long l, r; cin >> l >> r; cout << quer(l, r) << '\n'; } }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Objects; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay2516 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CBanhMi solver = new CBanhMi(); solver.solve(1, in, out); out.close(); } static class CBanhMi { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); char c[] = in.next().toCharArray(); int mod = (int) 1e9 + 7; Pair<Long, Long>[] a = new Pair[n + 1]; a[0] = new Pair<>(0L, 0L); for (int i = 1; i <= n; ++i) { if (c[i - 1] == '0') { a[i] = new Pair<>(a[i - 1].x, a[i - 1].y + 1); } else { a[i] = new Pair<>(a[i - 1].x + 1, a[i - 1].y); } } while (q-- > 0) { int l = in.nextInt(), r = in.nextInt(); long ones = a[r].x - a[l - 1].x; long zeros = a[r].y - a[l - 1].y; long po = func.power(2, ones, mod); while (po < 0) po += mod; po %= mod; long pz = func.power(2, zeros, mod); while (pz < 0) pz += mod; pz %= mod; out.println(((po - 1) % mod * pz % mod) % mod); } } } static class func { public static long power(long x, long y, int mod) { if (y == 0) return 1; long p = power(x, y / 2, mod); p = (p * p) % mod; return (y % 2 == 0) ? p : (x * p) % mod; } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) x).compareTo(o.x); if (value != 0) return value; return ((Comparable<V>) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; long long n, q, p[100005]; string s; long long expo(long long base, long long exponent, long long mod) { long long ans = 1; while (exponent != 0) { if (exponent & 1) ans = (1LL * ans * base) % mod; base = (1LL * base * base) % mod; exponent >>= 1; } return ans % mod; } void solve() { cin >> n >> q; cin >> s; p[0] = s[0] == '1'; for (long long i = 1; i < n; i++) { p[i] = (s[i] == '1') + p[i - 1]; } while (q--) { long long l, r; cin >> l >> r; l--, r--; long long first = p[r] - (l ? p[l - 1] : 0); long long second = r - l + 1 - first; long long ans = (expo(2, first, 1000000007) - 1 + 1000000007) % 1000000007; ans = (ans * expo(2, second, 1000000007)) % 1000000007; cout << ans << '\n'; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
CPP
1062_C. Banh-mi
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.
2
9
import java.util.*; import java.math.*; import java.io.*; public class CF1062C { final int MOD = (int) 1e9 + 7; public CF1062C() { FS scan = new FS(); PrintWriter out = new PrintWriter(System.out); long[] pt = new long[100_001]; pt[0] = 1; for(int i = 1 ; i < 100_001 ; i++) pt[i] = (pt[i - 1] << 1) % MOD; int n = scan.nextInt(), q = scan.nextInt(); char[] x = scan.next().toCharArray(); int[] pre1 = new int[n + 1]; int[] pre0 = new int[n + 1]; for(int i = 1 ; i <= n ; i++) { pre0[i] = pre0[i - 1] + (x[i - 1] == '0' ? 1 : 0); pre1[i] = pre1[i - 1] + (x[i - 1] == '1' ? 1 : 0); } for(int qq = 0 ; qq < q ; qq++) { int l = scan.nextInt(), r = scan.nextInt(); int z = pre0[r] - pre0[l - 1]; int o = pre1[r] - pre1[l - 1]; long res = (pt[z] - 1 + MOD) % MOD; res *= (pt[o] - 1 + MOD) % MOD; res %= MOD; res += (pt[o] - 1 + MOD) % MOD; res %= MOD; out.println(res); } out.close(); } class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] args) { new CF1062C(); } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author real */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); long mod = (long) 1e9 + 7; String str = in.readString(); int ct1[] = new int[n + 1]; int ct2[] = new int[n + 1]; for (int i = 0; i < n; i++) { ct1[i + 1] = ct1[i]; ct2[i + 1] = ct2[i]; if (str.charAt(i) != '0') ct1[i + 1]++; else ct2[i + 1]++; } long val2[] = new long[n + 2]; val2[0] = 1; for (int i = 1; i < n + 2; i++) { val2[i] = val2[i - 1] * 2; val2[i] %= mod; } long sum[] = new long[n + 2]; for (int i = 1; i < n + 2; i++) { sum[i] += sum[i - 1] + val2[i - 1]; sum[i] %= mod; } while (m > 0) { int l = in.nextInt(); int r = in.nextInt(); long once = ct1[r] - ct1[l - 1]; long zero = ct2[r] - ct2[l - 1]; if (once == 0) { out.println(0); } else { long ans = sum[(int) once]; // System.out.println(once+" "+zero); long init = ans; ans = ans + (init * (sum[(int) zero])) % mod; ans %= mod; if (ans < 0) ans += mod; out.println(ans); } m--; } } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare------ //remeber while comparing 2 non primitive data type not to use == //remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort //again silly mistakes ,yr kb tk krta rhega ye mistakes //try to write simple codes ,break it into simple things // for test cases make sure println(); ;) //knowledge>rating /* public class Main implements Runnable{ public static void main(String[] args) { new Thread(null,new Main(),"Main",1<<26).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC();//chenge the name of task solver.solve(1, in, out); out.close(); } */ if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
1062_C. Banh-mi
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.
2
9
//package graphs; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class PW { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader s = new FastReader(); //int a=s.nextInt(); //lon n=s.nextLong(); int n=s.nextInt(); int q=s.nextInt(); long mod=1000000007; String str=s.next(); long a[]=new long[str.length()]; long ans=0; for(int i=0;i<a.length;i++) { if(str.charAt(i)=='1') { if(i==0) a[i]=1; else a[i]=1+a[i-1]; } else { if(i-1>=0) a[i]=a[i-1]; } } for(int i=0;i<q;i++) { int l=s.nextInt(); int r=s.nextInt(); ans=0; l--;r--; long ones=0; if(l==0) ones=a[r]; else ones=a[r]-a[l-1]; long zeros=(r-l+1)-ones; //System.out.println("z "+zeros+"o "+ones); ans=(ans%mod+(( (fastExpo(2,ones,mod)-1)%mod) * (fastExpo(2,zeros,mod))%mod)%mod)%mod; System.out.println(ans); } } public static long solve(int n, int r) { long p = 1, k = 1; if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } //System.out.println(p); return p; } public static long gcd(long a,long b) { if(a==0||b==0) return a+b; return gcd(b,(a%b)); } public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } public static boolean prime(int n) { if(n<=2) return true; for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static long fastExpo(long a,long n,long mod){ if (n == 0) return 1; else{ long x = fastExpo(a,n/2,mod); if ((n&1) == 1){ return (((a*x)%mod)*x)%mod; } else{ return (((x%mod)*(x%mod))%mod)%mod; } } } } class pair{ //public: long f; long s; pair(long x,long y) { f=x; s=y; } }
JAVA
1062_C. Banh-mi
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.
2
9
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; import static java.lang.Math.*; public class Main { FastScanner in; PrintWriter out; void run() { in = new FastScanner(); out = new PrintWriter(System.out); problem(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void main(String[] args) { new Main().run(); } void build (int a[], int v, int tl, int tr) { if (tl == tr) t[v] = a[tl]; else { int tm = (tl + tr) / 2; build(a, v * 2, tl, tm); build(a, v * 2 + 1, tm + 1, tr); t[v] = t[v * 2] + t[v * 2 + 1]; } } int sum (int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (l == tl && r == tr) return t[v]; int tm = (tl + tr) / 2; return sum (v*2, tl, tm, l, min(r,tm)) + sum (v*2+1, tm+1, tr, max(l,tm+1), r); } public static long modPower(int x, int y) { if(y==0) return 1; long z = modPower(x, y/2); if ((y % 2) == 0) return (z*z)%N; else return (x*z*z)%N; } int [] t; static int N = (int)pow(10, 9) + 7; void problem() { int n = in.nextInt(); int q = in.nextInt(); String s = in.nextToken(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Character.getNumericValue(s.charAt(i)); } t = new int [4*n]; build(a, 1, 0, n - 1); for (int asd = 0; asd < q; asd++) { int l = in.nextInt(); int r = in.nextInt(); int k = sum(1, 0, n - 1, l - 1, r - 1); int z = r - l + 1 - k; long a1 = modPower(2, k) - 1; long sum0 = ((modPower(2, z) - 1) * a1) % N; out.println((a1 + sum0) % N); } } }
JAVA
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; int dcmp(long double n, long double y) { return fabs(n - y) <= 1e-9 ? 0 : n < y ? -1 : 1; } const int MAX = 1e5 + 10; const long long MOD = 1e9 + 7; long long prefix[MAX][2], sum_of_pow[MAX], n, q; string in; long long POW_M(long long a, long long p, long long m = MOD) { if (p == 0) return 1; if (p == 1) return a % m; long long x = POW_M(a, p / 2, m); if (p % 2 == 0) return ((x % m) * x) % m; return (((x % m) * x % m) * a) % m; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n >> q; sum_of_pow[0] = 1; cin >> in; for (long long i = 1; i < MAX; i++) { sum_of_pow[i] = (2LL * sum_of_pow[i - 1]) % MOD; } for (int i = 0; i < (int)(MAX - 2); ++i) { sum_of_pow[i + 1] = (sum_of_pow[i + 1] % MOD + sum_of_pow[i] % MOD) % MOD; } for (int i = 0; i < ((int)((in).size())); ++i) { prefix[i + 1][0] += (in[i] == '0') + prefix[i][0]; prefix[i + 1][1] += (in[i] == '1') + prefix[i][1]; } for (int i = 0; i < (int)(q); ++i) { int l, r; cin >> l >> r; long long z = prefix[r][0] - prefix[l - 1][0]; long long o = prefix[r][1] - prefix[l - 1][1]; long long ans = 0; if (o > 0) { ans += sum_of_pow[o - 1]; } if (z > 0) { ans += (o > 0 ? (sum_of_pow[z - 1] * (sum_of_pow[o - 1] % MOD)) % MOD : 0LL); ans %= MOD; } cout << ans << '\n'; } }
CPP
1062_C. Banh-mi
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.
2
9
n,m=map(int,input().split()) t=list(map(int,list(input()))) p=[0]+t[:] mod=10**9+7 o=[] for i in range(n): p[i+1]+=p[i] for _ in range(m): l,r=map(int,input().split()) a=p[r]-p[l-1] b=(r-l+1)-a o.append(str((pow(2,a+b,mod)-pow(2,b,mod))%mod)) print('\n'.join(map(str,o)))
PYTHON3
1062_C. Banh-mi
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.
2
9
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10; const long long mod = 1e9 + 7; long long a[maxn]; long long quick(long long a, long long n) { long long ans = 1; while (n != 0) { if (n % 2 == 0) a = a % mod * a % mod, n = n / 2; else ans = a % mod * ans % mod, n--; } return ans; } int32_t main() { long long n, q; cin >> n; cin >> q; string ss; cin >> ss; for (long long i = 1; i <= n; i++) { if (ss[i - 1] == '1') a[i] = a[i - 1] + 1; else a[i] = a[i - 1]; } while (q--) { long long l, r; cin >> l >> r; long long d = r - l + 1; long long x = a[r] - a[l - 1]; if (x == 0) { cout << 0 << endl; continue; } long long num = quick(2, x) - 1; num += (quick(2, x) - 1) % mod * (quick(2, d - x) - 1) % mod; cout << num % mod << endl; } }
CPP
1062_C. Banh-mi
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.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Vadim */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); R520_C solver = new R520_C(); solver.solve(1, in, out); out.close(); } static class R520_C { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.ni(); int q = in.ni(); String s = in.ns(); int[] cnts = new int[n + 1]; for (int i = 0; i < n; i++) { cnts[i + 1] = cnts[i]; if (s.charAt(i) == '1') cnts[i + 1]++; } long mod = 1_000_000_007; BigInteger MOD = BigInteger.valueOf(mod); BigInteger TWO = BigInteger.valueOf(2); for (int i = 0; i < q; i++) { int l = in.ni(); int r = in.ni(); int cnt1 = cnts[r] - cnts[l - 1]; int cnt0 = r - l + 1 - cnt1; BigInteger num = TWO.modPow(BigInteger.valueOf(cnt1), MOD).add(MOD).subtract(BigInteger.ONE).mod(MOD).multiply(TWO.modPow(BigInteger.valueOf(cnt0), MOD)).mod(MOD); out.println(num.toString()); } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String ns() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int ni() { return Integer.parseInt(ns()); } } }
JAVA
1062_C. Banh-mi
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.
2
9
import javafx.util.Pair; import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Main mm = new Main(); mm.problemC(new Input(new BufferedReader(new InputStreamReader(System.in))), out); out.close(); } static void problemC(Input in, PrintWriter out) throws IOException { int n=in.nextInt(),q=in.nextInt(); int md=1000000007; int[] ar=new int[n+1],pw=new int[n+1]; for (int i = 0; i < n; i++) { ar[i+1]=ar[i]+in.nextChar()-'0'; } pw[0]=1; for (int i = 1; i <= n; i++) { pw[i]=(pw[i-1]<<1)%md; } for (int i = 0; i < q; i++) { int l=in.nextInt(),r=in.nextInt(); int len=r-l+1; int zeros=len-(ar[r]-ar[l-1]); int res=(pw[len]-pw[zeros]+md)%md; out.println(res); } } static void problemB(Input in, PrintWriter out) throws IOException { int n=in.nextInt(),ans=1,mx=0; boolean bl=false; int pow=0; if(n==1) out.println("1 0"); else{ eratosfen e=new Main().new eratosfen(n); ArrayList<Integer> a=e.fillSieve(); for (int i = 0; n!=1; i++) { int b=a.get(i); int k=0; while (n%b==0){ n/=b; k++; } if(k!=0) { if(mx!=0&&k!=mx)bl=true; mx=Math.max(mx,k); ans*=b; } } int r=1; while(r<mx) { r*=2; pow++; } if(r!=mx||bl)pow++; out.print(ans);out.print(' ');out.println(pow); } } static void problemA(Input in, PrintWriter out) throws IOException { int n=in.nextInt(); int a=in.nextInt(),b=a,k=1,d=1,el=a,mx=0; for (int i = 1; i < n; i++) { a=in.nextInt(); if (a==b+1) { d++; if (d >= k) { k = d; el = a; } }else{ d = 1; if (k == b) { mx = b - 1; } } b=a; } if (b == k) { mx = k - 1; } mx=Math.max(mx,(el==1000)?d-1:0); mx=Math.max(mx,k-2); out.println(mx); } public class eratosfen { boolean[] primes; public eratosfen(int n) { primes=new boolean[n+1]; } public ArrayList<Integer> fillSieve() { ArrayList<Integer> a=new ArrayList<>(); Arrays.fill(primes, true); primes[0] = false; primes[1] = false; for (int i = 2; i < primes.length; ++i) { if (primes[i]) { for (int j = 2; i * j < primes.length; ++j) { primes[i * j] = false; } a.add(i); } } return a; } } static class Input { BufferedReader in; StringBuilder sb = new StringBuilder(); public Input(BufferedReader in) { this.in = in; } public Input(String s) { this.in = new BufferedReader(new StringReader(s)); } public String next() throws IOException { sb.setLength(0); while (true) { int c = in.read(); if (c == -1) { return null; } if (" \n\r\t".indexOf(c) == -1) { sb.append((char) c); break; } } while (true) { int c = in.read(); if (c == -1 || " \n\r\t".indexOf(c) != -1) { break; } sb.append((char) c); } return sb.toString(); } public char nextChar() throws IOException { while (true) { int c = in.read(); if (c == -1) { return (char)c; } if (" \n\r\t".indexOf(c) == -1) { return (char)c; } } } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
JAVA