[CF912E]Prime Gift
阿新 • • 發佈:2020-07-20
題目
題解
考慮二分一個 \(x\),看一看在 \([1,x]\) 中的合法數是否有 \(k\) 個
如何計算?我們考慮折半搜尋,將 \(p[i]\) 分成前一半和後一半,在前一半中列舉 \(m[i]\),然後在後一般中看一看小於等於 \(\frac{x}{m[i]}\) 的數有多少個,累加起來便是 \([1,x]\) 中的個數
此題較為簡單,但是細節較多:
- 不能直接乘法,可能會爆
long long
; - 由於我們前一半是列舉的,所以我們應儘可能將前一半的數的個數構造得小一點,而在後面二分的地方可以合理地大一點(所以我程式碼中有一個對於 \(p\) 從大到小排序的地方);
時間複雜度大概在 \(\mathcal O(5e6\log 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; }