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 ...
随机推荐
- T-SQL GROUP BY子句 分组查询
SQL Server GROUP BY子句与SELECT语句协作使用,以将相同的数据分组. GROUP BY子句位于SELECT语句中的WHERE子句之后,位于ORDER BY子句之前. 语法 以下是 ...
- C++ DLL
DLL(Dynamic Link Library)(1)DLL 的编制与具体的编程语言及编译器无关只要遵循约定的DLL接口规范和调用方式,用各种语言编写的DLL都可以相互调用.譬如Windows提供的 ...
- GZip、deflate和sdch压缩(网摘整理)
GZip和deflate: gzip是一种数据格式,默认且目前仅使用deflate算法压缩data部分:deflate是一种压缩算法,是huffman编码的一种加强. deflate与gzip解压的代 ...
- iOS ATS问题(补充中)
首先,针对2017年一月后,需要提交到apple store的程序,如果使用App Transport Security Settings对程序 ATS情况进行特殊配置,需要对苹果进行说明,指出使用的 ...
- 大数据mapreduce全局排序top-N之python实现
a.txt.b.txt文件如下: a.txt hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop ...
- Modbus库开发笔记之五:Modbus RTU Slave开发
Modbus在串行链路上分为Slave和Master,这一节我们就来开发Slave.对于Modbus RTU从站来说,需要实现的功能其实与Modbus TCP的服务器端是一样的.其操作过程也是一样的. ...
- 本文转自 MyEclipse 2015反编译插件安装
本文转自MyEclipse 2015反编译插件安装 分享一下下载插件的地址,百度网盘:链接:http://pan.baidu.com/s/1nturiAH 密码:yk73 其次:我来说下具体操作步骤: ...
- Confluence 6 升级自定义的站点和空间获得你的自定义布局
我们建议你在对站点进行布局修改的时候,你需要为你修改的 Confluence 站点或空间布局保留所有的修改记录. 如果没有的话,你应该可以通过下面的办法找到你的自定义修改.这个方法将会把你对全部网站和 ...
- Swift中使用oc代码桥接设置
1 将oc的代码拖入项目中 2 新建一个头文件 在头文件中导入你想用的oc头文件 import "****.h" 3 在设置build Setting 中搜索bird 找到 Ob ...
- 【python】json中字典key不可为数值型
遇到了一个很诡异的错误.写一个字典,存入json文件.之后读出判断是否存在key.结果惊奇的发现,同一个key居然存在两次. 原因:json会将数值key转换为unicode 结论:使用json时字典 ...