codeforces632D. Longest Subsequence (最小公倍数)
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,并把序列顺序输出来。
思路:这题容易想到用dp[i][j]表示当前遍历到第i个数,最小公倍数为j的最长子序列的长度是多少。状态转移为dp[i][lcm(a[i],k)]=dp[i-1][k]+1,但是这样的复杂度就爆了,所以要换种思路。我们可以枚举1~m中的每一个数作为最小公倍数,然后数出n个数中多少个数是它的因子,然后取最大就行了,这里要把a[i]的个数记下来,不然时间复杂度就爆。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000050
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef long double ldb;
int len[maxn],a[maxn],cnt[maxn];
int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=m;i++)len[i]=0,cnt[i]=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]<=m)cnt[a[i] ]++;
}
for(i=1;i<=m;i++){
for(j=i;j<=m;j+=i){
len[j]+=cnt[i];
}
}
int maxx=0;int maxlen=0;
for(i=1;i<=m;i++){
if(len[i]>maxlen){
maxx=i;
maxlen=len[i];
}
}
if(maxx==0){
printf("1 0\n");continue;
}
printf("%d %d\n",maxx,maxlen);
int flag=1;
for(i=1;i<=n;i++){
if(maxx%a[i]==0){
if(flag){
flag=0;printf("%d",i);
}
else printf(" %d",i);
}
}
printf("\n");
}
return 0;
}
codeforces632D. Longest Subsequence (最小公倍数)的更多相关文章
- D. Longest Subsequence
D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏
D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Educational Codeforces Round 9 D - Longest Subsequence
D - Longest Subsequence 思路:枚举lcm, 每个lcm的答案只能由他的因子获得,类似素数筛搞一下. #include<bits/stdc++.h> #define ...
- CF632D Longest Subsequence
D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
- [徐州网络赛]Longest subsequence
[徐州网络赛]Longest subsequence 可以分成两个部分,前面相同,然后下一个字符比对应位置上的大. 枚举这个位置 用序列自动机进行s字符串的下标转移 注意最后一个字符 #include ...
- 【Henu ACM Round #12 D】 Longest Subsequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个数字出现的次数cnt[x]; (大于1e6的直接忽略) 另外用一个数组z[1e6] 然后for枚举x 第二层for枚举x的倍 ...
- codeforces 632D. Longest Subsequence 筛法
题目链接 记录小于等于m的数出现的次数, 然后从后往前筛, 具体看代码. #include <iostream> #include <vector> #include < ...
- CodeForces 632D Longest Subsequence
暴力. 虽然$a[i]$最大有${10^9}$,但是$m$最大只有${10^6}$,因此可以考虑暴力. 记$cnt[i]$表示数字$i$有$cnt[i]$个,记$p[i]$表示以$i$为倍数的情况下, ...
随机推荐
- 【剑指 Offer】03.数组中重复的数字
题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...
- Python3爬取小说并保存到文件
问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...
- Nginx 实现动态负载均衡(Nginx-1.10.1 + Consul v0.6.4)
一直也没有找到合适的类似Socat + Haproxy 的组合能用在Nginx,后来发现了Nginx的几个模块,但是也存在各种不足. 而且Nginx 在大流量的情况下nginx -s reload 是 ...
- ps 2020 下载
一款极具实用价值的作图软件--ps,由于正版价格昂贵,所以这里分享破解版的资源.b话少说,下面是下载链接和安装步骤: 下载链接: 百度网盘链接:https://pan.baidu.com/s/1XPf ...
- 【RAC】Oracle 10g RAC相关启停命令,维护命令
Oracle10g RAC关闭及启动步骤 情况1:需要关闭DB(所有实例),OS及Server. a.首先停止Oracle10g环境 $ lsnrctl stop (每个节点上停止监听,也可以用s ...
- Unsafe Filedownload - Pikachu
概述: 文件下载功能在很多web系统上都会出现,一般我们当点击下载链接,便会向后台发送一个下载请求,一般这个请求会包含一个需要下载的文件名称,后台在收到请求后会开始执行下载代码,将该文件名对应的文件r ...
- 使用ogg实现oracle到postgresql表的实时同步
参考:https://docs.oracle.com/goldengate/c1221/gg-winux/index.html https://blog.51cto.com/hbxztc/188071 ...
- EFCore 5 新特性 —— Savepoints
EFCore 5 中的 Savepoints Intro EFCore 5中引入了一个新特性,叫做 Savepoints,主要是事务中使用,个人感觉有点类似于 Windows 上的系统还原点,如果事务 ...
- 封装JSONP 函数,方便请求发送
封装JSONP 函数,方便请求发送 封装jsonp的代码和封装Ajax的代码非常的相似!可以参照食用偶! <button id="btn">点击我发送请求!</b ...
- (02)-Python3之--列表(list)操作
1.定义 列表的关键字:list 列表以[]括起来,数据之间用 , 隔开.列表当中的数据,可以是任意类型.数值是可以重复的. 列表元素是 可变的,顺序是 有序的. 例如: b = ["萝卜& ...