Codeforces 623B Array GCD
最后的序列里肯定有a[1], a[1]-1, a[1]+1, a[n], a[n]-1, a[n]+1中的一个,枚举质因子, dp去check
#include<bits/stdc++.h>
#define LL long long
#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 = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; LL n, a, b, tot, ans = INF;
LL c[N], fac[], dp[N][]; void solve(int x) {
for(int i = ; i * i < x; i++) {
if(x % i) continue;
fac[tot++] = i;
while(x % i == ) x /= i;
}
if(x > ) fac[tot++] = x;
sort(fac, fac + tot);
tot = unique(fac, fac + tot) - fac;
} void calc(int fac) {
memset(dp, INF, sizeof(dp));
dp[][] = ;
for(int i = , r1, r2, r3; i < n; i++) {
r1 = (c[i + ] - ) % fac;
r2 = r1 + ; if(r2 >= fac) r2 -= fac;
r3 = r2 + ; if(r3 >= fac) r3 -= fac;
if(dp[i][] < INF) {
dp[i + ][] = min(dp[i + ][], dp[i][] + a);
if(!r2) dp[i + ][] = min(dp[i + ][], dp[i][]);
else if(!r1 || !r3) dp[i + ][] = min(dp[i + ][], dp[i][] + b);
}
if(dp[i][] < INF) {
dp[i + ][] = min(dp[i + ][], dp[i][] + a);
if(!r2) dp[i + ][] = min(dp[i + ][], dp[i][]);
else if(!r1 || !r3) dp[i + ][] = min(dp[i + ][], dp[i][] + b);
}
if(dp[i][] < INF) {
if(!r2) dp[i + ][] = min(dp[i + ][], dp[i][]);
else if(!r1 || !r3) dp[i + ][] = min(dp[i + ][], dp[i][] + b);
}
}
for(int i = ; i < ; i++)
ans = min(ans, dp[n][i]);
} int main() {
scanf("%lld%lld%lld", &n, &a, &b);
for(int i = ; i <= n; i++) scanf("%lld", &c[i]);
for(int i = -; i <= ; i++) solve(c[] + i);
for(int i = -; i <= ; i++) solve(c[n] + i);
for(int i = ; i < tot; i++) calc(fac[i]);
printf("%lld\n", ans);
return ;
} /*
*/
Codeforces 623B Array GCD的更多相关文章
- AIM Tech Round (Div. 2) D. Array GCD dp
D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...
- 【CodeForces 624D/623B】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- 【CodeForces 624D】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- Array GCD CodeForces - 624D (dp,gcd)
大意: 给定序列, 给定常数a,b, 两种操作, (1)任选一个长为$t$的子区间删除(不能全部删除), 花费t*a. (2)任选$t$个元素+1/-1, 花费t*b. 求使整个序列gcd>1的 ...
- codeforces 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- Codeforces 803C. Maximal GCD 二分
C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...
- Codeforces 338 D. GCD Table
http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...
- CodeForces 57C Array 组合计数+逆元
题目链接: http://codeforces.com/problemset/problem/57/C 题意: 给你一个数n,表示有n个数的序列,每个数范围为[1,n],叫你求所有非降和非升序列的个数 ...
随机推荐
- 安利一个很火的 Github 滤镜项目
安利一个很火的 Github 滤镜项目 园长 1 个月前 简评:通过深度学习,一秒钟让你的照片高大上,这是康奈尔大学和 Adobe 的工程师合作的一个新项目,通过卷积神经网络把图片进行风格迁移.项目已 ...
- 【bzoj4516】 Sdoi2016—生成魔咒
http://www.lydsy.com/JudgeOnline/problem.php?id=4516 (题目链接) 题意 依次向字符串末尾加上一个字符,每次求不同子串个数. Solution 如果 ...
- 【bzoj4009】 HNOI2015—接水果
http://www.lydsy.com/JudgeOnline/problem.php?id=4009 (题目链接) 题意 给出一颗无根树.有一些路径记为$P_i$,这些路径有两个端点和一个权值$W ...
- 【洛谷P1119】灾后重建
题目大意:给定一个 N 个顶点,M 条边的无向图,每个顶点有一个时间戳,且时间戳大小按照顶点下标大小依次递增,在给定时间 t 时,时间戳严格大于 t 的顶点不能被访问,现在有 Q 次询问,每次询问在给 ...
- Java基础-JAVA中常见的数据结构介绍
Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- 如何把手机app的视频下载到手机上?网页上的视频怎么下载?
手机上小视频怎么下载?求推荐不需要安装软件的下载方法? 如何把手机app的视频下载到手机上?比如把快手上的视频下载到手机上? 如何免费下载视频? ... 答案当然是用iiiLab提供的在线视频解析下载 ...
- bzoj千题计划181:bzoj1878: [SDOI2009]HH的项链
http://www.lydsy.com/JudgeOnline/problem.php?id=1878 之前用莫队做的,现在用树状数组 把每种数的第一个出现位置在树状数组中+1 nxt[i] 记录i ...
- 第12月第2天 uiscrollview _adjustContentOffsetIfNecessary 圆角
1. uiscrollview在调用setFrame,setBounds等方法的时候会默认调用稀有api: _adjustContentOffsetIfNecessary 这个方法会改变当前的cont ...
- Div中嵌套一个div,怎么是里面的div居中?
盒子居中是在写样式中经常遇到的问题,在这里说个我经常使用的方法~ 利用绝对定位: