CF 632D(Longest Subsequence-计数排序)

题意:给 n106 个数,选尽量多的数,使它们的lcm不超过 m106 ,输出任意一种方案。

考虑计数排序n个数,得到 cnt[1..m]
然后对每个数,将它自己加到其倍数上

O(m+m/2+m/3++m/m)=O(mlogm)

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[i]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (1000000+10)
int n,m;
int a[MAXN],cnt[MAXN];
int main()
{
// freopen("CF632D.in","r",stdin);
// freopen(".out","w",stdout);
    cin>>n>>m;
    For(i,n) {
        int x=read();
        a[i]=x;
        if (x>m) continue;
        else cnt[x]++;
    }
    ForD(i,m) {
        for(int j=2*i;j<=m;j+=i) cnt[j] += cnt[i]; 
    }
    int l=1;
    For(i,m) if (cnt[i] >cnt[l]) l=i;

    cout<<l<<' '<<cnt[l]<<endl;
    bool flag=0;
    For(i,n) 
        if (l%a[i]==0) {
        if (flag) putchar(' '); else flag=1;
        printf("%d",i);
    }
    puts("");   
    return 0;
}

你可能感兴趣的:(CF 632D(Longest Subsequence-计数排序))