Dancepted

Dancing Acceped!

Codeforces1062C. Banh-mi(贪心+快速幂)

题目链接:传送门

题目:

C. Banh-mi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.

First, he splits the Banh-mi into n
parts, places them on a row and numbers them from 1 through n. For each part i, he defines the deliciousness of the part as xi∈{0,1}. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the i-th part then his enjoyment of the Banh-mi will increase by xi and the deliciousness of all the remaining parts will also increase by xi. The initial enjoyment of JATC is equal to 0

.

For example, suppose the deliciousness of 3
parts are [0,1,0]. If JATC eats the second part then his enjoyment will become 1 and the deliciousness of remaining parts will become [1,_,1]. Next, if he eats the first part then his enjoyment will become 2 and the remaining parts will become [_,_,2]. After eating the last part, JATC's enjoyment will become 4

.

However, JATC doesn't want to eat all the parts but to save some for later. He gives you q
queries, each of them consisting of two integers li and ri. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range [li,ri]

in some order.

All the queries are independent of each other. Since the answer to the query could be very large, print it modulo 109+7

.
Input

The first line contains two integers n
and q (1≤n,q≤100000

).

The second line contains a string of n
characters, each character is either '0' or '1'. The i-th character defines the deliciousness of the i

-th part.

Each of the following q
lines contains two integers li and ri (1≤li≤ri≤n

) — the segment of the corresponding query.
Output

Print q
lines, where i-th of them contains a single integer — the answer to the i-th query modulo 109+7

.
Examples
Input
Copy

4 2
1011
1 4
3 4

Output
Copy

14
3

Input
Copy

3 2
111
1 2
3 3

Output
Copy

3
1

Note

In the first example:

    For query 1

: One of the best ways for JATC to eats those parts is in this order: 1, 4, 3, 2
.
For query 2
: Both 3, 4 and 4, 3

    ordering give the same answer. 

In the second example, any order of eating parts leads to the same answer.
View Code

题目大意:

  给出一个长度为n的二进制串,q次询问。

  每次询问l到r区间内吃Banh-mi得到的美味值的最大值。

  吃掉一个位置,会得到这个位置的美味值(初始值为0、1),其他位置的美味值也会加上这个位置的美味值。

思路:

  一个位置被吃掉后的贡献是和剩下的位置的数量正相关的,所以要贪心地吃美味值大的位置。

  然后举两个栗子模拟一下发现一只吃1的话,得到的美味值是1、2、4、8、16。。。

  然后开始吃0的话就是31、62、124、31*8、31*16。。。

  意会一下,答案大概就是(2cnt1-1)*2cnt0

  预处理前缀和就可以O(1)求出cnt1和cnt0了,然后快速幂跑一跑,时间复杂度为O(nlogn)。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;

string a;
ll sum[MAX_N];

ll fpow(ll a, ll p)
{
    ll ans = 1;
    for (; p; p >>= 1) {
        if (p&1)
            ans = (ans*a)%MOD;
        a = (a*a)%MOD;
    }
    return ans%MOD;;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    int N, Q;
    cin >> N >> Q;
    cin >> a;
    sum[0] = 0;
    for (int i = 1; i <= N; i++) {
        sum[i] = sum[i-1];
        if (a[i-1] == '1')
            sum[i]++;
    }
    while (Q--) {
        ll l, r;
        cin >> l >> r;
        ll len = r-l+1;
        ll cnt1 = sum[r] - sum[l-1];
        ll cnt0 = len - cnt1;
        ll ans = fpow(2, cnt1) - 1;
        if (ans < 0)
            ans += MOD;
        ans = (ans * fpow(2, cnt0)) % MOD;
        cout << ans << endl;
    }
    return 0;
}
View Code

posted on 2018-11-15 16:58  Danceped  阅读(506)  评论(0编辑  收藏  举报

导航