codeforces 632D. Longest Subsequence 筛法

题目链接

记录小于等于m的数出现的次数, 然后从后往前筛, 具体看代码。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int a[1000005], cnt[1000005], maxx, pos, flag;
int main()
{
    int n, m;
    cin>>n>>m;
    for(int i = 1; i<=n; i++) {
        scanf("%d", &a[i]);
        if(a[i]<=m) {
            cnt[a[i]]++;
            flag = 1;
        }
    }
    if(!flag) {
        puts("1 0");
        return 0;
    }
    for(int i = m; i>=1; i--) {
        for(int j = 2*i; j<=m; j+=i) {
            cnt[j] += cnt[i];
        }
    }
    for(int i = 1; i<=m; i++) {
        if(cnt[i]>maxx) {
            maxx = cnt[i];
            pos = i;
        }
    }
    cout<<pos<<" "<<maxx<<endl;
    for(int i = 1; i<=n; i++) {
        if(pos%a[i]==0)
            printf("%d ", i);
    }
    return 0;
}

 

你可能感兴趣的:(codeforces 632D. Longest Subsequence 筛法)