Educational Codeforces Round 9 D. Longest Subsequence dp

本文介绍了一道来自 CodeForces 平台的问题 D 的解题思路及实现过程。题目要求找出一个最长序列,使该序列中所有数的最小公倍数不超过给定值 m。通过统计每个数出现的次数,并使用类似筛法的方法来求解。

题目链接:http://codeforces.com/contest/632/problem/D
题意:给n个数,然后你要找到一个最长的序列,使得序列中的数的lcm小于m
解法:cm和顺序无关,所以我们只要统计每个数有多少个就好了,然后再类似筛法一样,去筛每一个数的因子有多少个就好了。比较考思维的一道好题。

//CF 632D

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, m, a[maxn], cnt[maxn], dp[maxn];//dp[i]代表lcm为i的最长的长度

int main()
{
    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 = 1; i <= m; 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值