http://acm.uestc.edu.cn/#/problem/show/923

给定一堆数字,求其所有数字的gcd。

现在要删除最多的数字,使得剩下的数字的gcd和原来的一样。

设dp[i][val]表示在前i个数中,得到val这个数字所需的最小数字,怎么得到val这个数字?就是gcd得到。

dp[i][gcd]

然后转移就是

dp[i][a[i]] = 1是必然的,自己一个

枚举新数字得到新的gcd  val

dp[i][val] = min(dp[i][val], dp[i - 1][j]  + 1)

每次都要和自己比较一下,因为防止自己更小,就是dp[i][val]本来就是1,这样就不需要枚举其他数加入来了,。

//#include<stdio.h>
//int main() {
// int i;
// char a[10][3]= {"一","和","三","四","物","社","嗯","阶","己","嗯"};
// for(i=0; i<10; i++)
// printf("%s\n",a[i]);
// return 0;
//} #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int dp[ + ][ + ];
int a[ + ];
void work() {
int n;
cin >> n;
for (int i = ; i <= n; ++i) {
cin >> a[i];
}
int ans = a[];
for (int i = ; i <= n; ++i) {
ans = __gcd(ans, a[i]);
}
memset(dp, 0x3f, sizeof dp);
dp[][a[]] = ;
for (int i = ; i <= n; ++i) {
dp[i][a[i]] = ; //自己一直出现,一次就够
for (int j = ; j <= ; ++j) {
if (dp[i - ][j] != inf) {
dp[i][j] = min(dp[i][j], dp[i - ][j]);
int t = __gcd(j, a[i]);
dp[i][t] = min(dp[i][t], dp[i - ][j] + );
}
}
}
cout << n - dp[n][ans] << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

D - 稳住GCD DP的更多相关文章

  1. UESTC 923 稳住GCD DP + GCD

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

  2. [原]携程预选赛A题-聪明的猴子-GCD+DP

    题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

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

  4. hdu CA Loves GCD(dp)

    一道我想骂人的题,差点把我气炸了. 题意: 求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和.结果MOD10^8+7输出. 输入输出不说了,自己看吧,不想写了. 当时我真把它当作数论 ...

  5. Xtreme8.0 - Play with GCD dp

    Play with GCD 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-g ...

  6. HDU 5656 CA Loves GCD dp

    CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...

  7. hdu-5656 CA Loves GCD(dp+数论)

    题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Ot ...

  8. HDU 5656 ——CA Loves GCD——————【dp】

    CA Loves GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  9. [HDOJ5726]GCD(RMQ,二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:给定数列,求区间[L,R]的GCD的值,并求出有多少个子区间满足和[L,R]的GCD相等. ...

随机推荐

  1. C#中如何获取汉字的笔画数和汉字的拼音

    以前玩过一个游戏,输入两个人的名字然后点击缘分就能产生一段缘分测试的结果,后来经过分析知道是根据名字笔画数之差来弄的小游戏,于是就在百度上找怎么得到汉字的笔画数,也没找到自己想要的答案,问遍了所有的人 ...

  2. puppet插件fact和hiera(puppet自动化系列3)

    四.Fact插件 4.1 使用pluginsync进行发布 这种方法比较特殊,节点factpath目录里除了编写好的rb文件之外,还需要在puppet模块中引用,运行一次之后才会转换成fact.通常在 ...

  3. 关于使用C# 启动msi失败的问题

    原以为在启动msi是件小儿科的事,上代码: ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "C:\\myTest ...

  4. cmake opencv,dlib 编译静态库 1

    无论windows,linux 所有的库 ,dlib,opencv 通过cmake-gui 设置好静态库, 动态库,和其他各个选项 Tips: cmake 优先级用cmake-gui,因为命令太多,容 ...

  5. rails中一个窗体多个模型——fields_for

    详细参考 http://railscasts.com/episodes/73-complex-forms-part-1中part-1.2.3部分 借助field_for可以生成表单来处理两个或更多模型 ...

  6. ABP源码学习目录

    ABP源码理解笔记 之前看过abp源码,但是时间久了很多也不记得了,所以近期打算重新看一遍,顺便做下笔记. 目录如下: Abp 框架启动流程分析 模块系统 依赖注入 模块配置 系统设置 工作单元的实现 ...

  7. linux命令-vim命令模式

    编辑模式下 按 “esc” 进去命令模式 :wq 保存并退出 :w  保存 已写入 :q!不保存强制退出 :wq! 强制保存退出 :set nu 显示行号 :set nonu 去掉行号 :nohl 取 ...

  8. js中的Number方法

    1.Number.toExponential(fractionDigits) 把number转换成一个指数形式的字符串.可选参数控制其小数点后的数字位数.它必须在0~20之间. 例如: documen ...

  9. Elasticsearch之curl创建索引库

    关于curl的介绍,请移步 Elasticsearch学习概念之curl 启动es,请移步 Elasticsearch的前后台运行与停止(tar包方式) Elasticsearch的前后台运行与停止( ...

  10. Hadoop 1.2.1 MapReduce 例子

    自学hadoop真的很困难,主要是hadoop版本太混乱了,各个版本之间兼容性并不算太好.更主要的是网上的很多MapReduce的Java例子不写import!!!只写类名!!!偏偏Hadoop中有很 ...