首页 > 其他分享 >每日一题 第二十七期 Codeforces Round 936 (Div. 2)

每日一题 第二十七期 Codeforces Round 936 (Div. 2)

时间:2024-03-23 10:33:26浏览次数:27  
标签:10 12 sum Codeforces Round test Div include array

B. Maximum Sum

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output
You have an array a a a of n n n integers.

You perform exactly k k k operations on it. In one operation, you select any contiguous subarray of the array a a a (possibly empty) and insert the sum of this subarray anywhere in the array.

Your task is to find the maximum possible sum of the array after k k k such operations.

As this number can be very large, output the answer modulo 1 0 9 + 7 10^9 + 7 109+7.

Reminder: the remainder of a number x x x modulo p p p is the smallest non-negative y y y such that there exists an integer q q q and x = p ⋅ q + y x = p \cdot q + y x=p⋅q+y.

Input

Each test consists of several test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers n n n and k k k ( 1 ≤ n , k ≤ 2 ⋅ 1 0 5 1 \le n, k \le 2 \cdot 10^5 1≤n,k≤2⋅105) — the length of the array a a a and the number of operations, respectively.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1​,a2​,…,an​ ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 −109≤ai​≤109) — the array a a a itself.

It is guaranteed that the sum of the values of n n n and k k k for all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.

Output

For each test, output a single integer — the maximum sum of the array that can be obtained after k k k operations modulo 1 0 9 + 7 10^9 + 7 109+7.

Example
inputCopy
12
2 2
-4 -7
3 3
2 2 8
1 7
7
5 1
4 -2 8 -12 9
7 4
8 14 -9 6 0 -1 3
7 100
5 3 -8 12 -5 -9 3
6 1000
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000
2 1
1000000000 8
5 4
0 0 0 0 0
6 10
48973 757292 58277 -38574 27475 999984
7 1
-1000 1000 -1000 1000 -1000 1000 -1000
10 10050
408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045
outputCopy
999999996
96
896
17
351
716455332
42
2
0
897909241
0
416571966

Note

In the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be ( − 4 ) + ( − 7 ) + 0 + 0 = − 11 (-4) + (-7) + 0 + 0 = -11 (−4)+(−7)+0+0=−11, modulo 1 0 9 + 7 10^9 + 7 109+7 this is 999   999   996 999\,999\,996 999999996.

In the second test case, it is advantageous to take the sum of the entire array three times and place it anywhere in the array, then one of the possible sequences of actions: [ 2 , 2 , 8 2, 2, 8 2,2,8] → \rightarrow → [ 2 , 2 , 8 , 12 2, 2, 8, 12 2,2,8,12] → \rightarrow → [ 2 , 2 , 8 , 12 , 24 2, 2, 8, 12, 24 2,2,8,12,24] → \rightarrow → [ 2 , 2 , 8 , 12 , 24 , 48 2, 2, 8, 12, 24, 48 2,2,8,12,24,48], the sum of the final array is 2 + 2 + 8 + 12 + 24 + 48 = 96 2 + 2 + 8 + 12 + 24 + 48 = 96 2+2+8+12+24+48=96.

In the fourth test case, it is advantageous to take a subarray of the array consisting of the first three numbers (i.e. consisting of the numbers 4 , − 2 4, -2 4,−2 and 8 8 8) and insert its sum at the beginning of the array, thereby obtaining the array [ 10 , 4 , − 2 , 8 , − 12 , 9 10, 4, -2, 8, -12, 9 10,4,−2,8,−12,9], the sum of this array is 17 17 17.

In the seventh test case, it will always be advantageous for us to take an empty subarray of the array. In this case, the sum of the resulting array will not differ from the sum of the original. The answer will be the sum of the original array, taken modulo — 42 42 42, because ( − 6 ⋅ ( 1 0 9 + 7 ) + 42 = − 6   000   000   000 ) (-6 \cdot (10^9 + 7) + 42 = -6\,000\,000\,000) (−6⋅(109+7)+42=−6000000000).

AC代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;

ll t;
ll a[N], num[N];
int main()
{
	cin >> t;
	while(t --){
		ll n , k, s = 0, res = 0, sum = 0;
		cin >> n >> k;
		for(int i = 1; i <= n; i ++)
		{
			cin >> a[i];
			sum = ( a[i] + sum ) % MOD;
		}
		for(int i = 1; i <= n; i ++)
		{
			if(s < 0) s = a[i];
			else s += a[i];
			res = max(res, s);
		}
//		cout << res << "flk" << endl;
		while(k --){
			while(sum < 0) {
				sum += MOD;
			}
			sum = (sum + res) % MOD;
			res = (res * 2) % MOD;
		}
		cout << (sum) % MOD << endl;
	}
	return 0;
}

标签:10,12,sum,Codeforces,Round,test,Div,include,array
From: https://blog.csdn.net/2301_80882026/article/details/136961915

相关文章

  • Codeforces Round 935 (Div. 3)
    A-SettingupCamp思路:判断c能否填充b让b为3的倍数查看代码voidsolve(){inta,b,c;cin>>a>>b>>c;intans=a+b/3;b%=3;if(b>0&&b+c<3)cout<<-1<<'\n';else{ans+=(b+c+2)/3;c......
  • loj#533. 「LibreOJ Round #6」花煎
    非常巧妙的转化。考虑仅计算半边的序列,那么这样的话\(len\)削了一半,要达成的色彩值也开平方了。问题就转化为,将\(l\)拆分为序列\(a\),使得\(\sum_{i=1}^{n}(a_i+1)=l\),且使得\(\prod_{i=1}^{n}a_i\geqk\)的最小\(l\)。经过一些计算,可以发现2的段不超过一个,3的段不......
  • cfRound935div3--DEFG题解
    ps:这场因为精神状态不佳,又C题题意有点绕,卡题了,头晕找不到错.最后做了两题就溜了.狠狠扣90分..D-SeraphimtheOwl题意:即选一个位置,使得其满足题意。而且在满足题意的基础上,要靠近中心越好,如果满足题意而且靠近中心的距离一样,那么输出前面那个.intcnt0[300005]={0};......
  • CF1920 Codeforces Round 919 (Div. 2)
    B.SummationGame给你\(n\)个数(均大于0),Alice先执行一次删除不超过\(k\)个数,Bob再执行一次把最多\(x\)个数变成相反数.问最后数组的最大和是多少?这题本来是想先让Alice删除\(k\)个数,但显然不太容易得到最优解,因为还有可能撤回Alice的删除操作,再加上Bob的操作.......
  • Codeforces Round 935 (Div. 3)
    A.SettingupCamp#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;voidsolve(){inta,b,c;cin>>a>>b>>c;intres=a+b/3;b%=3;if(b!=0){if(c<3-b){......
  • 1943+1944.Codeforces Round 934 (Div. 1,Div. 2) - sol
    20240321终于差不多把Div1补完了(F当然没补),第一次打Div1,还是出了一些小状况的。唉。没有补Div1F的逆天题,选择放弃。Dashboard-CodeforcesRound934(Div.2)-CodeforcesDashboard-CodeforcesRound934(Div.1)-Codeforces2A.DestroyingBridgesThere......
  • Educational Codeforces Round 163 (Rated for Div. 2)
    Preface这周很奇怪,连着计网、数据库、组合数学的课都莫名其妙不上了,突然变得很空闲了的说正好有空赶紧补补题,不然接下来有很多造题/比赛的任务搁置着就忘记了A.SpecialCharacters签到,\(n\)是偶数才有解,构造的话注意到一个AAB可以产生\(2\)的贡献,把\(\frac{n}{2}\)个AAB拼起......
  • Tree Compass Codeforces Round 934 (Div. 2) 1944E
    Problem-E-Codeforces题目大意:有一棵n个点的树,初始状态下所有点都是白色的,每次操作可以选择一个点u和一个距离dis,使得距离点u所有长度为dis的点变为黑色,问最少需要多少次操作能使所有点变成黑色,输出所有操作1<=n<=2000思路:要想操作数最少,就要使每次操作涂黑的点的数量尽......
  • HDU 1059 Dividing
    题目传送门:Problem-1059(hdu.edu.cn)问题描述玛莎和比尔拥有一些弹珠。他们希望在自己之间分配收藏品,以便双方都获得平等的弹珠份额。如果所有弹珠都具有相同的价值,这将很容易,因为这样他们就可以将收藏品分成两半。但不幸的是,有些弹珠比其他弹珠更大或更漂亮。因此,Marsha......
  • linux 中shell脚本中遇到 Runtime error (func=(main), adr=22): Divide by zero
    在Linux中编写Shell脚本时,遇到“Runtimeerror(func=(main),adr=22):Dividebyzero”这样的错误通常是因为在脚本中进行了除以零的操作,类似于编程语言中的除零错误。要解决这个问题,您需要检查Shell脚本中涉及到除法运算的地方,确保分母不为零。下面是一个示例S......