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 ...
随机推荐
- 双内网渗透代理之reGeorg+Proxifier
由于这个工具第一次体验感觉还不错,很稳定.因此在这记录一下reGeorg+Proxifier的配置及其使用. 下载地址 :https://github.com/sensepost/reGeorg.gi ...
- mysql查询语句的执行顺序(重点)
一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...
- Bookmarks_www2
Bookmarks Bookmarks alexis- (Alex Incogito) - Repositories · GitHub GitHub - aetcnc-Arduino_DeltaHMI ...
- linux和ubuntu防火墙相关命令
1.永久有效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2.即刻生效 开启: service iptables start 关闭: se ...
- [New learn]AutoLayout调查基于IB
代码:https://github.com/xufeng79x/AutoLayout-IB 1.简介 Autolayout旨在解决不同高宽度的屏幕下的显示问题,通过增加给控件增加约束来达到不同屏幕间的 ...
- eclipse 配置jsp
1.安装jdk和jre 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- P2737 [USACO4.1]麦香牛块Beef McNuggets(完全背包+数论确定上界)
题目链接:https://www.luogu.org/problem/show?pid=2737 题目大意:农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在 ...
- sql server 2008 R2无法连接127.0.0.1报错 Server error:40(错误:53)
在公司用sql server 2008 R2很好的,回家连接127.0.0.1就报错.sql server2008R2主机名和.都可以登录,连接127.0.0.1出错,在与 SQL Server 建立 ...
- 五:ZooKeeper的集群命令客户端的链接和命令操作的使用
一:zookeeper客户端链接[1]进入zookeeper的安装目录的bin目录下 # cd /opt/zookeeper/bin[2]敲击链接客户端的命令(zkCli.sh) ...
- 微软企业库5.0 学习之路——第六步、使用Validation模块进行服务器端数据验证
前端时间花了1个多星期的时间写了使用jQuery.Validate进行客户端验证,但是那仅仅是客户端的验证,在开发项目的过程中,客户端的信息永远是不可信的,所以我们还需要在服务器端进行服务器端的验证已 ...