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相等. ...
随机推荐
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
- css关于内外边距的详细解释
贴图吧,图一眼明了. 无效果时候有 只有一个<div>啦啦啦</div> 只有padding时候有: padding详细设计时: 解释:padding是对内的,如padding ...
- python操作sql server2008 pyodbc
使用Python通过PyODBC连接数据的注意事项 今天使者用PyODBC连接数据库,试了很久才出来,现把一些心得体会和大家分享! 一.PyODBC的下载地址: http://code.google. ...
- JavaScript之使用JavaScript模仿oop编程
第一, 首先,使用关键字function定义一个类 function Shape1(ax,ay) {//此时将function看成声明类的标志 ; ; var init=function () {// ...
- ViewController lifecyle(IOS学习)
斯坦福的ios教学视频笔记一张,如下
- Arcane Numbers 1
Vance and Shackler like playing games. One day, they are playing a game called "arcane numbers& ...
- HDFS追加文件
配置:hdfs-site.xml <property> <name>dfs.support.append</name> <value>true</ ...
- 阶段3-团队合作\项目-网络安全传输系统\sprint0-产品规划与设计\第2课-产品功能模型设计
- opengl 和 vs2010的安装配置
1.先安装vs2010, 输个激活秘钥 2.Windows 7下OpenGL如何配置,配置glut: http://jingyan.baidu.com/article/b0b63dbfd1c08 ...
- C#对图片进行切割
C#实例代码: /// <summary> /// 切割图片 /// </summary> /// <param name="sourceBitmap" ...