CodeForces912E 折半+二分+双指针

http://codeforces.com/problemset/problem/912/E

题意·n个质数,问因子中只包含这其中几个质数的第k大的数是多少

最显然的想法是暴力搜预处理出所有的小于1e18的值,排序后直接输出,但是n的范围是16,仔细一想发现事实并不可行,不论是时间还是空间都不允许。

本题的难点在于考虑到用折半将其分成两个集合,预处理出每个集合内所有可能的数,由于一个集合最多只有8个,这是一个允许爆搜的范围,与处理完了之后二分出答案即可,check函数里需要用到双指针的技巧来查找给定数字在两个集合乘积中的位置,

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)  
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))  
#define Sca(x) scanf("%d", &x)
#define Scl(x) scanf("%lld",&x);  
#define Pri(x) printf("%d
", x)
#define Prl(x) printf("%lld
",x);  
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long  
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second 
typedef vector<int> VI;
const double eps = 1e-9;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7; 
int N,M,tmp,sa,sb;
LL K;
int A[maxn],B[maxn];
vector<LL>a,b;
void init(int x[],vector<LL>& y,int sum,LL now,int k){
    if(k == sum + 1){
        y.pb(now);
        return;
    } 
    LL ans = 1;
    while(now <= 1e18 / ans){
        init(x,y,sum,now * ans,k + 1);
        if(ans > 1e18 / x[k]) break;
        ans *= x[k];
    }
}
LL check(LL t){
    int cnt1 = a.size(); int cnt2 = b.size() - 1;
    int tmp1 = 0,tmp2 = 0;
    LL ans = 0;
    for(int i = 0 ; i < cnt1; i ++){
        if(a[i] > t) break;
        while(b[cnt2] > t / a[i] && cnt2 >= 0) cnt2--;
        ans += cnt2 + 1;
    }
    return ans;
}
LL solve(){
    LL l = 0, r = 1e18;
    LL ans = 0;
    while(l <= r){
        LL m = (l + r) >> 1;
        if(check(m) >= K){
            
            r = m - 1;
            ans = m;
        }else {
            l = m + 1;
        }
    }
    return ans;
}
int main()
{
    Sca(N);
    sa = sb = 0;
    For(i,1,N){
        int x; Sca(x);
        if(i & 1) A[++sa] = x;
        else B[++sb] = x;
    }
    Scl(K);
    init(A,a,sa,1,1);
    init(B,b,sb,1,1);
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    Prl(solve());
    #ifdef VSCode
    system("pause");
    #endif
    return 0;
}
原文地址:https://www.cnblogs.com/Hugh-Locke/p/9574892.html