大意: 给定序列, 给定常数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. shell特殊字符汇总【转】

    Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是shell里面的那些个符号,各种特殊的符号在我们编写Shell脚本的时候如果能够用的好 ...

  2. easyUI----grid

    1.设置标题行高 .datagrid-header-row td{background-color:rgb(15,185,234);color:#fff;height:35px ;font-size: ...

  3. java连接redis使用jedis带密码

    一.引入jedis的Maven配置文件 <!-- redis连接客户端jedis --> <dependency> <groupId>redis.clients&l ...

  4. oled屏幕配套取字模软件使用

    oled屏幕配套取字模软件使用 作者:李剀 出处:https://www.cnblogs.com/kevin-nancy/p/10531368.html欢迎转载,但也请保留上面这段声明.谢谢! **P ...

  5. Python下Mysql数据连接池——单例

    # coding:utf-8 import threading import pymysql from DBUtils.PooledDB import PooledDB from app.common ...

  6. php中的$_GET如何获取带有“#”的参数

    <?php echo $_GET['key']; ?> 当url为http://test.com/c.php?key=999时,正常输出:999 当url为http://test.com/ ...

  7. Java学习第二十一天

    1:字符流(掌握) (1)字节流操作中文数据不是特别的方便,所以就出现了转换流. 转换流的作用就是把字节流转换字符流来使用. (2)转换流其实是一个字符流 字符流 = 字节流 + 编码表 (3)编码表 ...

  8. DistinctBy

    如何很好的使用Linq的Distinct方法[全屏看文] Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1&q ...

  9. FontAwesome 图标 class="fa fa-home"

    本文转自:http://www.yeahzan.com/fa/facss.html 引用方式: class="fa fa-home" 注意:每个图标的引用都必须要添加 fa fa- ...

  10. 9、搜索 :ion-searchbar

    /* ---html----*/ <ion-searchbar [(ngModel)]="searchQuery" (input)="getItems($event ...