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

题目有个限制是不能全部删除, 所以最后一定剩余a[1]或a[n], 暴力枚举a[1]与a[n]的所有素因子即可.

这场div. 2题目感觉都挺简单的, 但实现起来各种出错...........各种细节还是没考虑好......

#include <iostream>
#include <vector>
#include <cmath>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
using namespace std;
typedef long long ll; const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6+10;
int n, x, y;
int a[N], b[N];
ll ans, dp[3][N]; ll solve(int g, int *a, int n) {
memset(dp,0x3f,sizeof dp);
dp[0][0]=dp[1][0]=dp[2][0]=0;
REP(i,1,n) {
dp[0][i] = dp[0][i-1];
dp[1][i] = min(dp[0][i-1]+x,dp[1][i-1]+x);
dp[2][i] = min({dp[0][i-1],dp[1][i-1],dp[2][i-1]});
if (a[i]%g) {
if ((a[i]-1)%g==0||(a[i]+1)%g==0) dp[0][i]+=y,dp[2][i]+=y;
else dp[0][i]=dp[2][i]=INF;
}
}
return min(dp[1][n],dp[2][n]);
} vector<int> fac(int num) {
int mx = sqrt(num+0.5);
vector<int> v;
REP(i,2,mx) if (num%i==0) {
v.pb(i);
while (num%i==0) num/=i;
}
if (num>1) v.pb(num);
return v;
} void solve(int num, int *a, int n, int tp) {
vector<int> g = fac(num);
for (auto &&t:g) ans = min(ans, solve(t,a,n)+tp*y);
} int main() {
scanf("%d%d%d", &n, &x, &y);
REP(i,1,n) scanf("%d", a+i);
ans = INF;
solve(a[1],a+1,n-1,0);
solve(a[1]-1,a+1,n-1,1);
solve(a[1]+1,a+1,n-1,1);
solve(a[n],a,n-1,0);
solve(a[n]-1,a,n-1,1);
solve(a[n]+1,a,n-1,1);
printf("%lld\n", ans);
}

Array GCD CodeForces - 624D (dp,gcd)的更多相关文章

  1. Array Beauty CodeForces - 1189F (dp,好题)

    大意: 定义$n$元素序列$a$的美丽度为 $\min\limits_{1\le i<j\le n}|a_i-a_j|$. 给定序列$a$, 求$a$的所有长为$k$的子序列的美丽度之和. 记 ...

  2. CodeForces - 803C Maximal GCD 【构造】

    You are given positive integer number n. You should create such strictly increasing sequence of k po ...

  3. HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)

    树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/6 ...

  4. iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组

    一.GCD的基本使用 <1>GCD简介 什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数   GCD的优势 G ...

  5. codechef Dynamic GCD [树链剖分 gcd]

    Dynamic GCD 题意:一棵树,字词树链加,树链gcd 根据\(gcd(a,b)=gcd(a,a-b)\) 得到\(gcd(a_1, a_2, ..., a_i) = gcd(a_1, a_1- ...

  6. 【CodeForces 624D】Array GCD

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

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

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

  8. Recovering BST CodeForces - 1025D (区间dp, gcd)

    大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...

  9. UESTC 923 稳住GCD DP + GCD

    定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加,  dp[i][j] ...

随机推荐

  1. zabbix 千台服务器自动添加实战

    一,模式   zabbix 的自动添加 主机有梁祝方式: 自动发现-----被动模式 由服务端主动发起,Zabbix Server开启发现进程,定时扫描局域网中IP服务器.设备, 自动注册----主动 ...

  2. Java基础18-toString()方法、this关键字

    1.toString()方法 在java中,所有对象都有toString()这个方法 创建类时没有定义toString方法输出对象时会输出哈希码值 它通常只是为了方便输出,比System.out.pr ...

  3. ksframework的xlua版本

    https://github.com/zhaoqingqing/KSFramework_xlua

  4. 在Unity中创建攻击Slot系统

    http://www.manew.com/thread-109310-1-1.html 马上注册,结交更多好友,享用更多功能,让你轻松玩转社区. 您需要 登录 才可以下载或查看,没有帐号?注册帐号  ...

  5. log4j整理

    <meta http-equiv="refresh" content="1"/> # log4j日志组件 #- SLF4J,一个**通用日志接口** ...

  6. OpenLayers 案例一

    序 OpenLayers 是一个专为Web GIS 客户端开发提供的JavaScript 类库包,用于实现标准格式发布的地图数据访问. 例子 <!doctype html> <htm ...

  7. React.js 小书 Lesson14 - 实战分析:评论功能(一)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson14 转载请注明出处,保留原文链接和作者信息. 课程到这里大家已经掌握了 React.js 的 ...

  8. fabric省略输出

    fab -f test_fabric.py start --hide status,running,stdout,user,aborts,warnings,stderr 省略所有输出--hide st ...

  9. 在 Azure 中创建静态 HTML Web 应用

    Azure Web 应用提供高度可缩放.自修补的 Web 托管服务. 本快速入门教程演示如何将基本 HTML+CSS 站点部署到 Azure Web 应用. 使用 Azure CLI 创建 Web 应 ...

  10. oracle之数据同步:Oracle Sql Loader使用说明(大批量快速插入数据库记录)

    1.准备表数据 select * from emp10; create sequence seq_eseq increment start maxvalue ; --得到序列的SQL语句 select ...