Longest Subsequence codeforces 632D 暴力数学

来源:互联网 发布:魔域单机登录器源码 编辑:程序博客网 时间:2024/05/21 12:00

ou 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

题意:求一个最长子序列使得序列中的数的lcm小于m

分析:因为m的大小为 1e6.
所以可以暴力 最小公倍数肯定是比得到的序列中的任何一个数都要小,所以i把能到的倍数加上i的数量就好

#include <bits/stdc++.h>using namespace std;const int maxn=1e6+10;int a[maxn];int num[maxn];int sum[maxn];int main(){    int n,m;    cin>>n>>m;    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i]);        if(a[i]<=m)            num[a[i]]++;    }    for(int i=1;i<=m;i++)    {        if(num[i])        {            for(int j=i;j<=m;j+=i)                sum[j]+=num[i];        }    }    int maxx=0,maxval=0;    for(int i=1;i<=m;i++)    {        if(sum[i]&&maxx<sum[i])        {            maxval=i;            maxx=sum[i];        }    }    if(maxx==0)        printf("1 0\n");    else    {        int cnt=0;        printf("%d %d\n",maxval,maxx );        for(int i=1;i<=n;i++)        {            if(maxval%a[i]==0)            {                if(cnt)                    printf(" ");                printf("%d",i );                ++cnt;            }        }        printf("\n");    }}
原创粉丝点击