题意:给你n个数,和一个数m, 问最小需要多少个数,可以让这些数乘起来是m的倍数。如果有多组,取和最小的那一组。

思路:因为m的范围到1e12,并且和取模相关,所以容易想到处理出m的约数,然后离散化一下,降低DP的第二维的复杂度,因为如果这些数的乘积不是m的约数,就没有意义了。dp[i][j]表示处理到第i个数,约数是j的最小个数。dp需要存pair,因为要求个数一样的时候和最小。可以提前把a和m求gcd以降低复杂度,注意特判m为1的情况。

代码:

#include <bits/stdc++.h>
#define LL long long
#define pii pair<LL, LL>
#define INF 1e16
using namespace std;
const int maxn = 1010;
pii dp[maxn][7010];
map<LL, int> mp;
LL f[7010], tot;
int n;
LL m, a[maxn], b[maxn];
void init(LL x) {
for (LL i = 1; i * i <= x; i++) {
if(x % i == 0) {
f[++tot] = i;
if(i * i != x)
f[++tot] = x / i;
}
}
sort(f + 1, f + 1 + tot);
for (int i = 1; i <= tot; i++)
mp[f[i]] = i;
}
int main() {
scanf("%d%lld", &n, &m);
init(m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
b[i] = __gcd(a[i], m);
}
if(m == 1) {
LL ans = 1e15, pos = 0;
for (int i = 1; i <= n; i++) {
if(a[i] < ans) {
ans = a[i];
pos = i;
}
}
printf("%d\n%lld\n", 1, pos);
return 0;
}
for (int i = 2; i <= tot; i++)
dp[0][i] = pii(INF, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= tot; j++) {
dp[i][j] = dp[i - 1][j];
int pos = mp[f[j] / __gcd(f[j], b[i])];
pii tmp = pii(dp[i - 1][pos].first + 1, dp[i - 1][pos].second + a[i]);
dp[i][j] = min(dp[i][j], tmp);
}
}
if(dp[n][tot].first > n) printf("-1\n");
else {
printf("%lld\n", dp[n][tot].first);
LL now = f[tot], pos = tot;
for (int i = n; i >= 1; i--) {
if(dp[i][pos] != dp[i - 1][pos]) {
printf("%d ", i);
now /= __gcd(b[i], now);
pos = mp[now];
}
}
}
}

  

Codeforces 703E DP + 因数分解 +离散化的更多相关文章

  1. codeforces 703E Mishka and Divisors

    codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...

  2. CodeForces - 55D(数位dp,离散化)

    题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as ...

  3. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  4. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  5. 洛谷2344 奶牛抗议(DP+BIT+离散化)

    洛谷2344 奶牛抗议 本题地址:http://www.luogu.org/problem/show?pid=2344 题目背景 Generic Cow Protests, 2011 Feb 题目描述 ...

  6. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

  7. Two Melodies CodeForces - 813D (DP,技巧)

    https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...

  8. Consecutive Subsequence CodeForces - 977F(dp)

    Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...

  9. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

随机推荐

  1. CentOS 6.8 源码安装mysql 5.6

    一:卸载旧版本 rpm -qa | grep mysql rpm -e mysql #普通删除模式 rpm -e --nodeps xxx(xxx为刚才的显示的列表) # 强力删除模式,如果使用上面命 ...

  2. 使用Costura.Fody将源DLL合并到目标EXE

    本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 一.本文主要是使用Costura.Fody工具将源DLL合并到目标EXE,因此,需要从以下任一链接下载: ①从Github地址 ...

  3. js各种效果

    1.JavaScript 仿LightBox内容显示效果 2.固定高度的div,竖直方向出现滚动条,水平方向固定 http://www.jb51.net/css/109928.html <!do ...

  4. [LeetCode系列]链表环探测问题II

    给定一个链表头, 探测其是否有环, 如果没有返回NULL, 如果有返回环开始的位置. 环开始的位置定义为被两个指针指向的位置. 算法描述: 1. 快慢指针遍历, 如果到头说明无环返回NULL, 如果相 ...

  5. 12.Python使用requests发送post请求

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  6. display的flex属性使用详解

    flex的兼容性在pc端还算阔以,但是在移动端,那就呵呵了.今天我们只是学习学习,忽略一些不重要的东西. 首先flex的使用需要有一个父容器,父容器中有几个items. 父容器:container 属 ...

  7. happynear_caffe编译时,缺少头文件caffe.pb.h的问题

    由于一些问题,需要编译caffe 的windows版本,用的是happynear的caffe版本,在caffe.pb.h遇到了问题 如何生成 caffe.pb.h 将protobuf 里的 proto ...

  8. 如何使用indexdb

    一.实现步骤 1)获得indexedDB对象 if (!window.indexedDB) { window.indexedDB = window.mozIndexedDB || window.web ...

  9. Android 语音处理

    开源的sip android 项目 https://code.google.com/p/csipsimple/

  10. AngularJS.js: 杂项

    ylbtech-AngularJS.js: 杂项 AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多 ...