D. Longest Subsequence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 10^6) — the size of the array a and the parameter from the problem statement.

The second line contains n integers ai (1 ≤ ai ≤ 10^9) — the elements of a.

Output

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.

Examples
input
7 8
6 2 9 2 7 2 3
output
6 5
1 2 4 6 7
input
6 4
2 2 2 3 3 3
output
2 3
1 2 3
一句话题意:

分析:对于一个数k,它的所有约数的lcm肯定不大于k,也就是说对于每一个[1,m]中的数,我们只需要求出它的约数是a中的数的个数就好了,难道又要用筛法或质因数分解吗?其实不必,我们可以想到一个O(nm)的做法:枚举[1,m]中的每一个数,然后看看a中有多少个数整除它,显然T掉,能不能改进一下呢?
对于枚举的优化,要么是减少枚举层数,要么是减少对答案没有贡献的枚举,我们可以只用枚举整除数i的数,然后看看a中有没有这个数,这个操作可以用一个vis数组,只需要O(1)的时间查询。不过ai <= 10^9,我们似乎开不下这么大的vis数组,观察题目,发现m只有10^6,大于10^6的数我们可以不用考虑.
其实相对于枚举约数,枚举倍数更容易,我们从一个数扩展,将它的倍数的计数器累加即可,其实两种方法都可以.
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int maxn = ; int num[maxn], cnt[maxn],tot,a[maxn],ans,id,vis[maxn];
int n, m; int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
int t;
scanf("%d", &t);
a[i] = t;
if (t > m)
continue;
++num[t];
}
for (int i = ; i <= n; i++)
{
if (!vis[a[i]])
for (int j = a[i]; j <= m; j += a[i])
cnt[j] += num[a[i]];
vis[a[i]] = ;
}
for (int i = ; i <= m; i++)
if (cnt[i] > ans)
{
ans = cnt[i];
id = i;
}
printf("%d %d\n", id, ans);
for (int i = ; i <= n; i++)
if (id % a[i] == )
printf("%d ", i); return ;
}
      

CF632D Longest Subsequence的更多相关文章

  1. D. Longest Subsequence

    D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. 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 ...

  3. Educational Codeforces Round 9 D - Longest Subsequence

    D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...

  4. Educational Codeforces Round 9 D. Longest Subsequence dp

    D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...

  5. [徐州网络赛]Longest subsequence

    [徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...

  6. codeforces632D. Longest Subsequence (最小公倍数)

    You are given array a with n elements and the number m. Consider some subsequence of a and the value ...

  7. codeforces 632D. Longest Subsequence 筛法

    题目链接 记录小于等于m的数出现的次数, 然后从后往前筛, 具体看代码. #include <iostream> #include <vector> #include < ...

  8. CodeForces 632D Longest Subsequence

    暴力. 虽然$a[i]$最大有${10^9}$,但是$m$最大只有${10^6}$,因此可以考虑暴力. 记$cnt[i]$表示数字$i$有$cnt[i]$个,记$p[i]$表示以$i$为倍数的情况下, ...

  9. hihoCoder week227 Longest Subsequence

    题目链接 https://hihocoder.com/contest/hiho227/problem/1 题目详解 https://hihocoder.com/discuss/question/558 ...

随机推荐

  1. 样式缩写——css技巧(一)

    一.margin和padding缩写 例: .sample-margin1{ margin-top:15px; margin-right:20px; margin-bottom:12px; margi ...

  2. HDU 4707 Pet 邻接表实现

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 解题报告:题目大意是在无向图G中有n个点,分别从0 到n-1编号,然后在这些点之间有n-1条边, ...

  3. GDB基本用法

    基本命令 进入GDB:#gdb test test是要调试的程序,由gcc test.c -g -o test生成.进入后提示符变为(gdb) . 查看源码:(gdb) l 源码会进行行号提示. 如果 ...

  4. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:Frequent Subsets Problem (状态压缩)

    题目链接 题目翻译: 给出一个数n,和一个浮点数a,数n代表全集U = {1,2,...,n},然后给出 M个U的子集,如果一个集合B(是U的子集),M个集合中有至少M*a个集合包含B, 则B这个集合 ...

  5. glut glew区别

    GLEW是一个跨平台的C++扩展库,基于OpenGL图形接口.使用OpenGL的朋友都知道,window目前只支持OpenGL1.1的涵数,但 OpenGL现在都发展到2.0以上了,要使用这些Open ...

  6. Servlet笔记4--ServletConfig接口和ServletContext接口

    ServletConfig接口: ServletContext接口: 代码详解: (1)web.xml配置文件: <?xml version="1.0" encoding=& ...

  7. 禅道CMS 获文件名脚本

    use Net::HTTP::GET; use Base64; ; windowWidth=; windowHeight=; sid=jg9g2mk5kmru46lmd3g2evoc87>; # ...

  8. RW RO ZI ROM keil中的含义

    编译的一个ARM的程序,会得到这样的信息: ============================================================================== ...

  9. Vagrant 无法校验手动下载的 Homestead Box 版本

    起因 4年前电脑,配置不太好了,现有的 Homestead 运行起来太吃内存.在修改了 Homestead.yaml 文件里 memory 选项的内存配置为 1024 后,应用最新配置重启失败. 索性 ...

  10. 下一代Android打包工具,100个渠道包只需要10秒钟 https://github.com/mcxiaoke

    https://github.com/mcxiaoke/packer-ng-plugin https://github.com/Meituan-Dianping/walle https://githu ...