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相等. ...
随机推荐
- 洛谷【P1100】高低位交换
二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P11 ...
- Python 写文件时的Unicode设置
今天在把Evenote的笔记内容写为文件时出错: f.write(content) UnicodeEncodeError: 'gbk' codec can& ...
- HDOJ1728(限制转弯的迷宫问题)
用bfs进行深搜,求出每个可达点的最小转弯数 #include<cstdio> #include<cstring> #include<queue> using na ...
- JINKENS
https://www.cnblogs.com/ceshisanren/p/5639869.html
- Java常见设计模式之观察者模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述观察者(Observer)模式的: 观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Mo ...
- JS取得绝对路径
在项目中,我们经常要得到项目的绝对路径,方便我们上传下载文件,JS为我们提供了方法,虽说要迂回一下.代码如下: function getRealPath(){ //获取当前网址,如: h ...
- centos7安装与配置ansible
此次测试总共有三台机,分别如下: ansible服务器:10.0.0.20 client01:10.0.0.21 client02:10.0.0.22 一.安装ansible 1. python版本需 ...
- centos 7之文件共享
一,安装过程 1.在VirtualBox的软件菜单里面选择 “设备”--“存储”,添加VBoxGuestAdditions.iso(在VirtualBox目录下). 2.在桌面上出现一个光驱 ...
- 6.7 root和user之间的切换
- 教育网bt站点
北京交通大学 晨光BT (http://cgbt.cn)清华晨光BT(http://thubt.cn)北京科技大学 iBeiKeBT(http://bt.ibeike.com)上海大学 乐乎BT (h ...