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 ≤ 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.
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.
Example
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
从数组中挑选出尽可能多的数,保证其最小公倍数在m内。可以注意到此题m上限为10^6,也就是说最小公倍数不会超过这个值,一定程度上可以暴力。一开始只想得到暴力求解,1~m枚举最小公倍数再一个个检验是否整除,时间复杂度10^6*10^6显然不现实。在运算过程中可以发现,ka是a的倍数,下次枚举到(k+1)a时完全可以跳过计算。所以可以运用类似筛法的思想对数组进行预处理,a[i]在m范围内的倍数k*a[i]增加一次计数,意思是k*a[i]在这个数组里存在一个因数,预处理大约nlogm的复杂度,最后对公倍数的count数组比较最大值只需m的复杂度
要多留意数据的范围,m范围很小因此可以枚举公倍数,因数的个数也能用数组储存
#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iterator>
#include <string.h>
using namespace std;
typedef long long ll;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=0x3f3f3f3f;
const int sz=1000005;
int n,m;
int a[sz],cnt[sz],num[sz];
int main()
{
//freopen("r.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
scanf("%d",&m);
int ans=1,t=0,tmp;
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]<=m) num[a[i]]++;
}
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=m;i++){
if(num[i]==0) continue;
for(int j=i;j<=m;j+=i){
cnt[j]+=num[i];
//这一步是优化,没有会TLE,一百万个一百万内的数,其中肯定有重复,进一步压缩节约时间
}
}
for(int i=1;i<=m;i++){
if(cnt[i]>t){
t=cnt[i];
ans=i;
}
}
printf("%d %d\n",ans,t);
for(int i=1;t>0&&i<=n;i++){
if(a[i]<=ans&&ans%a[i]==0)
printf("%d ",i);
}
printf("\n");
}
return 0;
}