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 ...
随机推荐
- JAVA中的设计模式二(工厂模式)
工厂模式:主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类. 工厂模式主要有: 简单工厂模式,工厂方法,抽象工厂: 简单工厂: 又叫静态工厂,是工厂模式三中状态中结构最为简单的.主 ...
- Udacity并行计算课程笔记-The GPU Hardware and Parallel Communication Patterns
本小节笔记大纲: 1.Communication patterns gather,scatter,stencil,transpose 2.GPU hardware & Programming ...
- Xamarin android如何反编译apk文件
Xamarin android 如何反编译 apk文件 这里推荐一款XamarinAndroid开发的小游戏,撸棍英雄,游戏很简单,的确的是有点大.等一下我们来翻翻译这个Xamarin Android ...
- [UWP]了解IValueConverter
1. 前言 IValueConverter是用于数据绑定的强大的武器,它用于Value在Binding Source和Binding Target之间的转换.本文将介绍IValueConverter的 ...
- .net 连接SqlServer数据库及基本增删改查
一.写在前面 因为这学期选修的 .net 课程就要上机考试了,所以总结下.net 操作 SqlServer 数据的方法.(因为本人方向是 Java,所以对.net 的了解不多,但以下所写代码均是经过测 ...
- inode 详解
1.inode 解析: 存储文件元信息(文件创建者,创建日期,大小等)的区域叫做inode即 索引节点. 2.inode 内容: 文件字节数.拥有者UserID,GroupID,读写执行权限,时间戳, ...
- AdaBoost对实际数据分类的Julia实现
写在前面 AdaBoost是机器学习领域一个很重要很流行的算法,而Julia是一门新兴的发展迅速的科学计算语言.本文将从一个实际例子出发,展示如何用Julia语言实现AdaBoost算法. 什么是Ad ...
- 【转】String Date Calendar之间的转换
1.Calendar 转化 String Calendar calendat = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDa ...
- ASP.NET Core使用静态文件、目录游览与MIME类型管理
前言 今天我们来了解了解ASP.NET Core中的静态文件的处理方式. 以前我们寄宿在IIS中的时候,很多静态文件的过滤 和相关的安全措施 都已经帮我们处理好了. ASP.NET Core则不同,因 ...
- thinkphp5使用PHPExcel导入Excel数据
安装PHPExcel扩展类 地址:https://github.com/PHPOffice/PHPExcel ①通过composer安装 ②手动下载, 放在项目的extend目录下 代码中引入 由于P ...