fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, l, r, x;
  5. int c[20];
  6.  
  7. int ans;
  8.  
  9. void backtrack(int i, long long total_difficult, int mn, int mx, int cnt) {
  10. if (i == n + 1) {
  11. if (cnt >= 2 && l <= total_difficult && total_difficult <= r && mx - mn >= x) ans++;
  12. return;
  13. }
  14.  
  15. // bỏ qua vấn đề thứ i
  16. backtrack(i + 1, total_difficult, mn, mx, cnt);
  17.  
  18. // chọn vấn đề thứ i
  19. backtrack(i + 1, total_difficult + c[i], min(mn, c[i]), max(mx, c[i]), cnt + 1);
  20. }
  21.  
  22. int main() {
  23. ios::sync_with_stdio(0); cin.tie(0);
  24. cin >> n >> l >> r >> x;
  25. for (int i = 1; i <= n; i++) cin >> c[i];
  26.  
  27. ans = 0;
  28. backtrack(1, 0, 1e9, -1e9, 0);
  29. cout << ans << '\n';
  30. }
Success #stdin #stdout 0s 5300KB
stdin
3 5 6 1
1 2 3
stdout
2