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

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

题目的意思是输入n个数和一个m,求n个数里面能组成小于m的最小公倍数的用到数字最多是多少,并输出最小公倍数,用到的数的量,及每一个数的下标;

开始想想毫无头绪,但发现m比较小,才1e6,就想到开一个p[i]数组记录数字i作为最小公倍数会用到多少数字,先记下原序列中每个值出现的次数,然后k倍的这个值加上次数,因为很明显的是以a为因子的最小公倍数一定在ka中,然后搜一边p数组找到最大值就好了,要注意的是要取第一个最大值,因为答案的最小公倍数的整数倍也会是最大值。
最后在遍历一遍输出下标







#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f3f int n,m;
int a[1000006];
int p[1000006];
int cnt[1000006]; int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(cnt,0,sizeof(cnt));
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]<=m)
{
cnt[a[i]]++;
}
} for(int i=1; i<=m; i++)
for(int j=1; j*i<=m; j++)
{
p[i*j]+=cnt[i];
} int mx=-1;
int u=-1;
for(int i=1;i<=m;i++)
{
if(mx<p[i])
{
u=i;
mx=p[i];
}
}
printf("%d %d\n",u,mx);
int q=0;
for(int i=1;i<=n;i++)
{
if(u%a[i]==0)
{
if(q++)
printf(" ");
printf("%d",i);
}
}
printf("\n"); } return 0;
}




Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏的更多相关文章

  1. NYOJ-73 比大小 AC 分类: NYOJ 2014-01-17 21:29 195人阅读 评论(0) 收藏

    典型的大数题目,这只是大数的比较,到时还有大数加减乘除,更加还有乘方,对于大数,一般用数组或者字符串,因为其他的结构类型一般都没有那么大 的范围!! 这道题目需要你仔细回想怎么比较俩个数字的大小,考虑 ...

  2. hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏

    bit masking is very common on the lower level code. #include <cstdio> #include <algorithm&g ...

  3. Hadoop集群日常运维 分类: A1_HADOOP 2015-03-01 21:26 502人阅读 评论(0) 收藏

    (一)备份namenode的元数据 namenode中的元数据非常重要,如丢失或者损坏,则整个系统无法使用.因此应该经常对元数据进行备份,最好是异地备份. 1.将元数据复制到远程站点 (1)以下代码将 ...

  4. codeforces 678C. Joty and Chocolate(容斥) 2016-10-15 21:49 122人阅读 评论(0) 收藏

    C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...

  7. C++ Virtual介绍 分类: C/C++ 2015-06-16 21:36 26人阅读 评论(0) 收藏

    参考链接:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html 学过C++的人都知道在类Base中加了Virtual关键字的函 ...

  8. shell入门之函数应用 分类: 学习笔记 linux ubuntu 2015-07-10 21:48 77人阅读 评论(0) 收藏

    最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...

  9. Codeforces 766D Mahmoud and a Dictionary 2017-02-21 14:03 107人阅读 评论(0) 收藏

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. Haskell语言学习笔记(62)Divisible

    Divisible class Contravariant f => Divisible f where divide :: (a -> (b, c)) -> f b -> f ...

  2. VB.net 与 C# 的对应逻辑运算符

    And:对两个Boolean表达式执行逻辑和.AndAlso:与AndAlso类似,关键差异是AndAlso显示短路行为,如果AndAlso中的第一个表达式为False,则不计算第二个表达式.Or:对 ...

  3. JS中创建对象的方法及json

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 新手C#SQLServer在程序里实现语句的学习2018.08.12

    从C#中连接到SQL Server数据库,再通过C#编程实现SQL数据库的增删改查. ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据 ...

  5. select 1 与 select null (转)

    1.Select 1 在这里我主要讨论的有以下几个select 语句: table表是一个数据表,假设表的行数为10行,以下同. 1:select  1 from table 2:select cou ...

  6. Subquery typo with using in(转)

    Subquery typo with using in Do you use the following syntax?   SELECT * FROM TABLE WHERE COLUMN IN ( ...

  7. ALTER数据库

    alter table dbo.Sheet1$ alter column UserId int null

  8. acceleration

    acceleration - Bing dictionary US[ək.selə'reɪʃ(ə)n]UK[ək.selə'reɪʃ(ə)n] n.加速度:加快:(车辆)加速能力 网络促进:加速力:加 ...

  9. win10下docker安装和配置镜像仓库

    初学docker记录一下流程 1.首先安装直接官网下载 DockerToolbox 即可,安装过程傻瓜式下一步即可.(这个集成了虚拟机,果然安装过的可以去掉) 2.安装好后双击Docker Quick ...

  10. sublime Text与python3的中文编码错误解决办法

    在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal no ...