Codeforces 837F 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的更多相关文章
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- 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]代表已经选择 ...
- CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)
传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- 【题解】【数组】【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- ...
- 【题解】【数组】【Prefix Sums】【Codility】Passing Cars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...
- CF1303G Sum of Prefix Sums
点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...
- codeforces:Prefix Sums分析和实现
题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列 ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
随机推荐
- List<T>常用操作
1.List<T>类型强制转换: List<UIData> datalist=null;datalist.ConvertAll<object>(input => ...
- nodejs,javascript过滤emoj表情
1 前言 由于带emoj表情的昵称无法存储在mysql character_set_server= utf8模式下,按照参考文章[1],改成utf8mb4,无效(可能使用方法不对). 总体思路是,把昵 ...
- xampp 搭建好本地服务器以后手机无法访问
转载地址:https://blog.csdn.net/weixin_35773751/article/details/80076492 解决办法: 1.将网络连接修改为工作网络,然后关闭工作局域网的防 ...
- Java学习——集合框架【4】
一.集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个层次. 实 ...
- 论文阅读:Review of Visual Saliency Detection with Comprehensive Information
这篇文章目前发表在arxiv,日期:20180309. 这是一篇针对多种综合性信息的视觉显著性检测的综述文章. 注:有些名词直接贴原文,是因为不翻译更容易理解.也不会逐字逐句都翻译,重要的肯定不会错过 ...
- 《剑指offer》栈的插入弹出序列
本题来自<剑指offer> 栈的插入弹出序列 题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2, ...
- java内部类和异常类的概念
1.内部类的外嵌类的成员变量在内部类中任然有效,内部类中的方法也可以调用外嵌类中的 方法,内部类中不可以声明类的变量和方法,外嵌的类体可以用内部类声明对象,作为外嵌类的成员.内部类仅供他的外嵌类使用. ...
- mybatis常见错误总结
1. 现象:mybatis xml文件中查询的返回类型写成list或java.util.List时,执行sql时报 java.lang.UnsupportedOperationException错误. ...
- str类型
str:字符串类型,用单引号或双引号. #索引 s1 = s[0] 切片 : 顾头不顾尾 : s[首:尾:步长] 字符串的操作: 1.首字母大写: s = 'alexWUsir' s1 = s.cap ...
- oracle 备份脚本
本文是一个shell脚本.主要用于Oracle 数据库备份.默认情况下,在周一晚上进行全备.其他时间进行累积增量备份. 使用方法: 假如脚本保存名为: oracle_backup.sh 使用方法为 o ...