#include #include #include #include using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; ll S; cin >> n >> S; vector a(n); for(int i = 0; i < n; i++){ cin >> a[i]; } // For each index, find the maximum exponent k (starting at 1) such that a[i]^k <= S. // For each index i, possible exponents are 1,2,...,maxExp[i]. vector maxExp(n, 0); for (int i = 0; i < n; i++){ ll power = 1; int cnt = 0; // We must take at least k=1. while (true) { // Check for overflow: if power > S/a[i], then multiplying by a[i] would exceed S. if (power > S / a[i]) break; power *= a[i]; cnt++; } if(cnt < 1){ // If even a[i]^1 > S, no valid sequence exists. cout << 0 << "\n"; return 0; } maxExp[i] = cnt; } // Split indices into two groups. int mid = n / 2; vector groupAIndices, groupBIndices; for (int i = 0; i < mid; i++){ groupAIndices.push_back(i); } for (int i = mid; i < n; i++){ groupBIndices.push_back(i); } // For a given group, recursively enumerate all possible sums. auto recGroup = [&](auto &recGroup, int pos, ll sum, const vector& indices, vector& store) -> void { if (pos == indices.size()){ store.push_back(sum); return; } int i = indices[pos]; ll power = 1; // Try exponents from 1 up to maxExp[i] (if the sum does not exceed S). for (int exp = 1; exp <= maxExp[i]; exp++){ if(exp == 1) power = a[i]; // a[i]^1 else power *= a[i]; // a[i]^exp, computed iteratively. if(sum + power > S) break; // further exponents only increase the sum recGroup(recGroup, pos + 1, sum + power, indices, store); } }; vector groupASums, groupBSums; recGroup(recGroup, 0, 0LL, groupAIndices, groupASums); recGroup(recGroup, 0, 0LL, groupBIndices, groupBSums); // Sort one of the groups (here groupBSums) for binary search. sort(groupBSums.begin(), groupBSums.end()); // For each sum from group A, count how many sums from group B satisfy: sum_A + sum_B <= S. ll answer = 0; for (ll s : groupASums){ ll remain = S - s; // upper_bound finds the first element greater than remain. auto it = upper_bound(groupBSums.begin(), groupBSums.end(), remain); answer += (it - groupBSums.begin()); } cout << answer << "\n"; return 0; }