【CodeForces - 1062C】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?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 4Output
14 3Input
3 2 111 1 2 3 3Output
3 1Note
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喜歡吃冰激凌,有一天,他買(mǎi)了一箱冰激凌
首先, 他把這些冰激凌分成 n 份, 把它們從1 到n排成一排 . 對(duì)每一部分都有一個(gè) 美味系數(shù) xi 屬于 {0, 1}. Glory打算一個(gè)個(gè)吃掉它們. 每一次,Glory可以隨意選一部分吃掉,假設(shè)這部分是第i部分,然后 Glory的開(kāi)心值 將會(huì)增加xi剩下來(lái)的每一份冰激凌的美味系數(shù)將會(huì)增加xi. Glory最初的開(kāi)心值為 0.
舉個(gè)例子說(shuō)明這個(gè)過(guò)程,假設(shè)冰激凌被分成有 3份,美味系數(shù)為 [0, 1, 0]. 如果Glory先吃第二塊,那么他的開(kāi)心值將變?yōu)?1 ,然后三份的美味系數(shù)變?yōu)?[1, _, 1]. 然后Glory再吃第三塊,那么他的開(kāi)心值將變?yōu)? ,然后三份的美味系數(shù)變?yōu)閇_, _, 2].在吃最后一塊后, Glory的開(kāi)心值變?yōu)?4.
然而, Glory想利益最大化. 他給你 q 詢問(wèn),詢問(wèn)包含 l_i and r_i. 對(duì)每次詢問(wèn),你需要告訴他,他吃完[l_i, r_i]之間的所有冰激凌最多能獲得多少開(kāi)心值(不要求輸出吃的順序,只要求輸出最大收益).
答案可能會(huì)非常大,請(qǐng)mod 10^9+7輸出.
解題報(bào)告:
? 首先看第一步,肯定有1選1,如果同時(shí)有多個(gè)1,那選哪個(gè)1都不影響答案,一次類(lèi)推我們不需要關(guān)注位置,只需要關(guān)注在當(dāng)前查詢的 ( l , r )區(qū)間內(nèi)有多少1,有多少0。通過(guò)貪心,我們知道我們每次需要選擇最大的進(jìn)行貪心。
找?guī)讉€(gè)例子就畫(huà)出結(jié)論了。這里給一個(gè)別人的題解:
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)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 1062C】Banh-mi (贪心,数学,找规律,快速幂)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2019ACM浪潮杯山东省赛参赛总结
- 下一篇: 【牛客 - 317C】小a与星际探索(背