D. Longest Subsequence
2 seconds
256 megabytes
standard input
standard output
You are given array a with
n elements and the number m. Consider some subsequence of
a and the value of least common multiple (LCM) of its elements. Denote LCM as
l. Find any longest subsequence of
a with the value l ≤ m.
A subsequence of a is an array we can get by erasing some elements of
a. It is allowed to erase zero or all elements.
The LCM of an empty array equals 1.
The first line contains two integers n and
m (1 ≤ n, m ≤ 106) — the size of the array
a and the parameter from the problem statement.
The second line contains n integers
ai (1 ≤ ai ≤ 109) — the elements of
a.
In the first line print two integers l and
kmax (1 ≤ l ≤ m, 0 ≤ kmax ≤ n) — the value of LCM and the number of elements
in optimal subsequence.
In the second line print kmax integers — the positions of the elements from the optimal subsequence in the ascending order.
Note that you can find and print any subsequence with the maximum length.
7 8
6 2 9 2 7 2 3
6 5
1 2 4 6 7
6 4
2 2 2 3 3 3
2 3 1 2 3/*题目大意:给定n大小的数组a,求数组a的最长子序列的最小公倍数不超过m。要求输出其满足要求的任意子序列
*算法分析:先将小于等于m的数择出来,然后从后向前筛
*/
#include <bits/stdc++.h>
using namespace std; typedef long long int llint;
const int maxn = 1e6 + 100;
llint a[maxn], b[maxn]; int main() {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
llint n, m, flag = 0;
scanf("%I64d%I64d",&n, &m);
for (llint i = 0; i<n; i++) {
scanf("%I64d",&a[i]);
if (a[i] <= m) {
b[a[i]] ++ ;
flag = 1;
}
}
if (!flag) cout << "1 0" << endl;
else {
for (llint i = m; i>=1; i--) {
for (llint j = 2*i; j<=m; j+=i) {
b[j] += b[i];
}
}
llint max = 0, lcm = 0;
for (llint i = 1; i<=m; i++) {
if (b[i] > max) {
max = b[i];
lcm = i;
}
}
cout << lcm << " " << max << endl;
for (llint i = 0; i<n; i++) {
if (lcm%a[i] == 0) cout << i+1 << " " ;
}
cout << endl;
}
return 0;
}
D. Longest Subsequence的更多相关文章
- Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏
D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Educational Codeforces Round 9 D - Longest Subsequence
D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...
- CF632D Longest Subsequence
D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
- [徐州网络赛]Longest subsequence
[徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...
- codeforces632D. Longest Subsequence (最小公倍数)
You are given array a with n elements and the number m. Consider some subsequence of a and the value ...
- codeforces 632D. Longest Subsequence 筛法
题目链接 记录小于等于m的数出现的次数, 然后从后往前筛, 具体看代码. #include <iostream> #include <vector> #include < ...
- CodeForces 632D Longest Subsequence
暴力. 虽然$a[i]$最大有${10^9}$,但是$m$最大只有${10^6}$,因此可以考虑暴力. 记$cnt[i]$表示数字$i$有$cnt[i]$个,记$p[i]$表示以$i$为倍数的情况下, ...
- hihoCoder week227 Longest Subsequence
题目链接 https://hihocoder.com/contest/hiho227/problem/1 题目详解 https://hihocoder.com/discuss/question/558 ...
随机推荐
- Python学习日记:day5-------dict字典
#字典dict------->唯一的映射类型 1.数据类型的划分 数据类型划分为可变数据类型和不可变数据类型. 不可变数据类型:tupe(元组).bool.int.str 可 ...
- Inception使用详解
一.Inception简介一款用于MySQL语句的审核的开源工具,不但具备自动化审核功能,同时还具备执行.生成对影响数据的回滚语句功能. 基本架构: 二.Inception安装 1.软件下载 下载链接 ...
- wincc flexable变量组态
1.变量分类 2.变量组态练习 3.变量组态之前新建一个设备连接取名connect 3.tag1组态bool类型,双击变量弹出下面窗口,具体如下图所示connect表示外部变量 4.组态tag2 5. ...
- xamarin android如何监听单击事件
在xamarin android单击事件是最基础的事情,看过菜鸟上的android教程时,java写的都是监听事件,为一个按钮,单选按钮.多选按钮的单击事件有三种,前面两种用的非常普遍,也很简易,我这 ...
- bzoj 2959: 长跑
Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能"为祖国健康工作五十年",同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上 ...
- Ryz的鬼题
蚂蚁(ant)[题目描述] 小 R 种了一棵苹果树,这棵树上有 n 个节点(标号从 0 到 n-1),有 n-1 条树枝连接这 n 个节点,这 n 个节点相互连通.每条树枝的长度为 1. 苹果树 ...
- class_copyIvarList方法获取实例变量问题引发的思考
在runtime.h中,你可以通过其中的一个方法来获取实例变量,那就是class_copyIvarList方法,具体的实现如下: - (NSArray *)ivarArray:(Class)cls { ...
- IDE 、SATA、SCSI 的区别
http://chuanwang66.iteye.com/blog/1134784 IDE IDE的英文全称为“Integrated Drive Electronics”,即“电子集成驱动器”,它的 ...
- 学习Object.assign()
Object.assign()用于将所有可枚举的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...source); var obj = { ...
- Linux 文本编辑器vi命令
1.Vim Vim 是一个功能强大的全屏幕文本编辑器,是 Linux/UNIX 上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim 没有菜单,只有命令 2.Vim 工作模式 3.插入 ...