日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 1062C】Banh-mi (贪心,数学,找规律,快速幂)

發(fā)布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 1062C】Banh-mi (贪心,数学,找规律,快速幂) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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

First, he splits the Banh-mi into?nn?parts, places them on a row and numbers them from?11?through?nn. For each part?ii, he defines the?deliciousness?of the part as?xi∈{0,1}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?ii-th part then his?enjoyment?of the Banh-mi will increase by?xixi?and the deliciousness of all the remaining parts will also increase by?xixi. The initial enjoyment of JATC is equal to?00.

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

However, JATC doesn't want to eat all the parts but to save some for later. He gives you?qq?queries, each of them consisting of two integers?lili?and?riri. For each query, you 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][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+7109+7.

Input

The first line contains two integers?nn?and?qq?(1≤n,q≤1000001≤n,q≤100000).

The second line contains a string of?nn?characters, each character is either '0' or '1'. The?ii-th character defines the deliciousness of the?ii-th part.

Each of the following?qq?lines contains two integers?lili?and?riri?(1≤li≤ri≤n1≤li≤ri≤n)?— the segment of the corresponding query.

Output

Print?qq?lines, where?ii-th of them contains a single integer?— the answer to the?ii-th query modulo?109+7109+7.

Examples

Input

4 2 1011 1 4 3 4

Output

14 3

Input

3 2 111 1 2 3 3

Output

3 1

Note

In the first example:

  • For query?11: One of the best ways for JATC to eats those parts is in this order:?11,?44,?33,?22.
  • For query?22: Both?33,?44?and?44,?33?ordering give the same answer.

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

中文題意:

Glory喜歡吃冰激凌,有一天,他買了一箱冰激凌

首先, 他把這些冰激凌分成 n 份, 把它們從1 到n排成一排 . 對每一部分都有一個 美味系數(shù) xi 屬于 {0, 1}. Glory打算一個個吃掉它們. 每一次,Glory可以隨意選一部分吃掉,假設(shè)這部分是第i部分,然后 Glory的開心值 將會增加xi剩下來的每一份冰激凌的美味系數(shù)將會增加xi. Glory最初的開心值為 0.

舉個例子說明這個過程,假設(shè)冰激凌被分成有 3份,美味系數(shù)為 [0, 1, 0]. 如果Glory先吃第二塊,那么他的開心值將變?yōu)?1 ,然后三份的美味系數(shù)變?yōu)?[1, _, 1]. 然后Glory再吃第三塊,那么他的開心值將變?yōu)? ,然后三份的美味系數(shù)變?yōu)閇_, _, 2].在吃最后一塊后, Glory的開心值變?yōu)?4.

然而, Glory想利益最大化. 他給你 q 詢問,詢問包含 l_i and r_i. 對每次詢問,你需要告訴他,他吃完[l_i, r_i]之間的所有冰激凌最多能獲得多少開心值(不要求輸出吃的順序,只要求輸出最大收益).

答案可能會非常大,請mod 10^9+7輸出.

解題報告:

? 首先看第一步,肯定有1選1,如果同時有多個1,那選哪個1都不影響答案,一次類推我們不需要關(guān)注位置,只需要關(guān)注在當前查詢的 ( l , r )區(qū)間內(nèi)有多少1,有多少0。通過貪心,我們知道我們每次需要選擇最大的進行貪心。

找?guī)讉€例子就畫出結(jié)論了。這里給一個別人的題解:

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const ll mod = 1e9+7; const int MAX = 2e5 + 5; char s[MAX]; int sum0[MAX]; int bit[MAX]; int main() { int n,q;cin>>n>>q;cin>>(s+1);for(int i = 1; i<=n; i++) {sum0[i] = sum0[i-1];if(s[i] == '0') sum0[i]++;}int l,r;bit[0]=1;for(int i = 1; i<=n; i++) bit[i] = (bit[i-1]*2)%mod;while(q--) {scanf("%d%d",&l,&r);int len = r-l+1;ll ans = bit[len] - bit[sum0[r] - sum0[l-1] ];if(ans<=0) ans += mod;ans %=mod;printf("%lld\n",ans);}return 0 ;}

?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 1062C】Banh-mi (贪心,数学,找规律,快速幂)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。