Longest Subsequence CodeForces 632D 数学/筛法

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;
}
基于Swin Transformer与ASPP模块的图像分类系统设计与实现 本文介绍了一种结合Swin Transformer与空洞空间金字塔池化(ASPP)模块的高效图像分类系统。该系统通过融合Transformer的全局建模能力和ASPP的多尺度特征提取优势,显著提升了模型在复杂场景下的分类性能。 模型架构创新 系统核心采用Swin Transformer作为骨干网络,其层次化窗口注意力机制能高效捕获长距离依赖关系。在特征提取阶段,创新性地引入ASPP模块,通过并行空洞卷积(膨胀率6/12/18)和全局平均池化分支,实现多尺度上下文信息融合。ASPP输出经1x1卷积降维后与原始特征拼接,有效增强了模型对物体尺寸变化的鲁棒性。 训练优化策略 训练流程采用Adam优化器(学习率0.0001)和交叉熵损失函数,支持多GPU并行训练。系统实现了完整的评估指标体系,包括准确率、精确率、召回率、特异度和F1分数等6项指标,并通过动态曲线可视化模块实时监控训练过程。采用早停机制保存最佳模型,验证集准确率提升可达3.2%。 工程实现亮点 1. 模块化设计:分离数据加载、模型构建和训练流程,支持快速迭代 2. 自动化评估:每轮训练自动生成指标报告和可视化曲线 3. 设备自适应:智能检测CUDA可用性,无缝切换训练设备 4. 中文支持:优化可视化界面的中文显示与负号渲染 实验表明,该系统在224×224分辨率图像分类任务中,仅需2个epoch即可达到92%以上的验证准确率。ASPP模块的引入使小目标识别准确率提升15%,特别适用于医疗影像等需要细粒度分类的场景。未来可通过轻量化改造进一步优化推理速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值