[CF912E]Prime Gift

题目

传送门

题解

考虑二分一个 \(x\),看一看在 \([1,x]\) 中的合法数是否有 \(k\)

如何计算?我们考虑折半搜索,将 \(p[i]\) 分成前一半和后一半,在前一半中枚举 \(m[i]\),然后在后一般中看一看小于等于 \(\frac{x}{m[i]}\) 的数有多少个,累加起来便是 \([1,x]\) 中的个数

此题较为简单,但是细节较多:

  1. 不能直接乘法,可能会爆 long long
  2. 由于我们前一半是枚举的,所以我们应尽可能将前一半的数的个数构造得小一点,而在后面二分的地方可以合理地大一点(所以我代码中有一个对于 \(p\) 从大到小排序的地方);

时间复杂度大概在 \(\mathcal O(5e6\log 5e6)\),这个 \(5e6\) 是打表得来的

代码

#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=16;
const int MAXSIZE=5e6;//打表得出
const LL MAXNUM=1000000000000000000ll;

int n;

LL p[MAXN+5],k;

inline int cmp(const int x,const int y){return x>y;}

vector<LL>a[2];
LL sz[2];

inline void Init(){
    a[0].push_back(0),a[1].push_back(0);
    n=read(1);
    rep(i,1,n)p[i]=read(1ll);
    sort(p+1,p+n+1,cmp);
    k=read(1ll);
}

inline void putIn(const int ind,const LL x){a[ind].push_back(x);++sz[ind];}

void Get(const int ind,const int now,const int lim,const LL x){
    if(now==lim+1)return putIn(ind,x);
    LL tmp=x;
    rep(i,0,1000){
        // printf("Now p[%d] == %lld, i_end_ == %d\n",now,p[now],i_end_);
        // printf("Now tmp == %lld, MAXNUM/p[%d] == %lld\n",tmp,now,MAXNUM/p[now]);
        Get(ind,now+1,lim,tmp);
        if(tmp>MAXNUM/p[now])return;
        tmp=tmp*p[now];
    }
}

inline LL Count(const LL x){
    // printf("Now Count %lld\n",x);
    LL ret=0;int pos=sz[1];
    rep(i,1,sz[0]){
        // printf("Now i == %d, x/a[0][%d] == %lld\n",i,i,x/a[0][i]);
        // printf("Now a[1][%d] == %lld\n",pos,a[1][pos]);
        while(pos>0 && x/a[0][i]<a[1][pos]){
            // printf("x/a[0][%d] == %lld, but a[1][pos(%d)] == %lld\n",i,x/a[0][i],pos,a[1][pos]);
            --pos;
        }
        // printf("pos == %d\n",pos);
        if(pos==0)break;
        ret+=pos;
    }
    return ret;
}

inline void bisearch(){
    LL l=1,r=MAXNUM,mid,ans,ret;
    while(l<=r){
        // printf("Now l == %lld, r == %lld\n",l,r);
        ret=Count(mid=(l+r)>>1);
        // printf("When mid == %lld, ret == %lld\n",mid,ret);
        if(ret<k)l=mid+1;
        else ans=mid,r=mid-1;
    }
    // printf("out of while!\n");
    writc(ans,'\n');
}

signed main(){
    Init();
    Get(0,1,n>>1,1);
    Get(1,(n>>1)+1,n,1);
    // sort(a[0]+1,a[0]+sz[0]+1);
    // sort(a[1]+1,a[1]+sz[1]+1);
    sort(a[0].begin(),a[0].end());
    sort(a[1].begin(),a[1].end());
    // printf("this is a[0] :\n");
    // rep(i,1,sz[0])writc(a[0][i],'\n');
    // printf("\nthis is a[1] :\n");
    // rep(i,1,sz[1])writc(a[1][i],'\n');
    bisearch();
	return 0;
}
posted @ 2020-07-20 09:12  Arextre  阅读(78)  评论(0编辑  收藏  举报