Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏
2 seconds
256 megabytes
standard input
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.
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.
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.
7 8
6 2 9 2 7 2 3
6 5
1 2 4 6 7
6 4
2 2 2 3 3 3
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) 收藏的更多相关文章
- NYOJ-73 比大小  AC                                                    分类:            NYOJ             2014-01-17 21:29    195人阅读    评论(0)    收藏
		
典型的大数题目,这只是大数的比较,到时还有大数加减乘除,更加还有乘方,对于大数,一般用数组或者字符串,因为其他的结构类型一般都没有那么大 的范围!! 这道题目需要你仔细回想怎么比较俩个数字的大小,考虑 ...
 - 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 ...
 - Hadoop集群日常运维                                                    分类:            A1_HADOOP             2015-03-01 21:26    502人阅读    评论(0)    收藏
		
(一)备份namenode的元数据 namenode中的元数据非常重要,如丢失或者损坏,则整个系统无法使用.因此应该经常对元数据进行备份,最好是异地备份. 1.将元数据复制到远程站点 (1)以下代码将 ...
 - 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 ...
 - 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 ...
 - 哈夫曼树-Fence Repair                                                       分类:            树             POJ             2015-08-05 21:25    2人阅读    评论(0)    收藏
		
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...
 - 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关键字的函 ...
 - shell入门之函数应用                                                    分类:            学习笔记             linux             ubuntu             2015-07-10 21:48    77人阅读    评论(0)    收藏
		
最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...
 - 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 ...
 
随机推荐
- JavaScript eval_r() 函数
			
定义和用法 eval_r() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval_r(string) 参数 描述 string 必需.要计算的字符串,其中含有要计算的 ...
 - Conductor Server
			
安装 要求 数据库:Dynomite https://github.com/Netflix/dynomite 索引后端: Elasticsearch 2.x http ...
 - Path-O-LOGIC Keynote
			
[Path-O-LOGIC Keynote] 1. OnSpawned()OnSpawned(SpawnPool pool) 2. OnDespawned()OnDespawned(SpawnPool ...
 - bootstrap下拉框的例子,提示Error: Bootstrap's JavaScript requires jQuery
			
bootstrap很多js依赖jquery,所以需要引入jquery 遇到的问题: 页面访问提示:Error: Bootstrap's JavaScript requires jQuery 解 ...
 - [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
			
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
 - ROS两种workspace  :overlay rosbuild_ws->catkin_ws->ROS库,
			
概念 ROS里面有一系列概念,作为初学者,最先接触的概念无非是node, package和workspace. node node是ROS里面最小的执行单位,你可以把node看成是一个main函数,当 ...
 - 在控制台远程连接mysql数据库时,出现ERROR 2049 (HY000)错误
			
问题的原因是,你本地的数据库版本过高,而远程的数据库版本低. 解决方法:在连接时加上 --skip-secure-auth 参数就可以了. mysql -h主机 -u用户名 -p密码 --skip- ...
 - linux配置网络
			
1.配置网络信息 linux系统的第一块网卡信息 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE 设备名 ONBOOT 系统启动后是否自动启动网卡设备 ...
 - 基于TCP的套接字
			
tcp服务端 1 ss = socket() #创建服务器套接字 2 ss.bind() #把地址绑定到套接字 3 ss.listen() #监听链接 4 inf_loop: #服务器无限循环 5 c ...
 - iOS.ObjC.Basic-Knowledge
			
1. ObjC的基础 2. ObjC2.0中的编译指令 3. ObjC Runtime 4. ObjC Object Model 5. ObjC的新语法 6. FQA 1. ObjC的基础 2. Ob ...