1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; typedef pair<int,int> PII; const int N = 2e5 + 10; const int mod = 1e9 + 7; vector<ll> arr, a, b; ll n, m; void dfs(ll l, ll r, ll sum, ll sign){ if(sum>m){ return ; } if(l>r){ if(sign==0){ a.push_back(sum); } else if (sign==1){ b.push_back(sum); } return ; } dfs(l+1,r,sum+arr[l],sign); dfs(l+1,r,sum,sign);
} void solve() { cin >> n >> m; arr = vector<ll> (n+1); for (int i= 1;i<=n;i++) cin >> arr[i]; dfs(1, n/2, 0, 0); dfs(n/2+1, n, 0, 1); sort(a.begin(), a.end()); ll ans = 0; for (int i = 0;i<b.size();i++){ ans += upper_bound(a.begin(), a.end(), m-b[i]) - a.begin(); } cout << ans;
} int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tomorin=1; while (tomorin--) solve(); return 0; }
|