Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence
题目连接:
http://www.codeforces.com/contest/632/problem/D
Description
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.
Sample Input
7 8
6 2 9 2 7 2 3
Sample Output
6 5
1 2 4 6 7
Hint
题意
给n个数,然后你要找到一个最长的序列,使得序列中的数的lcm小于m
题解:
lcm和顺序无关,所以我们只要统计每个数有多少个就好了
然后再类似筛法一样,去筛每一个数的因子有多少个就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
int cnt[maxn];
int dp[maxn];
int a[maxn];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]<=m)cnt[a[i]]++;
}
for(int i=m;i;i--)
for(int j=i;j<=m;j+=i)
dp[j]+=cnt[i];
long long ans1=-1,ans2=-1;
for(int i=1;i<=m;i++)
if(dp[i]>ans1)
ans1=dp[i],ans2=i;
cout<<ans2<<" "<<ans1<<endl;
for(int i=1;i<=n;i++)
if(ans2%a[i]==0)
cout<<i<<" ";
cout<<endl;
}
Educational Codeforces Round 9 D. Longest Subsequence dp的更多相关文章
- Educational Codeforces Round 9 D - Longest Subsequence
D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...
- Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法
D. Longest k-Good Segment 题目连接: http://www.codeforces.com/contest/616/problem/D Description The arra ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 51 D. Bicolorings(dp)
https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...
- Educational Codeforces Round 1 E. Chocolate Bar dp
题目链接:http://codeforces.com/contest/598/problem/E E. Chocolate Bar time limit per test 2 seconds memo ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- Educational Codeforces Round 32 E. Maximum Subsequence
题目链接 题意:给你两个数n,m,和一个大小为n的数组. 让你在数组找一些数使得这些数的和模m最大. 解法:考虑 dfs但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往 ...
- Educational Codeforces Round 39
Educational Codeforces Round 39 D. Timetable 令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间 转移就是枚举第\ ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
随机推荐
- Eclipse svn 忽略文件夹/ svn 设置不同步
Eclipse 开发中我们经常用到SVN插件, 但是对于某些文件的缓存来说, 我们只要有操作缓存便会保存一次, 每次提交很是麻烦, 小编这里教你一种不同步某个文件夹的方法 工具/原料 MyEclips ...
- vue-实现倒计时功能
JavaScript 创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日.时.分.秒数,每隔一秒递归执行一次. msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以 ...
- Keil MDK 5.14 仿真时System Viewer菜单显示空白和Peripherals菜单无外设寄存器
keil mdk5.14新建工程进行仿真时,进入Debug环境发现System Viewer菜单显示空白,Peripherals菜单没有外设寄存器.如图1和图2所示.打开Oprons for Targ ...
- Reactor与Proactor区别
如网络编程中accept之后等待数据到达,并且读取数据为例: Reactor: 基于同步IO 1. 线程等待读取socket数据,将socketfd添加到事件分派器中,如添加到epoll: 2. 事件 ...
- Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
转自:http://www.th7.cn/system/lin/201606/167750.shtml 我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一 ...
- "Flags mismatch irq" register interrupt handler error
Question : When you see the log "Flags mismatch irq ............", maybe you use the same ...
- python基础===单元测试unittest
''' 编写一个名为Employee 的类,其方法__init__()接受名.姓和年薪,并 将它们都存储在属性中.编写一个名为give_raise()的方法,它默认将年薪增加5000 美元,但也能够接 ...
- HighGUI图形图像界面初步——鼠标操作
OpenCV中的鼠标操作和滑动条的消息映射方式很类似,都是通过一个中介函数配合一个回调函数来实现的,创建和指定滑动条回调函数为createTrackbar, 而指定鼠标操作消息回调函数的函数为setM ...
- squid 代理服务
squid代理服务分为两种方式: 一.正向代理(用在企业的办公环境中,员工上网需要通过Squid代理来上网) 客户端发送请求到代理服务器,代理服务器去向真正的服务器请求结果,并将结果返回给客户端 二. ...
- tornado write render redirect IP
write 用法( self.flush() ) render (跳转指定网页)用法 redirect(跳转指定路由)用法 self.request.remote_ip 显示用户 IP 地址 less ...