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. Loadrunner通过吞吐量计算每个用户需要的带宽

    Loadrunner通过吞吐量计算每个用户需要的带宽 运行一个场景,点击Analysis进行分析,使用分析报告中的Average Throughput(bytes/second)进行计算. 计算公式: ...

  2. C语言实现大数四则运算

    一.简介 众所周知,C语言中INT类型是有限制,不能进行超过其范围的运算,而如果采用float类型进行运算,由于float在内存中特殊的存储形式,又失去了计算的进度.要解决整个问题,一种解决方法是通过 ...

  3. pre换行段落间距

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

  4. 数字与字符串之间的转换以及%f与%lf的输入输出用法区别

    1.C++字符串与C字符串的转换: (1)string --> char * string str("OK"); strcpy(p,str.c_str());//p是char ...

  5. iOS 开发 需要的版本管理工具,UI图,bug管理工具等

    1.版本管理工具  或直接 终端敲命令SVN(smartSvn 或者cornerstone/终端)  或git (sourceTree/终端) 2. 原型管理工具 使用墨刀(https://modao ...

  6. 零基础学习hadoop到上手工作线路指导(编程篇)

    问题导读: 1.hadoop编程需要哪些基础? 2.hadoop编程需要注意哪些问题? 3.如何创建mapreduce程序及其包含几部分? 4.如何远程连接eclipse,可能会遇到什么问题? 5.如 ...

  7. php中的declare

    <?php // 事件的回调函数 function func_tick() { echo "call...\r\n"; } // 注册事件的回调函数 register_tic ...

  8. 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:

    1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...

  9. C#控制台自定义背景颜色,字体颜色大全

    效果: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  10. c3p0数据源配置

    Xml代码   <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: --> ...