Longest Subsequence CodeForces 632D 数学/筛法

本文介绍了一个算法问题,即从给定数组中找到最长的子序列,使得这些元素的最小公倍数不超过给定值m。文章详细解释了如何通过预处理和筛选策略来优化搜索过程,从而将时间复杂度降低到可接受范围内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example
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

从数组中挑选出尽可能多的数,保证其最小公倍数在m内。可以注意到此题m上限为10^6,也就是说最小公倍数不会超过这个值,一定程度上可以暴力。一开始只想得到暴力求解,1~m枚举最小公倍数再一个个检验是否整除,时间复杂度10^6*10^6显然不现实。在运算过程中可以发现,ka是a的倍数,下次枚举到(k+1)a时完全可以跳过计算。所以可以运用类似筛法的思想对数组进行预处理,a[i]在m范围内的倍数k*a[i]增加一次计数,意思是k*a[i]在这个数组里存在一个因数,预处理大约nlogm的复杂度,最后对公倍数的count数组比较最大值只需m的复杂度
要多留意数据的范围,m范围很小因此可以枚举公倍数,因数的个数也能用数组储存

#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iterator>
#include <string.h>
using namespace std;
typedef long long ll;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=0x3f3f3f3f;
const int sz=1000005;
int n,m;
int a[sz],cnt[sz],num[sz];
int main()
{
    //freopen("r.txt","r",stdin);
    while(scanf("%d",&n)!=EOF){
        scanf("%d",&m);
        int ans=1,t=0,tmp;
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(a[i]<=m) num[a[i]]++;
        }
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=m;i++){
            if(num[i]==0) continue;
            for(int j=i;j<=m;j+=i){
                cnt[j]+=num[i];
                //这一步是优化,没有会TLE,一百万个一百万内的数,其中肯定有重复,进一步压缩节约时间
            }
        }
        for(int i=1;i<=m;i++){
            if(cnt[i]>t){
                t=cnt[i];
                ans=i;
            }
        }
        printf("%d %d\n",ans,t);
        for(int i=1;t>0&&i<=n;i++){
            if(a[i]<=ans&&ans%a[i]==0)
                printf("%d ",i);
        }
        printf("\n");
    }
    return 0;
}
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值