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. 洛谷【P1100】高低位交换

    二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P11 ...

  2. 洛谷【P1104】生日(插入排序版)

    题目传送门:https://www.luogu.org/problemnew/show/P1104 题目很简单,我主要是来讲插入排序的. 所谓插入排序,就是从待排序数组不断将数据插入答案数组里. 假设 ...

  3. 一个能获取如果hash或search是中文的内容小例子

    代码: (function () { var url = "http//baidu.com#a=你好&b=world"; var url1 = "http//ba ...

  4. C# 判断路径和文件存在

    1.判断路径是否存在,不存在则创建路径: if (!System.IO.Directory.Exists(@"D:\Export")) { System.IO.Directory. ...

  5. 15 Practical Grep Command Examples In Linux / UNIX

    You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ...

  6. [原创]SQL表值函数:获取从当月计算起往前自定义月份数

      今天我现在发现看一篇博文不能够太长,只要能够描述清楚自己想表达的东西,能够让大家知道你要讲什么就行了.因为我今天看了一些长篇博文,真的觉得知识点太多了, 会让人囫囵吞枣. 这篇博文跟我昨天发表的类 ...

  7. 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  8. 27、BLASTN的参数

    转载:http://blog.sina.com.cn/s/blog_5ecfd9d90100ksui.html http://www.docin.com/p-704735699.html 与之前的bl ...

  9. netty+mqtt

    package io.mqtt.server; import io.mqtt.tool.ConfigService;import io.netty.bootstrap.ServerBootstrap; ...

  10. Asp.Net 之 Web.config 配置文件详解 -转

    在asp.net中配置文件名一般默认是web.config.每个web.config文件都是基于XML的文本文件,并且可以保存到Web应用程序中的任何目录中.在发布Web应用程序时web.config ...