Prefix Sums

在 n >= 4时候直接暴力。

n <= 4的时候二分加矩阵快速幂去check

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-;
const double PI = acos(-); const int MN = ;
struct Matrix {
LL a[MN][MN];
Matrix() {
memset(a, , sizeof(a));
}
void init() {
for(int i = ; i < MN; i++)
a[i][i] = ;
}
Matrix operator * (const Matrix &B) const {
Matrix C;
for(int i = ; i < MN; i++)
for(int j = ; j < MN; j++)
for(int k = ; k < MN; k++) {
C.a[i][j] += 1.0 * a[i][k] * B.a[k][j] <= 2e18 ? a[i][k] * B.a[k][j] : INF;
C.a[i][j] = min(C.a[i][j], INF);
}
return C;
}
Matrix operator ^ (LL b) {
Matrix C; C.init();
Matrix A = (*this);
while(b) {
if(b & ) C = C * A;
A = A * A; b >>= ;
}
return C;
}
}; int n;
LL k;
LL a[N], b[N];
Matrix mat;
int Mat[][] = {
{, , },
{, , },
{, , }
}; int main() {
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
mat.a[i][j] = Mat[i][j];
scanf("%d%lld", &n, &k);
for(int i = ; i < n; i++) scanf("%d", &a[i]);
int m = n; n = ;
for(int i = , flag = ; i < m; i++) {
if(a[i]) flag = ;
if(flag) a[n++] = a[i];
}
LL mx = ;
for(int i = ; i < n; i++) mx = max(mx, a[i]);
if(mx >= k) {
puts("");
return ;
}
if(n >= ) {
for(int o = ; o <= && mx < k; o++) {
LL prefix = ;
for(int i = ; i < n && mx < k; i++) b[i] = i ? b[i - ] + a[i] : a[i];
for(int i = ; i < n && mx < k; i++) a[i] = b[i], mx = max(mx, a[i]);
if(mx >= k) {
printf("%d\n", o);
return ;
}
}
} else {
for(int i = n - ; i >= ; i--) a[ - (n - i)] = a[i];
for(int i = ; i < ( - n); i++) a[i] = ;
LL low = , high = (LL)1e18, ans = high;
while(low <= high) {
LL mid = (low + high) >> ;
Matrix tmp = mat ^ mid;
LL val = ;
for(int i = ; i < ; i++) {
val += 1.0 * a[i] * tmp.a[][i] <= 2e18 ? a[i] * tmp.a[][i] : INF;
val = min(val, INF);
}
if(val >= k) high = mid - , ans = mid;
else low = mid + ;
}
printf("%lld\n", ans);
}
return ;
} /*
*/

Codeforces 837F Prefix Sums的更多相关文章

  1. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

  2. Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]

    PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...

  3. CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)

    传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...

  4. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  5. 【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query

    A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper- ...

  6. 【题解】【数组】【Prefix Sums】【Codility】Passing Cars

    A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...

  7. CF1303G Sum of Prefix Sums

    点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...

  8. codeforces:Prefix Sums分析和实现

    题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列 ...

  9. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

随机推荐

  1. 运维与自动化系列④自动化部署基础与git

    运维与自动化系列④自动化部署基础与git 自动化部署基础与git 一:上一篇的代码是保存在本地,但是在生产环境当中是由版本控制进行代码管理,以便于发布代码和回滚,一般是使用gitlib比较多,另外还有 ...

  2. [PHP]常量的一些特性

    1. define()函数如果定义了两个名称一模一样的常量,那么它将不会抛出错误,并且只有第一次的定义是生效的,再次定义并不会覆盖先前的值: define('ABC', 0); define('ABC ...

  3. GZip、deflate和sdch压缩(网摘整理)

    GZip和deflate: gzip是一种数据格式,默认且目前仅使用deflate算法压缩data部分:deflate是一种压缩算法,是huffman编码的一种加强. deflate与gzip解压的代 ...

  4. AES对数据进行加密与解密

    AES对数据进行加密与解密随着对称密码的发展,DES数据加密标准算法由于密钥长度较小(56位),已经不适应当今分布式开放网络对数据加密安全性的要求,因此1997年NIST公开征集新的数据加密标准,即A ...

  5. CSS弹性(flexible)盒子

    弹性盒子         弹性盒子由弹性容器(Flex container)和弹性子元素(Flex item)组成 弹性容器通过display:flex | inline-flex将其定义为弹性容器 ...

  6. 手机端rem 用法

    !function(n){ var e=n.document, t=e.documentElement, i=720, d=i/100, o="orientationchange" ...

  7. 【python】控制台输出颜色

    来源:http://www.cnblogs.com/yinjia/p/5559702.html 在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端 ...

  8. Laravel5使用QQ邮箱发送邮件配置

    在.env文件中设置如下MAIL_DRIVER=smtpMAIL_HOST=smtp.qq.comMAIL_PORT=465MAIL_USERNAME=00000000000@qq.comMAIL_P ...

  9. 伪Ap接入点

    1.创建一个伪造的Ap接入点,必须购买一个无线网卡的设备,接受功率在300Mbps ,低于这个传输速率的值,效果很差,都达到用户可以连接验证的效果.其芯片必须支持kali linux 内核系统. 2. ...

  10. anaconda利用pip安装module

    开始_程序 中搜索:anaconda prompt (控制台) 输入pip 出现pip的一些信息,可以忽略 接着输入 pip install 模块名称 例如:pip install alphalens ...