比赛链接

2022 China Collegiate Programming Contest (CCPC) Guilin Site

C. Array Concatenation

给定一个长度为 \(n\) 的数组 \(b_i\),有两种操作变换:

  • \(b^{\prime}=\left\{b_1, b_2, \ldots, b_{|b|}, b_1, b_2, \ldots, b_{|b|}\right\}\)
  • \(b^{\prime}=\left\{b_{|b|}, b_{|b-1|}, \ldots, b_1, b_1, b_2, \ldots, b_{|b|}\right\}\)

\(m\) 次操作后数组的所有前缀的和模 \(10^9+7\) 后的最大值

解题思路

思维

假设长度为 \(n\) 的数组,其和为 \(s\),所有前缀和为 \(ss\),所有后缀和为 \(sss\)
可以发现:当有操作 \(2\) 时,当前数组会变成回文数组,后面无论哪一种操作都不影响结果,且只要有操作 \(2\),其结果都一定,操作 \(m\) 次,最终结果为 \(2^{m-1}\times (ss+sss)+\frac{2^m\times (2^m-1)}{2}\times n\times s\),只有第一种操作的最终结果为 \(2^{m}\times ss+\frac{2^m\times (2^m-1)}{2}\times n\times s\),两种结果取 max 即为答案
另外,这里很容易爆 long long,直接 __int128

  • 时间复杂度:\(O(logm)\)

代码

// Problem: C. Array Concatenation
// Contest: Codeforces - 2022 China Collegiate Programming Contest (CCPC) Guilin Site
// URL: https://codeforces.com/gym/104008/problem/C
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
// #define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e5+5,mod=1e9+7;
__int128 n,m,a[N];
int ksm(int a,int b,int p)
{
	int res=1%p;
	while(b)
	{
		if(b&1)res=(LL)res*a%p;
		a=(LL)a*a%p;
		b>>=1;
	}
	return res;
}
int main()
{
    read(n),read(m);
    __int128 s=0,ss=0,sss=0;
    for(int i=1;i<=n;i++)
    {
    	read(a[i]);
    	s+=a[i];
    	ss=(ss+(LL)(n-i+1)*a[i]%mod)%mod;
    	sss=(sss+(LL)i*a[i]%mod)%mod;
    }
    int res[2];
    res[0]=((LL)ss*ksm(2,m,mod)%mod+(LL)ksm(2,m,mod)*(ksm(2,m,mod)-1)/2%mod*n%mod*s%mod)%mod;
    res[1]=((LL)ss*ksm(2,m-1,mod)%mod+(LL)sss*ksm(2,m-1,mod)%mod+(LL)ksm(2,m,mod)*(ksm(2,m,mod)-1)/2%mod*n%mod*s%mod)%mod;
    cout<<max(res[0],res[1]);
    return 0;
}

原文地址:http://www.cnblogs.com/zyyun/p/16847054.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性