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的更多相关文章

  1. 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 ...

  2. 【CodeForces 624D/623B】Array GCD

    题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...

  3. 【CodeForces 624D】Array GCD

    题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...

  4. Array GCD CodeForces - 624D (dp,gcd)

    大意: 给定序列, 给定常数a,b, 两种操作, (1)任选一个长为$t$的子区间删除(不能全部删除), 花费t*a. (2)任选$t$个元素+1/-1, 花费t*b. 求使整个序列gcd>1的 ...

  5. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

  6. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  7. Codeforces 803C. Maximal GCD 二分

    C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...

  8. Codeforces 338 D. GCD Table

    http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...

  9. CodeForces 57C Array 组合计数+逆元

    题目链接: http://codeforces.com/problemset/problem/57/C 题意: 给你一个数n,表示有n个数的序列,每个数范围为[1,n],叫你求所有非降和非升序列的个数 ...

随机推荐

  1. 洛谷P4382 [八省联考2018]劈配(网络流,二分答案)

    洛谷题目传送门 说不定比官方sol里的某理论最优算法还优秀一点? 所以\(n,m\)说不定可以出到\(1000\)? 无所谓啦,反正是个得分题.Orz良心出题人,暴力有70分2333 思路分析 正解的 ...

  2. python之旅:异常处理

    一 什么是异常 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 一个异常分为三部分 ...

  3. Git基本知识

    一,安装 Ubuntu安装git:sudo apt-get install git-core Centos安装git:yum install git-core 二,配置身份---在提交代码时可以辨别身 ...

  4. java回顾(项目前期的基本准备)

    一.     基础回顾 1   集合 1.1 集合的类型与各自的特性 ---|Collection: 单列集合 ---|List: 有存储顺序, 可重复 ---|ArrayList:  数组实现, 查 ...

  5. 工具类 | window批处理杀死指定端口进程

    window批处理杀死指定端口进程,注意保存时使用ansi格式,运行输入端口即可 @echo off setlocal enabledelayedexpansion set /p port=请输入端口 ...

  6. java concurrent 中ExecutorService和CompletionService简单区别

    举个例子,现在需要执行10个任务,这些任务都是有返回值,并且需要使用10个线程同时执行.一般的做法就是创建ExecutorService线程池,pool大小10,每个任务实现Callable接口,然后 ...

  7. getopts 用法

    getopts是linux系统中的一个内置变量,一般用在循环中.每当执行循环是,getopts都会检查下一个命令选项,如果这些选项出现在option中,则表示是合法选项,否则不是合法选项.并将这些合法 ...

  8. linux - JDK 环境

    JDK安装 vi /etc/profile # 添加环境变量 export JAVA_HOME=/usr/local/jdk1.8.2_45 export CLASSPATH=.:$JAVA_HOME ...

  9. 第11月第8天 ffmpeg ffplay

    static int ffplay_video_thread(void *arg) { FFPlayer *ffp = arg; VideoState *is = ffp->is; AVFram ...

  10. ifconfig,netstat command not found

    当CentOS7进行最小化安装时,有很多工具包是没有的. [root@vultr ~]# ifconfig -bash: ifconfig: command not found [root@vultr ...