Array GCD CodeForces - 624D (dp,gcd)
大意: 给定序列, 给定常数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)的更多相关文章
- Array Beauty CodeForces - 1189F (dp,好题)
大意: 定义$n$元素序列$a$的美丽度为 $\min\limits_{1\le i<j\le n}|a_i-a_j|$. 给定序列$a$, 求$a$的所有长为$k$的子序列的美丽度之和. 记 ...
- CodeForces - 803C Maximal GCD 【构造】
You are given positive integer number n. You should create such strictly increasing sequence of k po ...
- HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)
树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/6 ...
- iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组
一.GCD的基本使用 <1>GCD简介 什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 GCD的优势 G ...
- 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- ...
- 【CodeForces 624D】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- 【CodeForces 624D/623B】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- Recovering BST CodeForces - 1025D (区间dp, gcd)
大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...
- UESTC 923 稳住GCD DP + GCD
定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加, dp[i][j] ...
随机推荐
- 抱歉最近朋友结婚去浪了几天~未来几天会正常更新.今天带来XML文件解析
三种解析方法 DOM,SAX,XMLPullParse;你以为我要讲三种嘛 错 ,我只讲一种 ,其他两种我只是说下优缺点, 一.DOM解析器 优点: DOM解析器在解析XML文档时,会把文档中的所有元 ...
- 牛客网Java刷题知识点之ArrayList 、LinkedList 、Vector 的底层实现和区别
不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...
- java将list分为指定大小的新集合
上代码: import java.util.ArrayList; import java.util.List; public class JayCommonUtil { /** * 按指定大小,分隔集 ...
- pat1014. Waiting in Line (30)
1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- angular2 遇到的问题汇总
angular2 学习资源集锦:https://github.com/timjacobi/angular2-education 在学习angular开发项目过程遇到的问题: 1. 不同componen ...
- select操作大全
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- Table 边框合并(collapse)
border-collapse:collapse 用于表格属性, 表示表格的两边框合并为一条; <style type="text/css"> table { bord ...
- jsp的动作标签
常用的标签: 1. forward 请求转发 [基本不使用] <==> request.getRequestDispatcher(url).forward(request,respon ...
- [MySQL] - MySQL连接字符串总结
来源:http://blog.sina.com.cn/s/blog_5f0dab1e0100e4pv.html?retcode=0 一.MySQL Connector/ODBC 2.50 (MyODB ...
- 从零开始的全栈工程师——ajax
AJAX AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX = Asynchronous JavaScript and XML. AJAX 是一种用于创建快速动态网页 ...