标签:inter poi ret 判断 amp turn define else pointer
挂个链接CodeForces
题目大意:给定k个质数,求出约数中只有由这几个数组合一下(可以多次用一个数)的第k个值
分析:
我们看到n是16,然后如果爆搜n/2是可以过的.所以考虑Meeting In The Middle
他让我们求第k个数,理所应当地想到二分答案.
然后我们先把所有的组合求出来,然后二分答案判断就好了.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<queue>
#include<algorithm>
#define ll long long
#define re register
using namespace std;
inline int gi(){
int sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
inline ll gl(){
ll sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
int n,p[20],t1,t2;
ll s1[5000010],s2[5000010],k;
void dfs1(int x,ll s){
if(x>n){s1[++t1]=s;return;}
for(ll w=1;;w*=p[x]){dfs1(x+2,s*w);if(1e18/p[x]<w*s)break;}
}
void dfs2(int x,ll s){
if(x>n){s2[++t2]=s;return;}
for(ll w=1;;w*=p[x]){dfs2(x+2,s*w);if(1e18/p[x]<w*s)break;}
}
int main(){
n=gi();
for(re int i=1;i<=n;i++)p[i]=gi();
sort(p+1,p+n+1);
dfs1(1,1);dfs2(2,1);
sort(s1+1,s1+t1+1);sort(s2+1,s2+t2+1);
k=gi();
ll l=0,r=1e18,ans;
while(l<=r){
ll mid=(l+r)>>1,tot=0;
for(re int i=1,p=t2;i<=t1;i++,tot+=p)
while(p && mid/s1[i]<s2[p])p--;
if(tot<k)l=mid+1;
else{ans=mid;r=mid-1;}
}
printf("%lld\n",ans);
return 0;
}
CF912E Prime Gift(Meeting In The Middle+Two pointers)
标签:inter poi ret 判断 amp turn define else pointer
原文地址:https://www.cnblogs.com/cjgjh/p/9769489.html