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. MySQL联结查询和子查询

    2018-2-24 16:18:12 星期六 今天需要统计一个运营活动的数据, 涉及三个表, 分组比较多 活动描述: 每个人可以领取多张卡片,  好友也可以赠送其卡片, 20或40张卡片可以兑换一个奖 ...

  2. FS 日志空间限定

    一.说明: FS默认安装的log文件,仅仅的限制了每个文件的大小,但是没有限制文件的个数.这种情况下,在FS运行很长时间之后,会出现物理空间不够的情况,导致FS或者mysql 或者其他应用没有空间使用 ...

  3. 《Spring5官方文档》新功能(4,3)

    <Spring5官方文档>新功能 原文链接 译者:supriseli Spring框架的新功能 这一章主要提供Spring框架新的功能和变更. 升级到新版本的框架可以参考.Spring g ...

  4. Bootstrap 固定底部导航栏菜单

    直接上代码: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" ...

  5. 12)django-ORM(单表返回数据3种方式)

    单表查询还回数据有3种形式,返回形式不一样,模板调用方式不同 1)返回Queryset里面内容为对象:Business.objects.all() 这里内容显示是对象 2)返回Queryset里面内容 ...

  6. Confluence 6 基于 Confluence 数据中心的 SAML 单点登录

    安全申明标记语言(Security Assertion Markup Language (SAML))是一个基于 XML 的数据格式,允许各个软件平台通过identity provider (IdP) ...

  7. linux之ab压力测试工具

    等待... https://www.cnblogs.com/myvic/p/7703973.html

  8. nginx安装编译参数

  9. verilog 异步复位代码

    module reset_sync (input clk, input reset_in, output reset_out); (* ASYNC_REG = 'b1; (* ASYNC_REG = ...

  10. 【kafka】celery与kafka的联用问题

    背景:一个小应用,用celery下发任务,任务内容为kafka生产一些数据. 问题:使用confluent_kafka模块时,单独启用kafka可以正常生产消息,但是套上celery后,kafka就无 ...