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. JAVA类变量(静态变量)

    类变量也称为静态变量,在类中以static关键字声明,但必须在方法.构造方法和语句块之外. -无论一个类创建了多少个对象,类只拥有类变量的一份拷贝. -静态变量除了被声明为常量外很少使用.常量是指声明 ...

  2. D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...

  3. Eclipse配置Maven的一些问题

    问题1:连接私服 build项目非常缓慢 配置好本地的setting文件后,发现build非常缓慢,照显示的进度,可能要一天才会build后一个项目,同事指导解决方法如下: MyEclipse2017 ...

  4. nginx 重写URL尾部斜杠

    1. 在URL结尾添加斜杠 在虚拟主机中这么添加一条改写规则: rewrite ^(.*[^/])$ $1/ permanent;或者rewrite ^([/\w-_]*[^/])$ $1/ perm ...

  5. Spark记录-org.apache.spark.sql.hive.HiveContext与org.apache.spark.sql.SQLContext包api分析

    HiveContext/SQLContext val hiveContext=new HiveContext(new SparkContext(new SparkConf().setAppName(& ...

  6. Spark记录-scala快速入门

    1.hello world程序 object HelloWorld { def main(args: Array[String]) { println("Hello,World!" ...

  7. bzoj千题计划262:bzoj4868: [六省联考2017]期末考试

    http://www.lydsy.com/JudgeOnline/problem.php?id=4868 假设 最晚出成绩的是第i天 预处理 cnt[i] 表示 有多少个学生 期望出成绩的那一天 &l ...

  8. linux解压

    tar –xvf file.tar //解压 tar包tar -xzvf file.tar.gz //解压tar.gztar -xjvf file.tar.bz2 //解压 tar.bz2tar –x ...

  9. Spring RedisTemplate操作-事务操作(9)

    @Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> stringr ...

  10. nodejs安装zmq出错

    想用zmq来做进程间通信,在Windows下.Centos下安装成功.记录如下: 一.Windows安装zmq 直接 npm install zmq  成功就成功. 不成功的话估计是报"未能 ...