D - 稳住GCD DP
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的更多相关文章
- UESTC 923 稳住GCD DP + GCD
定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加, dp[i][j] ...
- [原]携程预选赛A题-聪明的猴子-GCD+DP
题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 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 ...
- hdu CA Loves GCD(dp)
一道我想骂人的题,差点把我气炸了. 题意: 求一个数的集合中(非多重集,每个数只出现一次)所有子集的gcd的和.结果MOD10^8+7输出. 输入输出不说了,自己看吧,不想写了. 当时我真把它当作数论 ...
- Xtreme8.0 - Play with GCD dp
Play with GCD 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-g ...
- 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 ...
- hdu-5656 CA Loves GCD(dp+数论)
题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 5656 ——CA Loves GCD——————【dp】
CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- [HDOJ5726]GCD(RMQ,二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:给定数列,求区间[L,R]的GCD的值,并求出有多少个子区间满足和[L,R]的GCD相等. ...
随机推荐
- Floyd-Warshall算法:求结点对的最短路径问题
Floyd-Warshall算法:是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径问题,同时也被用于计算有向图的传递闭包. 原理: Floyd-Warshall算法的原理是动态 ...
- POJ 2017 No Brainer(超级水题)
一.Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indi ...
- 微服务理论之六:ESB与SOA的关系
一.SOA和ESB一直是没有明确概念的两个缩略词 SOA----面向服务架构,实际上强调的是软件的一种架构,一种支撑软件运行的相对稳定的结构,表面含义如此,其实SOA是一种通过服务整合来解决系统集成的 ...
- telnet IP:ERROR
实验环境:CentOS6.8 主机:172.16.xxx.xxx:80 客户端:172.16.xxx.xxx [root@www ~18:32:27]#telnet 172.16.xxx.xxx 80 ...
- KMeans的数据压缩
import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.u ...
- URL网址规范化
网址URL规范化(URL canonicalization)是近一年来在Google搜索结果中出现的一个很大的问题.它指的是搜索引擎挑选最好的URL网址作为真正网址的过程.举例来说,下面这几个URL一 ...
- Learning Python 011 高级特性 2
Python 高级特性 2 列表生成式 列表生成式就是指类似这样的代码:[x for x in range(1, 11)] >>> L = [x for x in range(1, ...
- ARC097D Equals
传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint4-嵌入式web服务器开发\第3课-CGI程序开发
实现CGI程序显示一幅图片最核心的功能 把上一节课编写好的led.c程序拷贝过来,并重新命名为image.c 把led的某些部分删除,后如下 那么如何显示一幅图片呢,百度(搜索在html里面去插入图片 ...
- node安装和配置
windows 环境 安装node node下载地址 下载后点击安装,默认下一步即可(安装路径可更改为d:盘) 检测PATH环境变量是否配置了Node.js 点击开始=>运行=>输入&qu ...