Small and easy solution


#1
int divCount(int n)
{
    bool hash[n + 1];
    memset(hash, true, sizeof(hash));
    for (int p = 2; p * p < n; p++)
        if (hash[p] == true)
            for (int i = p * 2; i < n; i += p)
                hash[i] = false;

    int total = 1;
    for (int p = 2; p <= n; p++) {
        if (hash[p]) {
            int count = 0;
            if (n % p == 0) {
                while (n % p == 0) {
                    n = n / p;
                    count++;
                }
                total = total * (count + 1);
            }
        }
    }
    return total;
}

vector<int> Solution::solve(vector<int> &A, vector<int> &B) {
    
    int n=A.size();
    vector<long long> G;

    if(A.size()==1) {
        return {A[0]};
    }

    sort(A.begin(), A.end());

    for(int i=0;i<n;i++) {
        copy(A.begin()+i, A.end(), back_inserter(G));
    }

    for(int i=0;i<G.size();i++) {
//        cout<<G[i]<<" ";
        int n_div = divCount(G[i]);
        if(n_div%2==0) {
            G[i] = pow(G[i], n_div/2);
            G[i]%=100000007;
        } else {
            G[i] = pow(G[i], n_div/2) * sqrt(G[i]);
            G[i]%=100000007;
        }

    }

    sort(G.begin(), G.end(), greater<int>());
    vector<int> ret;
    for(int i=0;i<B.size();i++) {
        ret.push_back(G[B[i]-1]);
    }
    return ret;
}

#2

It gives wrong answer even for small cases like
A-> 1 4 2
B-> 1 2 3 4 5 6
Correct ans is 8 8 8 8 2 1
But your code gives 8 8 8 2 2 1