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 ...
随机推荐
- MySQL操作时间的函数集
求两个Timestamp之间的秒差值: select TIMESTAMPDIFF(SECOND,TIMESTAMP("2017-03-01 07:58:20"),timestamp ...
- Oracle初级——续续篇
逝者如斯夫,不舍昼夜 所有的SQL都经过测试,可粘贴,可复制,有问题请各位大神指出...... --约束 与表一起使用 约束不合理的数据,使其不能进入表中? ','李小龙','一班','该学生成天练武 ...
- SSL/TLS通信
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/31 复习基本概念 对称密码:加密和解密使用同一密匙. 公钥密码: ...
- 1-MySQL数据库(android连接MySQL数据库)
很好的链接 http://www.cnblogs.com/best/p/6517755.html 一个小时学会MySQL数据库 http://www.cnblogs.com/klguang/p/47 ...
- bzoj 4765: 普通计算姬
Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能计算数列区间和,而普通计算姬能计算树中 ...
- mysql 错误信息
1 连接MySQL错误:Can't connect to MySQL server (10060) link:>>> http://blog.csdn.net/testcs_dn/ ...
- Python新手需要掌握的知识点
一.基础语法 1 变量 2 逻辑判断 3 循环 4 函数 二.数据结构 1 数字(加减乘除) 2 字符串(一串字符) 3 布尔 (真假) 4 元组 (不能修改的列表) 5 列表(Python的苦力,最 ...
- js 判断值为Array or Object的方法
①obj instanceof Array / Object ②Array.prototype.isPrototypeOf(obj) ③Object.prototype.toString.call(o ...
- [Spark内核] 第28课:Spark天堂之门解密
本課主題 什么是 Spark 的天堂之门 Spark 天堂之门到底在那里 Spark 天堂之门源码鉴赏 引言 我说的 Spark 天堂之门就是SparkContext,这篇文章会从 SparkCont ...
- 第二章:Python基础の快速认识基本数据类型和操作实战
本课主题 字符串和操作实战 二进制操作实战 List 列表和操作实战 Tuple 元組和操作实战 Dict 字典和操作实战 作業需求 引言 这遍文章简单介绍了 Python 字符串和集合的方法和应用, ...